Update: I’ve extracted the configuration described in this post to a Rails plugin named graphql-rails-namespace. You should use the plugin instead, but I’ll leave this post up for posterity.
The GraphQL Ruby library encourages placing GraphQL modules and classes such as type definitions under the
app/graphql/ directory in Rails applications. Here’s an example from the GraphQL Ruby homepage:
class Types::ProfileType < Types::BaseObject
field :id, ID, null: false
field :name, String, null: false
field :avatar, Types::PhotoType
Because Rails treats all subdirectories of
app as root directories for the purpose of autoloading, classes in
app/graphql/types/ are nested in a top-level
Types module. In the above example,
Types is too general a top-level namespace for GraphQL type definitions. The GraphQL schema class defined in
app/graphql/schema.rb is named
Schema, but it’s specifically a GraphQL schema, not a generic schema. It would be nice to nest GraphQL-specific modules and classes in a GraphQL-specific namespace.