Ask a Question

The @generate Directive

The @generate directive is used to specify which GraphQL APIs are generated for a given type.

Here’s the GraphQL definition of the directive

input GenerateQueryParams {
	get: Boolean
	query: Boolean
	password: Boolean
	aggregate: Boolean
}

input GenerateMutationParams {
	add: Boolean
	update: Boolean
	delete: Boolean
}
directive @generate(
	query: GenerateQueryParams,
	mutation: GenerateMutationParams,
	subscription: Boolean) on OBJECT | INTERFACE

The corresponding APIs are generated by setting the Boolean variables inside the @generate directive to true. Passing false forbids the generation of the corresponding APIs.

The default value of the subscription variable is false while the default value of all other variables is true. Therefore, if no @generate directive is specified for a type, all queries and mutations except subscription are generated.

Example of @generate directive

type Person @generate(
    query: {
        get: false,
        query: true,
        aggregate: false
    },
    mutation: {
        add: true,
        delete: false
    },
    subscription: false
) {
    id: ID!
    name: String!
}

The GraphQL schema above will generate a queryPerson query and addPerson, updatePerson mutations. It won’t generate getPerson, aggregatePerson queries nor a deletePerson mutation as these have been marked as false using the @generate directive. Note that the updatePerson mutation is generated because the default value of the update variable is true.