Builder
To create a GraphQl schema, you need to use the Swift\GraphQl\Schema\Builder\Builder
. This class is used to generated objects will be interpreted by the Compiler to generate the Schema. The builder comes with the following static methods.
#
Add a Query or MutationTo register a query or mutation we have to add a field to the Query or Mutation type, which are already predefined in the Schema. How extending the type works is explained in the following section. See the example below how the AuthTokenGet mutation is added to the Mutations type.
#
Extending a typeIt is likely that at some point you will want or have to extend a type in the Schema. The following section explains how to do this. For example when adding a Query or Mutation, you will have to extend the Query or Mutation type to add a field.
Extending a field is done by all extendType( string $name, \Closure $closure )
method. The $name
parameter is the name of the type you want to extend. The $closure
parameter is a closure that will be called with the type builder as parameter. In this Closure (like in the example above) you can add fields to the type and register new types that didn't exist before.
#
Referring to other typesFor GraphQl Resolver to work it is important to refer the Core GraphQl type as type to use. Those will only be available after the Schema is compiled. Another option is that the type you're referring to is not yet defined in the building phase. There it is always possible (like in the example above) to lazily refer to types with a Closure. This Closure will only be called when the type is needed in the Resolver. This is where the Registry comes in handy.
The Registry has a static property $typeMap
that contains all types that are defined in the Schema. This property is used to refer to types that are not yet defined in the building phase, but will be after compilation.