{"record":{"id":"34c51c8c72406c73","repo":"cube-js/cube","slug":"variable-value-name-value-is-not-defined","errorCode":null,"errorMessage":"Variable \"${value.name.value}\" is not defined","messagePattern":"Variable \"(.+?)\" is not defined","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cubejs-api-gateway/src/graphql.ts","lineNumber":235,"sourceCode":"\nfunction parseArgumentValue(value: ValueNode, variables?: Record<string, any>) {\n  switch (value.kind) {\n    case 'BooleanValue':\n    case 'IntValue':\n    case 'StringValue':\n    case 'FloatValue':\n    case 'EnumValue':\n      return value.value;\n    case 'ListValue':\n      return value.values.map(v => parseArgumentValue(v, variables));\n    case 'ObjectValue':\n      return value.fields.reduce((obj, v) => ({\n        ...obj,\n        [v.name.value]: parseArgumentValue(v.value, variables),\n      }), {});\n    case 'Variable':\n      if (variables?.[value.name.value] === undefined) {\n        throw new Error(`Variable \"${value.name.value}\" is not defined`);\n      }\n\n      return variables[value.name.value];\n    default:\n      return undefined;\n  }\n}\n\nfunction getArgumentValue(node: FieldNode, argName: string, variables: Record<string, any> = {}) {\n  const argument = node.arguments?.find(a => a.name.value === argName)?.value;\n\n  if (argument?.kind === 'Variable') {\n    const varValue = variables[argument.name.value];\n    if (varValue === undefined) {\n      throw new Error(`Variable \"${argument.name.value}\" is not defined`);\n    }\n    return variables[argument.name.value];\n  }","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/cube-js/cube/blob/7d981676b36392fec34088b9afab6bdcad40207c/packages/cubejs-api-gateway/src/graphql.ts#L217-L253","documentation":"When translating a GraphQL query into a Cube query, parseArgumentValue resolves `Variable`-kind argument nodes against the supplied variables object. If a variable is referenced but not present (or present as undefined) in that map, this Error is thrown naming the variable.","triggerScenarios":"Calling getJsonQueryFromGraphQLQuery with a GraphQL document using `$var` in arguments but passing a variables record missing that key — or the key defined with an undefined value, e.g. variables: { dateRange: undefined }.","commonSituations":"Typos between the declared variable name in the query and the variables object key; forgetting to pass the variables argument entirely; constructing queries dynamically where a variable is only sometimes populated.","solutions":["Pass every variable referenced in the GraphQL document in the variables object with defined values.","Fix key-name typos so variable names in the query match the variables record exactly.","Substitute concrete values instead of variables for optional fields, or omit the argument when the value is absent.","Validate variables before calling: assert each declared variable has a defined value."],"exampleFix":"// before\ngetJsonQueryFromGraphQLQuery(doc, {}); // query uses $dateRange\n// after\ngetJsonQueryFromGraphQLQuery(doc, { dateRange: ['2024-01-01', '2024-01-31'] });","handlingStrategy":"validation","validationCode":"function assertVariablesDefined(doc, variables = {}) {\n  const used = new Set();\n  doc.definitions.forEach(d => (d.selectionSet?.selections || []).forEach(s =>\n    (s.arguments || []).forEach(a => { if (a.value?.kind === 'Variable') used.add(a.value.name.value); })));\n  const missing = [...used].filter(name => variables[name] === undefined);\n  if (missing.length) throw new Error(`Missing variables: ${missing.join(', ')}`);\n}","typeGuard":"function hasAllVariables(variables, requiredNames) {\n  return requiredNames.every(n => variables?.[n] !== undefined);\n}","tryCatchPattern":"try {\n  const cubeQuery = getJsonQueryFromGraphQLQuery(doc, variables);\n} catch (e) {\n  if (/is not defined/.test(e.message)) {\n    // supply the named variable from the message before re-running\n  }\n  throw e;\n}","preventionTips":["Keep variables object construction colocated with the query document definition","Type variables with TypeScript/GraphQL codegen so missing keys are compile-time errors","Avoid passing undefined-valued keys; delete keys instead of setting them to undefined"],"tags":["graphql","variables","validation","developer-error"],"backgroundTag":"graphql-variable-not-defined","analyzedSha":"7d981676b36392fec34088b9afab6bdcad40207c","analyzedAt":"2026-09-02T03:45:10.400Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}