The @generate directive - Graphql

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.


This is a companion discussion topic for the original entry at https://dgraph.io/docs/graphql/schema/generate/

i have problema for generate directive @auth and @custom. here share error :slight_smile:
resolving updateGQLSchema failed because input:1: You can’t add scalar definitions. Only type, interface, union, input and enums are allowed in initial schema.\ninput:4: You can’t add scalar definitions. Only type, interface, union, input and enums are allowed in initial schema.\ninput:81: Point is a reserved word, so you can’t declare a type with this name. Pick a different name for the type.\ninput:109: Polygon is a reserved word, so you can’t declare a type with this name. Pick a different name for the type.\ninput:117: MultiPolygon is a reserved word, so you can’t declare a type with this name. Pick a different name for the type.\ninput:1: Int64 is a reserved word, so you can’t declare a SCALAR with this name. Pick a different name for the SCALAR.\ninput:4: DateTime is a reserved word, so you can’t declare a SCALAR with this name. Pick a different name for the SCALAR.\ninput:31: DgraphIndex is a reserved word, so you can’t declare a ENUM with this name. Pick a different name for the ENUM.\ninput:49: AuthRule is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:56: HTTPMethod is a reserved word, so you can’t declare a ENUM with this name. Pick a different name for the ENUM.\ninput:64: Mode is a reserved word, so you can’t declare a ENUM with this name. Pick a different name for the ENUM.\ninput:69: CustomHTTP is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:86: PointRef is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:91: NearFilter is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:96: PointGeoFilter is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:105: PointListRef is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:113: PolygonRef is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:121: MultiPolygonRef is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:183: IntFilter is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:193: Int64Filter is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:203: FloatFilter is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:213: DateTimeFilter is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:223: StringTermFilter is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:228: StringRegExpFilter is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:232: StringFullTextFilter is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:237: StringExactFilter is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\ninput:247: StringHashFilter is a reserved word, so you can’t declare a INPUT_OBJECT with this name. Pick a different name for the INPUT_OBJECT.\n (Locations: [{Line: 3, Column: 4}])

Can you share your schema or a gist of it ? Looking at the errors it looks like the generated schema (generated by Dgraph internally) is used as input schema. The generated schema contains definitions of scalers, polygons and so on. It is not needed to be part of the schema which is used while updating schema using curl .
Can you also share how you are updating the schema ?