{"record":{"id":"528682e757e26318","repo":"dgraph-io/dgraph","slug":"type-s-doesn-t-have-a-key-directive","errorCode":null,"errorMessage":"type %s doesn't have a key Directive","messagePattern":"type (.+?) doesn't have a key Directive","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"graphql/schema/wrappers.go","lineNumber":1733,"sourceCode":"\tif len(representations) == 0 {\n\t\treturn nil, fmt.Errorf(\"expecting at least one item in `representations` argument\")\n\t}\n\trepresentation, ok := representations[0].(map[string]interface{})\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"error parsing %dth item in the `_representations` argument\", 0)\n\t}\n\ttypename, ok := representation[Typename].(string)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"unable to extract __typename from %dth item in the\"+\n\t\t\t\" `_representations` argument\", 0)\n\t}\n\ttyp := q.op.inSchema.schema.Types[typename]\n\tif typ == nil {\n\t\treturn nil, fmt.Errorf(\"type %s not found in the schema\", typename)\n\t}\n\tkeyDir := typ.Directives.ForName(apolloKeyDirective)\n\tif keyDir == nil {\n\t\treturn nil, fmt.Errorf(\"type %s doesn't have a key Directive\", typename)\n\t}\n\tkeyFldName := keyDir.Arguments[0].Value.Raw\n\n\t// initialize the struct to return\n\tentityReprs := &EntityRepresentations{\n\t\tTypeDefn: &astType{\n\t\t\ttyp:             &ast.Type{NamedType: typename},\n\t\t\tinSchema:        q.op.inSchema,\n\t\t\tdgraphPredicate: q.op.inSchema.dgraphPredicate,\n\t\t},\n\t\tKeyVals:                make([]interface{}, 0, len(representations)),\n\t\tKeyValToRepresentation: make(map[string]map[string]interface{}),\n\t}\n\tentityReprs.KeyField = entityReprs.TypeDefn.Field(keyFldName)\n\n\t// iterate over all the representations and parse\n\tfor i, rep := range representations {\n\t\trepresentation, ok = rep.(map[string]interface{})","sourceCodeStart":1715,"sourceCodeEnd":1751,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/graphql/schema/wrappers.go#L1715-L1751","documentation":"For federation entity resolution, the type named by __typename must declare an @key directive identifying its primary key field(s). This error is thrown when the type exists in the schema but has no @key directive, so the subgraph cannot resolve the entity from the representation.","triggerScenarios":"_entities query for a type defined in the subgraph schema without `@key(fields: \"...\")` (or `directive @key` from the federation spec) — e.g. an entity type missing federation annotations.","commonSituations":"Adding a type to a federation subgraph but forgetting the @key directive; converting a plain type into an entity without updating the schema; federation spec version mismatch (e.g. missing `extend type ... @key`).","solutions":["Add a @key directive to the type: `type Product @key(fields: \"id\") { id: ID! ... }`","Ensure the federation @key directive is declared/imported in the schema","Republish the supergraph after adding the directive"],"exampleFix":"// before\ntype Product { id: ID! name: String }\n// after\ntype Product @key(fields: \"id\") { id: ID! name: String }","handlingStrategy":"validation","validationCode":"// before deploying, assert every entity type has @key\nfor _, t := range schema.Types {\n\tif isEntityType(t) && t.Directives.ForName(\"key\") == nil {\n\t\treturn fmt.Errorf(\"type %s missing @key\", t.Name)\n\t}\n}","typeGuard":"func hasKeyDirective(t *ast.Definition) bool {\n\treturn t != nil && t.Directives.ForName(\"key\") != nil\n}","tryCatchPattern":"entity, err := q.RepresentationsArg()\nif err != nil && strings.Contains(err.Error(), \"key Directive\") {\n\treturn nil, fmt.Errorf(\"type not resolvable as federated entity: %w\", err)\n}","preventionTips":["Annotate every federated entity type with @key(fields: ...) at schema authoring time","Declare the federation @key directive in the schema (or import the federation spec)","Republish the supergraph after adding entities so the gateway knows the keys"],"tags":["graphql","federation","schema","directives"],"backgroundTag":"missing-federation-key-directive","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}