Table of Contents

Type Assignment

Types like inputs, interfaces, objects, and sources can be assigned to other classes. This assignment means that the GraphQL component is directly associated with that other class, and they will cooperate in providing a smooth interaction.

Setting It

You can simply set an assignment as following:

# app/graphql/sources/admin_source.rb
class GraphQL::AdminSource < GraphQL::ARSource
  self.assigned_to = 'AdminUser'

It is recommended to always assign to the name of the class, so you don’t generate unnecessary loading of constants.

Read more about recommendations.

How it Works

Whenever the GraphQL component needs to check if it is interacting with a value, it will first check if the value is from the assigned_class.

Some classes, like sources, also guarantee that the assigned class is based on another class to ensure its features are compatible.

Type Map

Another interesting thing the assignment does is add an alias to the Type Map for that class to the proper key to find the underlying GraphQL component. That means:

:001 > Rails::GraphQL.type_map.fetch(User)
    => #<GraphQL::Object User ...