Graphql-ruby: How to generate schema.json

Created on 28 Sep 2019  路  7Comments  路  Source: rmosolgo/graphql-ruby

I've seen that there's a SchemaPrinter class. I also see that there are some rake tasks we can include.

However, I am unable to figure out exactly how to print a schema.json from the command line in a vanilla rails app. Are there any instructions for this?

Most helpful comment

See here https://graphql-ruby.org/api-doc/1.9.12/GraphQL/RakeTask

Create a file lib/tasks/graphql.rake with this content

require "graphql/rake_task"
GraphQL::RakeTask.new(schema_name: "MySchema")
  • rails graphql:schema:dump dumps both a schema.graphql and a schema.json
  • rails graphql:schema:json dumps only a schema.json
  • rails graphql:schema:idl dumps only a schema.graphql

You can also customize the RakeTask.new for example, mine looks like this

GraphQL::RakeTask.new(
  load_schema: ->(_task) {
    require File.expand_path("../../config/environment", __dir__)
    MySchema
  },
  load_context: ->(_task) {
    { current_user: User.new(role: "admin") }
  }
)

I don't load the schema by name, because it needs the Rails environment and I didn't want to load the whole env just when the rake task was required. I also set a custom context with a dummy admin user, because of some masking that looks for this kind of user.

All 7 comments

See here https://graphql-ruby.org/api-doc/1.9.12/GraphQL/RakeTask

Create a file lib/tasks/graphql.rake with this content

require "graphql/rake_task"
GraphQL::RakeTask.new(schema_name: "MySchema")
  • rails graphql:schema:dump dumps both a schema.graphql and a schema.json
  • rails graphql:schema:json dumps only a schema.json
  • rails graphql:schema:idl dumps only a schema.graphql

You can also customize the RakeTask.new for example, mine looks like this

GraphQL::RakeTask.new(
  load_schema: ->(_task) {
    require File.expand_path("../../config/environment", __dir__)
    MySchema
  },
  load_context: ->(_task) {
    { current_user: User.new(role: "admin") }
  }
)

I don't load the schema by name, because it needs the Rails environment and I didn't want to load the whole env just when the rake task was required. I also set a custom context with a dummy admin user, because of some masking that looks for this kind of user.

I looked and looked and somehow missed this page in the docs. Thanks!

I had to add the proc for load_schema to get this to work.

I kind of wish the rake task was already loaded by the gem and ran with environment included. Generating a dump could be a lot more automatic. I had to dig around quite a bit.

Thanks for the feedback, @jhirn ! I think automatic loading could be accomplished with a Railtie but I haven't had a chance to look into it. If you're interested in taking a try, I'd be happy to review a PR with that feature!

Hey you鈥檙e a busy guy. Couldn鈥檛 be more satisfied with this library overall. It鈥檚 fantastic!

Maybe spitting something out with the generator would be nice? It seems people鈥檚 needs are different and the schema class is app specific, so generating a working base in lib/tasks one could extend might be ideal. If the task were in the gem, I feel like it might end up needing some configuration. Some commented examples and explanation in the generated file might even save a trip outside the editor :)

Thoughts? I鈥檇 be happy to take a swag at it if you agree with the approach.

:+1: to generator, love it!

If anyone runs into the following error in the console:

NameError: uninitialized constant Rake::DSL

Then you can just run the following and they should all return true:

require 'rake/dsl_definition'
require 'rake'
require "graphql/rake_task"

And if you are creating a custom rake task you can do something like:

namespace :graphql do
  task dump_schema: :environment do
    require "graphql/rake_task"

    GraphQL::RakeTask.new(
      load_schema: ->(_task) {
        require File.expand_path("../../app/graphql/api_schema", __dir__)
        ApiSchema
      },
      directory: "./config"
    )
    Rake::Task["graphql:schema:idl"].invoke
  end
end
Was this page helpful?
0 / 5 - 0 ratings