creationCallbacks

Contents

Add remove, or modify existing fields, their description, types and interfaces.

Introduction

creationCallbacks are invoked during the GraphQL schema generation - every time Guillotine or an application extending Guilltine is started or stopped.

Important to remember:

  • Can only be defined for types and interfaces.

    field renaming is not supported.
  • May be invoked in a different order from what is defined in the creationCallbacks property.

  • If you add or modify some fields to 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

This property must be an object with the following structure:

creationCallbacks: {
    <GraphQLTypeName>: function (params) {
        // if you want to add new fields
        params.addFields({
            <newFieldName>: {
                type: <GraphQLType>,
                args: {
                    <argName>: <GraphQLType>,
                    // the rest of arguments definitions ...
                },
            },
        });

        // if you want to remove existing fields
        params.removeFields([
             "fieldName 1",
             "filedName 2",
             // the rest of fieldName ...
        ]);

        // If you want to modify existing fields.
        // Take aware that all arguments will be rewritten by new arguments.
        // If type or args are not provided then will be used type and args from original field.
        params.modifyFields({
            <existingFieldName>: {
                type: <GraphQLType>,
                args: {
                    <argName>: <GraphQLType>,
                    // the rest of arguments definitions ...
                },
            },
        });

        // If you want to change description
        params.setDescription("New description");

        // If you want to add or remove some interfaces to object.
        // Take aware that interfaces will be rewritten by new array of interfaces.
        // Also, developer is responseble for implmentation of interface, do not forget to add all required fields.
        params.setInterfaces([
            <GraphQLInterfaceType or GraphQLReferenceType>
        ]);
    },
    // the rest of creation callback definition ...
}
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:

creationCallbacks: {
    MyCustomType: function (params) {
        params.addFields({
            extraField: {
                type: graphQL.GraphQLString,
            }
        });

        params.removeFields([
            "fieldToRemove1",
            "fieldToRemove2",
        ]);

        params.modifyFields({
          fieldToModify: {
            type: graphQL.GraphQLString,
          }
        });
    },

}

To know how to set or override data fetcher for a field you can refer to the resolvers section.


Contents

Contents