Add GraphQL support to custom Field Types¶
If you want to use custom Field Types in GraphQL, you need to map them. Their values and field definition structure, need to be defined, to interact with them using GraphQL. For example:
|Name||Possible field value||Resolver||Field definition|
Map a custom Field Type¶
There are two ways of mapping a custom Field Type:
You need to write a custom
FieldDefinitionMapper if the field definition settings and constraints impact how it is mapped to GraphQL.
For example, the selection Field Type has a "multiple" option.
If set to false, it accepts and returns a single value,
but if set to true, it accepts and returns an array of values.
If your field definition doesn't require additional clarifications, you can map it with configuration.
Map with configuration¶
To map a custom Field Type with configuration use a compiler pass to modify a container parameter,
It is a hash that maps a Field Type identifier (
ezstring) to the following entries:
value_type- the GraphQL type values of the custom field. It can be a native type (string, int), or a custom type. If none is specified, string will be used.
value_resolver- how values of this field are resolved and passed to the defined value type. If not specified, it will receive the
Fieldobject for the field type:
definition_type- the GraphQL type the field definitions is mapped to. If not specified, it will use
Compiler pass example that should be placed in
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Map with a custom
FieldDefinitionMapper API uses service decorators.
To register your own mapper, make it decorate the
1 2 3 4 5
$innerMapper argument passes the decorated mapper to the constructor.
You can use the
DecoratingFieldDefinitionMapper from the
It requires that you implement the
getFieldTypeIdentifier method to tell which Field Type is covered by the mapper.
MyCustomFieldDefinitionMapper.php mapper to
1 2 3 4 5 6 7 8 9 10 11 12 13
FieldDefinitionMapper interface defines following methods:
mapToFieldValueType- returns the GraphQL type value for the defined field
mapToFieldValueInputType- returns the GraphQL type value for the field input value
mapToFieldValueResolver- returns the resolver, as an expression language string, values are resolved with
mapToFieldDefinitionType- returns the GraphQL type field definitions of the mapped type
Only implement methods that you need, the rest will be handled by other mappers (configuration or default).
When a mapper method is decorated, you need to call the decorated service method for unsupported types.
To do that, you need to replace
mapXXX by the method it is in:
1 2 3
It is required for every implemented method, so that other mappers are called for the other Field Types.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
The value type depends on the field definition allowed Content Types setting:
- for types that return Content items if there are no restrictions, or several types are allowed, the value will be an
The cardinality (single or collection) depends on the selection limit setting:
- if only one item is allowed, the value is unique:
- if there are no limits, or the limit is larger than 1, the value is a collection:
Field input mapping¶
mapToFieldValueInputType method is used to document what input type is expected by Field Types that require a more complex input value.
ezmatrix generates its own input types depending on the configured columns.
Example of a
MyCustomFieldDefinitionMapper mapper for a complex Field Type:
1 2 3 4 5 6 7 8 9 10 11
The following variables are available in the resolver's expression:
fieldis the current field, as an extension of the API's Field object that proxies properties requests to the Field Value
contentis the resolved Content item's
locationis the Content item's resolved location, for more information, see Querying Locations
itemis the content together with its location
SelectionFieldValueBuilder can be used as examples.