Table of Contents
Types
This gem provides all 6 GraphQL types: enums, inputs,
interfaces, objects, scalars,
and unions, and they all inherit from the same base Type
class.
This base class helps to maintain common information about its children’s classes.
Here is all the information you can get from them:
Base type
The base_type
returns the top-most class of the type, ignoring the Type
class.
Kind
The kind
returns a symbol that indicates the kind of the type, which is one
of the first 6 of this list in snake case.
Kind Enum
Same as above, but returning the string value instead.
Indicators
- The
abstract?
checks if the type is marked as abstract. - The
hidden?
checks if the type is marked as hidden. - The
input_type?
checks if the type can be used as an input; - The
output_type?
checks if the type can be used as an output; - The
leaf_type?
checks if the type is a leaf (enum or scalar); - The
operational?
checks if it is a fake type like_Query
.
Hidden
Unavailable This feature is yet to be published.
The hidden setting allow you to use types and directives that will never be exposed to a request. It enables you to use the GraphQL structure to meta-configure itself, like with authorization.
# app/graphql/enums/roles.rb
self.hidden = true
Beware even if a type is marked as hidden, it will still be published to the Type Map, which may cause unexpected overrides. To avoid that, use an isolated namespace.
Metadata
Unavailable This feature is yet to be published.
Metadata is a simple Hash
added to types that may contain any additional information
you want to associate with them. See the example below for how to write and read values.
# Writing
metadata :counter, 1
# Reading
GraphQL::User.metadata[:counter]
Equivalency
All types and fields have an equivalency operator =~
.
This operator helps to identify if the left side can be used as the right side. The equivalency takes
into consideration several different factors, and each type has its own extension of this
operator. For example:
- An object is equivalent to an interface, if it implements that interface;
- An object is equivalent to a union, if it is a member of that union;
- The
float
scalar is equivalent to thetime
scalar because the latter inherits from the former.
Inline Creation
Schemas allow types to be created “inline”, meaning they won’t have a file of their own.
When types are created this way, their constant is added to a NestedTypes
module from where
the creation started.
Types created inline can almost be fully configured using named arguments. But, you can always pass a block and add things as if inside their classes.
This requires further implementations and documentation as well.
Although this is not recommended, it can be handy in cases where you just need to simply define an union or an enum.
# app/graphql/app_schema.rb
union 'Person', of_types: %w[User Admin]
enum 'Role', values: %i[admin support]
Read mode about inline types.