The examples focus on queries, but you can use the same patterns with mutations and subscriptions.
Direct Definition
Directly defining fields means adding them straight into your schema. This is the simplest approach, best used sparingly for simple, immutable values.app/graphql/app_schema.rb
app/graphql/app_schema.rb
Set Definition
When multiple fields share common logic, define a class where they can all live together. This allows you to add shared methods and a list of fields, then import the set as a dependency in your schema.app/graphql/queries/migrations_set.rb
Available Classes
GraphQL::FieldSetGraphQL::QuerySetGraphQL::MutationSetGraphQL::SubscriptionSet
Standalone Definition
For complex fields requiring several methods and multiple parts for resolution, define a single class for a single field. This is similar to using Rails Services.app/graphql/queries/permissions.rb
For mutations, besides the
resolve method, you also have the perform method as an entry point. Read more about it in the mutations guide.Available Classes
GraphQL::FieldGraphQL::QueryGraphQL::MutationGraphQL::Subscription
Source Definition
Sources represent the highest level of abstraction. Instead of defining individual elements, you write a translator that can convert other classes into GraphQL components. Once you have the translator, all objects following the same pattern can be handled consistently. A great example is ActiveRecord, which already has its source implemented in this gem. All your models can be easily turned into their GraphQL counterparts.Unlike the previous examples, sources don’t require importing because they have their own mechanism for publishing fields.
app/graphql/sources/user_source.rb
app/graphql/app_schema.rb
Sources are one of the most powerful features of this gem.