If you’re using GraphQL with Ruby on Rails, you can use generators to:
You can add GraphQL to a Rails app with graphql:install:
rails generate graphql:install
This will:
app/graphql/Query type definitionMutation type definition with a base mutation classgraphiql-railsEnable ActiveRecord::QueryLogs and add GraphQL-related metadata (using GraphQL::Current)
After installing you can see your new schema by:
bundle installrails serverlocalhost:3000/graphiql--directory=DIRECTORY will directory where generated files should be saved (default is app/graphql)--schema=MySchemaName will be used for naming the schema (default is #{app_name}Schema)--skip-graphiql will exclude graphiql-rails from the setup--skip-mutation-root-type will not create of the mutation root type--skip-query-logs will skip the QueryLogs setup--relay will add Relay-specific code to your schema--batch will add GraphQL::Batch to your gemfile and include the setup in your schema--playground will include graphql_playground-rails in the setup (mounted at /playground)--api will create smaller stack for API only appsSeveral generators will add GraphQL types to your project. Run them with -h to see the options:
rails g graphql:objectrails g graphql:inputrails g graphql:interfacerails g graphql:unionrails g graphql:enumrails g graphql:scalarThe graphql:object and graphql:input generators can detect the existence of an ActiveRecord class with the same name, and scaffold all database columns as fields/arguments using appropriate GraphQL types and nullability detection
--namespaced-types will generate each one of the object/input/interface/… types under separate Types::Objects::*/Types::Inputs::*/Types::Interfaces::*/… namespaces and foldersYou can prepare a Relay Classic mutation with
rails g graphql:mutation #{mutation_name}
You can generate a Relay Classic create, update or delete mutation for a given model with
rails g graphql:mutation_create #{model_class_name}
rails g graphql:mutation_update #{model_class_name}
rails g graphql:mutation_delete #{model_class_name}
model_class_name accepts both namespace/class_type and Namespace::ClassType formats.
This mutation also accepts the --namespaced-types flag, to keep it consistent with the scaffolded Object and Input classes from the type generators
You can prepare a GraphQL::Batch loader with
rails g graphql:loader