UNPKG

@graphql-tools/executor

Version:

Fork of GraphQL.js' execute function

164 lines (163 loc) 5.74 kB
import { GraphQLError, GraphQLInputType, ValueNode } from 'graphql'; import { Maybe, VariableValues } from '@graphql-tools/utils'; type FragmentVariableValues = any; type OnErrorCB = (path: ReadonlyArray<string | number>, invalidValue: unknown, error: GraphQLError) => void; /** * Validate that the provided input value is allowed for this type, collecting * all errors via a callback function. * @param inputValue - JavaScript value to validate. * @param type - GraphQL input type to validate the value against. * @param onError - Callback invoked for each validation error and path. * @param hideSuggestions - Whether suggestion text should be omitted from errors. * @returns Nothing. * @example * ```ts * // Collect validation errors with their input paths. * import { * GraphQLInputObjectType, * GraphQLInt, * GraphQLNonNull, * } from 'graphql/type'; * import { validateInputValue } from 'graphql/utilities'; * * const ReviewInput = new GraphQLInputObjectType({ * name: 'ReviewInput', * fields: { * stars: { type: new GraphQLNonNull(GraphQLInt) }, * }, * }); * const errors = []; * * validateInputValue({ stars: 'bad' }, ReviewInput, (path, _invalidValue, error) => { * errors.push({ message: error.message, path }); * }); * * errors; // => [ { message: 'Expected value of type "Int", found: "bad".', path: ['stars'] } ] * ``` * @example * ```ts * // This variant hides suggestion text for unknown input fields. * import { GraphQLInputObjectType, GraphQLString } from 'graphql/type'; * import { validateInputValue } from 'graphql/utilities'; * * const ReviewInput = new GraphQLInputObjectType({ * name: 'ReviewInput', * fields: { * comment: { type: GraphQLString }, * }, * }); * const errors = []; * * validateInputValue( * { rating: 'extra field' }, * ReviewInput, * (_path, _invalidValue, error) => { * errors.push(error.message); * }, * true, * ); * * errors; // => ['Expected value of type "ReviewInput" not to include unknown field "rating", found: { rating: "extra field" }.'] * ``` */ export declare function validateInputValue(inputValue: unknown, type: GraphQLInputType, onError: OnErrorCB, hideSuggestions?: boolean): void; /** * Validate that the provided input literal is allowed for this type, collecting * all errors via a callback function. * * If variable values are not provided, the literal is validated statically * (not assuming that those variables are missing runtime values). * @param valueNode - GraphQL value AST node to validate. * @param type - GraphQL input type to validate the literal against. * @param onError - Callback invoked for each validation error and path. * @param variables - Operation variable values returned by getVariableValues. * @param fragmentVariableValues - Fragment variable values for the current fragment scope. * @param hideSuggestions - Whether suggestion text should be omitted from errors. * @returns Nothing. * @example * ```ts * // Validate literal input values and collect literal paths. * import { parseValue } from 'graphql/language'; * import { * GraphQLInputObjectType, * GraphQLInt, * GraphQLNonNull, * } from 'graphql/type'; * import { validateInputLiteral } from 'graphql/utilities'; * * const ReviewInput = new GraphQLInputObjectType({ * name: 'ReviewInput', * fields: { * stars: { type: new GraphQLNonNull(GraphQLInt) }, * }, * }); * const errors = []; * * validateInputLiteral( * parseValue('{ stars: "bad" }'), * ReviewInput, * (error, path) => { * errors.push({ message: error.message, path }); * }, * ); * * errors; // => [ { message: 'Expected value of type "Int", found: "bad".', path: ['stars'] } ] * ``` * @example * ```ts * // This variant resolves variable references using VariableValues from getVariableValues(). * import assert from 'node:assert'; * import { parse, parseValue } from 'graphql/language'; * import { GraphQLInt } from 'graphql/type'; * import { getVariableValues } from 'graphql/execution'; * import { buildSchema, validateInputLiteral } from 'graphql/utilities'; * * const schema = buildSchema(` * type Query { * review(stars: Int): String * } * `); * const document = parse('query ($stars: Int = 5) { review(stars: $stars) }'); * const operation = document.definitions[0]; * const result = getVariableValues(schema, operation.variableDefinitions, { * stars: '4', * }); * * assert('variableValues' in result); * * const errors = []; * validateInputLiteral( * parseValue('$stars'), * GraphQLInt, * (error) => errors.push(error.message), * result.variableValues, * undefined, * true, * ); * * errors; // => [] * ``` */ export declare function validateInputLiteral(valueNode: ValueNode, type: GraphQLInputType, onError: (error: GraphQLError, path: ReadonlyArray<string | number>) => void, variables?: Maybe<VariableValues>, fragmentVariableValues?: Maybe<FragmentVariableValues>, hideSuggestions?: Maybe<boolean>): void; /** * Creates a keyed JS object from an array, given a function to produce the keys * for each value in the array. * * This provides a convenient lookup for the array items if the key function * produces unique results. * @internal * @example * ```ts * const phoneBook = [ * { name: 'Jon', num: '555-1234' }, * { name: 'Jenny', num: '867-5309' }, * ]; * * const entriesByName = keyMap(phoneBook, (entry) => entry.name); * * Object.keys(entriesByName); // => ['Jon', 'Jenny'] * entriesByName['Jenny']; // => { name: 'Jenny', num: '867-5309' } * ``` */ export declare function keyMap<T>(list: ReadonlyArray<T>, keyFn: (item: T) => string): Record<string, T>; export {};