creationCallbacks
Contents
Add remove, or modify existing fields, their description, types and interfaces.
Introduction
creationCallbacks are invoked when Guillotine builds the GraphQL schema. Application start, stop and uninstall events invalidate the cached schema; the callbacks run again when the next query triggers a rebuild. See Lifecycle and execution for the full execution flow.
Important to remember:
-
Can only be defined for types and interfaces.
field renaming is not supported. -
May be invoked in a different order than what is defined in the
creationCallbacksproperty. -
If you add or modify some fields of an interface in the creation callback, you must also do that for all implementations. Otherwise, you will get an error during schema generation.
-
If you modify some field, make sure that you implement correct data fetcher in the resolvers section. Otherwise, you can get an error during query execution.
| Conflicts. Multiple applications may define creationCallbacks. Conflicts between these may lead to schema generation errors or query errors. |
It is NOT recommended to make modification to the standard fields of the HeadlessCMS type. But if you have to do it then make sure that it will not introduce conflicts and side effects. |
Usage
Add functions to the creationCallbacks property returned by your extension function. Each key names the type or interface to modify. Callback parameters provide these methods:
-
addFieldsadds named fields with atypeand optionalargsmap. -
removeFieldsremoves fields by name. -
modifyFieldschanges a field’s type or arguments. Supplyingargsreplaces all existing arguments; omitted properties retain their original values. -
setDescriptionchanges the type’s description. -
setInterfacesreplaces the interfaces implemented by an object type. Include all fields required by those interfaces.
By default, Guillotine will throw an error if it tries to modify an unknown field. You can customise this behaviour by setting the graphql.extensions.modifyUnknownField property in the configuration. |
For example, given an existing MyCustomType with the named fields:
import type {Extensions, GraphQL} from '@enonic-types/guillotine';
export const extensions = (graphQL: GraphQL): Extensions => ({
creationCallbacks: {
MyCustomType: (params) => {
params.addFields({
extraField: {
type: graphQL.GraphQLString,
},
});
params.removeFields([
'fieldToRemove1',
'fieldToRemove2',
]);
params.modifyFields({
fieldToModify: {
type: graphQL.GraphQLString,
},
});
},
},
});
To learn how to set or override data fetcher for a field you can refer to the resolvers section.