GraphqlTagPluck
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_listUsage
With a sample heredoc like the following inside a .rb file:
SAMPLE_QUERY = <<-GRAPHQL
query SampleQuery {
hoge
fuga {
piyo
}
}
GRAPHQLwill 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 (nullfor unnamed operations) -
source: the normalized GraphQL document of the definition -
type:query,mutation,subscription, orfragment -
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), ornullwhen 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_listLoading 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_pluckReleasing
Releases are automated by the Release workflow:
- Bump the version in the
VERSIONfile at the repository root in a pull request. - 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.