UNPKG

@graphql-tools/utils

Version:

Common package containing utils and types for GraphQL tools

52 lines (51 loc) • 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.asGetArgumentValuesVariables = asGetArgumentValuesVariables; exports.coerceGetVariableValuesResult = coerceGetVariableValuesResult; const graphql_1 = require("graphql"); function emptySources() { return Object.create(null); } /** * GraphQL 17 `getArgumentValues` expects `{ coerced, sources }`; GraphQL 16 accepts a flat map. * Pass execution `variableValues` (often a flat record) through this helper before calling * `getArgumentValues`. */ function asGetArgumentValuesVariables(variableValues) { if (variableValues == null) { return undefined; } if (graphql_1.versionInfo.major >= 17 && !('coerced' in variableValues)) { return { coerced: variableValues, sources: emptySources(), }; } return variableValues; } /** * Normalize `getVariableValues` success/error shapes across GraphQL 16 (`{ coerced }`) and * GraphQL 17 (`{ variableValues: { coerced, sources } }`) for helpers like `shouldIncludeNode`. */ function coerceGetVariableValuesResult(result) { if (result.errors != null && result.errors.length > 0) { return { errors: result.errors }; } if (result.variableValues != null) { return { variableValues: result.variableValues }; } if (result.coerced != null) { return { variableValues: { coerced: result.coerced, sources: emptySources(), }, }; } return { variableValues: { coerced: Object.create(null), sources: emptySources(), }, }; }