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
| Config | Type | Default | Description |
|---|---|---|---|
| description | string | description of the type in the schema | |
| input | boolean | string | false | whether this type is intended for input of queries & mutations |
| fields | map of string |TypeFieldConfig | placeholder | configuration 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:
| Shortcut | Resolved 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
| Config | Type | Default | Description |
|---|---|---|---|
| type | string | TypeFieldConfig | type of the field - can itself be a type definition, resulting in a new type that is created on-the-fly | |
| description | string | description of the field in the schema | |
| required | boolean | false | whether the GraphQL API considers this field mandatory |
| list | boolean | false | whether this field is in fact a list of it's type |
| resolve | ResolverFn | you 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:
| Type | Description |
|---|---|
| Accessory | the type we added here |
| AccessoryInfo | name of this type is determined by concatenating the type and field name |
| AccessoryInfoPrice | you can nest types as deep as necessary |
| AccessoryCurrencyEnum | an 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: AccessoryWe 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: AccessoryInputSince 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