Domain configuration
More
Type configuration

Type Configuration

Any entity results in a schema type in your GraphQL schema. Sometimes you just need a schema type to use as input or return type, e.g. of a custom query or mutation.

TypeConfig

ConfigTypeDefaultDescription
descriptionstringdescription of the type in the schema
inputboolean | stringfalsewhether this type is intended for input of queries & mutations
fieldsmap of string |TypeFieldConfigplaceholderconfiguration of the type's fields - can be nested

Example

enum: 
  Category: 
    - Exterior
    - Interior
    - Electronics
 
type:
  Accessory:
    fields:
      name: String!
      price: Int
      tags: String[]
      category: Category!
 
query: 
  accessories: 
    type: Accessory[]

You see that we added a Custom Query that uses our Type as return type. If we would just add a Type to the DomainConfiguration we wouldn't see it in the Schema since the Schema Generator would only include types that are accessible. Obviously, this "accessories" Query would not return any data, since no resolve function is provided, but is enough to see the "Accessory" Type in the schema.

Type Field Configuration

In the example above we added the fields with any scalar or schema type as field type. This is in fact a shortcut. The following shortcuts are possible:

ShortcutResolved TypeFieldConfig
Foo{type: 'Foo' }
Foo!{type: 'Foo', required: true }
[Foo] | Foo[]{type: 'Foo', list: true }
[Foo!] | [Foo!] | [Foo]! | [Foo]!{type: 'Foo', list: true, required: true }

TypeFieldConfig

ConfigTypeDefaultDescription
typestring | TypeFieldConfigtype of the field - can itself be a type definition, resulting in a new type that is created on-the-fly
descriptionstringdescription of the field in the schema
requiredbooleanfalsewhether the GraphQL API considers this field mandatory
listbooleanfalsewhether this field is in fact a list of it's type
resolveResolverFnyou can resolve the value of this field here

Nested Types

Instead of referring to existing Types you can create types on-the-fly by replacing the type name with a TypeConfig. Look at the next (intentionally overcomplicated) example:

type:
  Accessory:
    fields: 
      name: String!
      info: 
        fields: 
          description: String
          price:             
            fields: 
              currency: 
                - EUR
                - USD
              amount: Int!

From this configuration the following types will be generated into the schema:

TypeDescription
Accessorythe type we added here
AccessoryInfoname of this type is determined by concatenating the type and field name
AccessoryInfoPriceyou can nest types as deep as necessary
AccessoryCurrencyEnuman Enum that was created on-the-fly since we stated possible values for the type

You can use the on-the-fly generated types as any other type anywhere in the DomainConfiguration.

Input Types

In GraphQL you must differentiate whether a Type is intended to be used as input or output. So far our examples created output types that we could not use as input for a Custom Mutation. If we would try to use our "Accessory" as input for mutation:

mutation:
  createAccessory:
    args: 
      accessory: Accessory

We would get a schema build error:

Error: The type of Mutation.createAccessory(accessory:) must be Input Type but got: Accessory.

We can instead create an input type by using { input: true } and use it as args in the Mutation and the "Accessory" type as output type.

type:
  Accessory:
    fields: 
      name: String!
      price: Int!
 
  AccessoryInput:
    input: true
    fields: 
      name: String!
      price: Int!
 
mutation:
  createAccessory:
    type: Accessory
    args:       
      accessory: AccessoryInput

Since it is not so uncommon to have the same type as input and output of a Mutation you can create both types from the same configuration by providing the name of the input type instead of { input: true }. The following example would create exactly the same types as the example before:

type:
  Accessory:
    input: AccessoryInput
    fields: 
      name: String!
      price: Int!
      
mutation:
  createAccessory:
    type: Accessory
    args:       
      accessory: AccessoryInput