isValidLiteralValue.js.flow 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // @flow strict
  2. import { type GraphQLError } from '../error/GraphQLError';
  3. import { Kind } from '../language/kinds';
  4. import { type ValueNode } from '../language/ast';
  5. import { visit, visitWithTypeInfo } from '../language/visitor';
  6. import { ValuesOfCorrectType } from '../validation/rules/ValuesOfCorrectType';
  7. import { ValidationContext } from '../validation/ValidationContext';
  8. import { type GraphQLInputType } from '../type/definition';
  9. import { GraphQLSchema } from '../type/schema';
  10. import { TypeInfo } from './TypeInfo';
  11. /**
  12. * Utility which determines if a value literal node is valid for an input type.
  13. *
  14. * Deprecated. Rely on validation for documents containing literal values.
  15. *
  16. * This function will be removed in v15
  17. */
  18. export function isValidLiteralValue(
  19. type: GraphQLInputType,
  20. valueNode: ValueNode,
  21. ): $ReadOnlyArray<GraphQLError> {
  22. const emptySchema = new GraphQLSchema({});
  23. const emptyDoc = { kind: Kind.DOCUMENT, definitions: [] };
  24. const typeInfo = new TypeInfo(emptySchema, undefined, type);
  25. const context = new ValidationContext(emptySchema, emptyDoc, typeInfo);
  26. const visitor = ValuesOfCorrectType(context);
  27. visit(valueNode, visitWithTypeInfo(typeInfo, visitor));
  28. return context.getErrors();
  29. }