The project is in a healthy, maintained state
Plucks GraphQL document from Ruby codes
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

~> 2.0
~> 3.3, >= 3.3.7.2
~> 0.1.0
>= 1.8
 Project Readme

GraphqlTagPluck

Gem Version

A gem for plucking GraphQL queries / mutations / fragments defined by heredocs inside .rb files and exporting them into a json file.

Install this gem and run the following rake task to get the exported json file containing the list of GraphQL queries / mutations / fragments.

bundle exec rake graphql_tag_pluck:generate_graphql_operation_list

Usage

With a sample heredoc like the following inside a .rb file:

SAMPLE_QUERY = <<-GRAPHQL
  query SampleQuery {
    hoge
    fuga {
      piyo
    }
  }
GRAPHQL

will output json file like: (following json content is formatted)

{
  "SampleQuery": {
    "name": "SampleQuery",
    "source": "query SampleQuery {\n  hoge\n  fuga {\n    piyo\n  }\n}",
    "type": "query",
    "file": "app/clients/sample_client.rb",
    "line": 2,
    "constant": "SAMPLE_QUERY"
  }
}

Each entry contains:

  • name: the operation / fragment name (null for unnamed operations)
  • source: the normalized GraphQL document of the definition
  • type: query, mutation, subscription, or fragment
  • file: the file the definition was found in, relative to the current directory
  • line: the line the definition starts on
  • constant: the fully qualified constant the heredoc is assigned to (e.g. Clients::UserClient::USER_QUERY), or null when the heredoc is not assigned to a constant

Entries are keyed by definition name. Unnamed operations are keyed by their location (e.g. "app/clients/sample_client.rb:2"). Definition names must be unique: any name defined more than once, even identically, raises GraphqlTagPluck::Error and no file is written.

by executing

bundle exec rake graphql_tag_pluck:generate_graphql_operation_list

Loading the rake task

In a Rails app the task is available automatically. In any other project, add this to your Rakefile:

require "graphql_tag_pluck/rake_tasks"

Configuration

You can configure the following options by creating .graphqltagpluckconfig.yaml and specifying them inside.

  • graphql_heredoc_identifiers
    • An array of heredoc identifiers used to define GraphQL queries / mutations / fragments
    • default: ["GRAPHQL", "GQL"]
  • file_glob_pattern
    • A glob pattern to search for the heredocs containing GraphQL queries / mutations / fragments
    • default: "#{Dir.pwd}/**/*.rb"
  • output_path
    • The destination of exported json file path
    • default: "__generated__/graphql_operation_list.json"

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add graphql_tag_pluck

Releasing

Releases are automated by the Release workflow:

  1. Bump the version in the VERSION file at the repository root in a pull request.
  2. Merge it into main.

The workflow then tags the merge commit as vX.Y.Z, runs the specs and publishes the gem to RubyGems.org. If publishing fails, re-run it from Actions → Release → Run workflow; the existing tag is reused.

One-time setup: on RubyGems.org, open the gem's Trusted publishers settings and add a GitHub Actions publisher with repository indigolain/graphql_tag_pluck, workflow release.yml, and environment rubygems.org.

License

The gem is available as open source under the terms of the MIT License.