Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to generate schema.json #2501

Closed
sarink opened this issue Sep 28, 2019 · 7 comments
Closed

How to generate schema.json #2501

sarink opened this issue Sep 28, 2019 · 7 comments

Comments

@sarink
Copy link

sarink commented Sep 28, 2019

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?

@23tux
Copy link

23tux commented Sep 28, 2019

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.

@sarink
Copy link
Author

sarink commented Sep 28, 2019

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

@sarink sarink closed this as completed Sep 28, 2019
@jhirn
Copy link

jhirn commented Nov 6, 2019

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.

@rmosolgo
Copy link
Owner

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!

@jhirn
Copy link

jhirn commented Nov 28, 2019

Hey you’re a busy guy. Couldn’t be more satisfied with this library overall. It’s fantastic!

Maybe spitting something out with the generator would be nice? It seems people’s 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’d be happy to take a swag at it if you agree with the approach.

@rmosolgo
Copy link
Owner

👍 to generator, love it!

@sankalpk
Copy link

sankalpk commented Feb 25, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants