{"record":{"id":"faec2a1070c91b49","repo":"dgraph-io/dgraph","slug":"variable-s-is-missing-in-given-context","errorCode":null,"errorMessage":"variable $%s is missing in given context.","messagePattern":"variable \\$(.+?) is missing in given context\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"graphql/schema/remote.go","lineNumber":426,"sourceCode":"// matchArgSignature matches the type signature for arguments supplied in metadata with\n// corresponding remote arguments\nfunc matchArgSignature(md *argMatchingMetadata) error {\n\t// TODO: maybe add path information in error messages,\n\t// so they are more informative. Like if query was `query { userNames(car: {age: $var}) }`,\n\t// then for errors on `age`, we shouldn't just be putting `age` in error messages,\n\t// instead we should put `car.age`\n\tfor givenArgName, givenArgVal := range md.givenArgVals {\n\t\tremoteArgTyp, ok := md.remoteArgMd.typMap[givenArgName]\n\t\tif !ok {\n\t\t\treturn errors.Errorf(\"argument `%s` is not present in remote %s `%s`.\",\n\t\t\t\tgivenArgName, *md.operationType, *md.givenQryName)\n\t\t}\n\n\t\tswitch givenArgVal.Kind {\n\t\tcase ast.Variable:\n\t\t\tgivenArgTyp, ok := md.givenVarTypes[givenArgVal.Raw]\n\t\t\tif !ok {\n\t\t\t\treturn errors.Errorf(\"variable $%s is missing in given context.\", givenArgVal.Raw)\n\t\t\t}\n\t\t\t// TODO: we will consider ID as String for the purpose of type matching\n\t\t\t//rootType := givenArgTyp\n\t\t\t//for rootType.NamedType == \"\" {\n\t\t\t//\trootType = rootType.Elem\n\t\t\t//}\n\t\t\t//if rootType.NamedType == \"ID\" {\n\t\t\t//\trootType.NamedType = \"String\"\n\t\t\t//}\n\t\t\texpectedArgType := givenArgTyp.String()\n\t\t\tgotArgType := remoteArgTyp.String()\n\t\t\tif expectedArgType != gotArgType {\n\t\t\t\treturn errors.Errorf(\"found type mismatch for variable `$%s` in %s `%s`, expected\"+\n\t\t\t\t\t\" `%s`, got `%s`.\", givenArgVal.Raw, *md.operationType, *md.givenQryName,\n\t\t\t\t\texpectedArgType, gotArgType)\n\t\t\t}\n\t\t\t// deep check the remote type and verify it with the local schema.\n\t\t\tif err := matchDeepTypes(remoteArgTyp, md.remoteTypes, md.schema); err != nil {","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/graphql/schema/remote.go#L408-L444","documentation":"When an argument is supplied as a GraphQL variable (ast.Variable), matchArgSignature looks up the variable's type in the map of variable definitions collected from the operation (givenVarTypes). If the variable used in an argument value was never declared in the operation's variable definitions, this error is thrown. It catches variables that are referenced but never declared, which would otherwise fail at execution time on the remote.","triggerScenarios":"validateRemoteGraphql -> matchArgSignature on a query like `query { userNames(car: {age: $var}) }` where `$var` is used but there is no `($var: Int!)` variable definition on the operation, including nested object/list argument values.","commonSituations":"Copy-pasting a query fragment and losing the variable declaration line; dynamically building queries and appending variable usages without variable definitions; typo in variable name between usage and declaration ($userId vs $id).","solutions":["Add a variable definition to the operation: `query MyQuery($var: Int!) { ... }` matching where $var is used.","Fix typos so the variable usage and declaration names match exactly (including $ prefix handling).","If the value should be a literal instead, inline the scalar value rather than using a variable.","Regenerate queries from templates so variable declarations and usages are emitted together."],"exampleFix":"// before\nquery { userNames(car: {age: $var}) }\n// after\nquery($var: Int!) { userNames(car: {age: $var}) }","handlingStrategy":"validation","validationCode":"// Parse the operation and ensure every $var used has a definition\nfor _, v := range collectUsedVariables(queryAST) {\n    if _, ok := varDefinitions[v]; !ok {\n        return fmt.Errorf(\"variable $%s used but not declared\", v)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := validateRemoteGraphql(...); err != nil {\n    if strings.Contains(err.Error(), \"is missing in given context\") {\n        return fmt.Errorf(\"undeclared variable in query: %w\", err)\n    }\n    return err\n}","preventionTips":["Always emit variable definitions alongside variable usages in query builders","Use a GraphQL parser/validator (graphql-js validate) before composition","Avoid hand-editing generated queries in ways that drop definitions","Keep variable names unique per operation"],"tags":["graphql","variables","query-validation"],"backgroundTag":"undefined-graphql-variable","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}