|
7 | 7 | * @flow
|
8 | 8 | */
|
9 | 9 |
|
10 |
| -import { print } from '../language/printer'; |
11 |
| -import type { |
12 |
| - ValueNode, |
13 |
| - ListValueNode, |
14 |
| - ObjectValueNode, |
15 |
| -} from '../language/ast'; |
16 |
| -import * as Kind from '../language/kinds'; |
17 |
| -import { |
18 |
| - isScalarType, |
19 |
| - isEnumType, |
20 |
| - isInputObjectType, |
21 |
| - isListType, |
22 |
| - isNonNullType, |
23 |
| -} from '../type/definition'; |
| 10 | +import { TypeInfo } from './TypeInfo'; |
| 11 | +import type { GraphQLError } from '../error/GraphQLError'; |
| 12 | +import type { ValueNode } from '../language/ast'; |
| 13 | +import { DOCUMENT } from '../language/kinds'; |
| 14 | +import { visit, visitWithTypeInfo } from '../language/visitor'; |
24 | 15 | import type { GraphQLInputType } from '../type/definition';
|
25 |
| -import isInvalid from '../jsutils/isInvalid'; |
26 |
| -import keyMap from '../jsutils/keyMap'; |
| 16 | +import { GraphQLSchema } from '../type/schema'; |
| 17 | +import { ValuesOfCorrectType } from '../validation/rules/ValuesOfCorrectType'; |
| 18 | +import { ValidationContext } from '../validation/validate'; |
27 | 19 |
|
28 | 20 | /**
|
29 |
| - * Utility for validators which determines if a value literal node is valid |
30 |
| - * given an input type. |
| 21 | + * Utility which determines if a value literal node is valid for an input type. |
31 | 22 | *
|
32 |
| - * Note that this only validates literal values, variables are assumed to |
33 |
| - * provide values of the correct type. |
| 23 | + * Deprecated. Rely on validation for documents containing literal values. |
34 | 24 | */
|
35 | 25 | export function isValidLiteralValue(
|
36 | 26 | type: GraphQLInputType,
|
37 | 27 | valueNode: ValueNode,
|
38 |
| -): Array<string> { |
39 |
| - // A value must be provided if the type is non-null. |
40 |
| - if (isNonNullType(type)) { |
41 |
| - if (!valueNode || valueNode.kind === Kind.NULL) { |
42 |
| - return [`Expected "${String(type)}", found null.`]; |
43 |
| - } |
44 |
| - return isValidLiteralValue(type.ofType, valueNode); |
45 |
| - } |
46 |
| - |
47 |
| - if (!valueNode || valueNode.kind === Kind.NULL) { |
48 |
| - return []; |
49 |
| - } |
50 |
| - |
51 |
| - // This function only tests literals, and assumes variables will provide |
52 |
| - // values of the correct type. |
53 |
| - if (valueNode.kind === Kind.VARIABLE) { |
54 |
| - return []; |
55 |
| - } |
56 |
| - |
57 |
| - // Lists accept a non-list value as a list of one. |
58 |
| - if (isListType(type)) { |
59 |
| - const itemType = type.ofType; |
60 |
| - if (valueNode.kind === Kind.LIST) { |
61 |
| - return (valueNode: ListValueNode).values.reduce((acc, item, index) => { |
62 |
| - const errors = isValidLiteralValue(itemType, item); |
63 |
| - return acc.concat( |
64 |
| - errors.map(error => `In element #${index}: ${error}`), |
65 |
| - ); |
66 |
| - }, []); |
67 |
| - } |
68 |
| - return isValidLiteralValue(itemType, valueNode); |
69 |
| - } |
70 |
| - |
71 |
| - // Input objects check each defined field and look for undefined fields. |
72 |
| - if (isInputObjectType(type)) { |
73 |
| - if (valueNode.kind !== Kind.OBJECT) { |
74 |
| - return [`Expected "${type.name}", found not an object.`]; |
75 |
| - } |
76 |
| - const fields = type.getFields(); |
77 |
| - |
78 |
| - const errors = []; |
79 |
| - |
80 |
| - // Ensure every provided field is defined. |
81 |
| - const fieldNodes = (valueNode: ObjectValueNode).fields; |
82 |
| - fieldNodes.forEach(providedFieldNode => { |
83 |
| - if (!fields[providedFieldNode.name.value]) { |
84 |
| - errors.push( |
85 |
| - `In field "${providedFieldNode.name.value}": Unknown field.`, |
86 |
| - ); |
87 |
| - } |
88 |
| - }); |
89 |
| - |
90 |
| - // Ensure every defined field is valid. |
91 |
| - const fieldNodeMap = keyMap(fieldNodes, fieldNode => fieldNode.name.value); |
92 |
| - Object.keys(fields).forEach(fieldName => { |
93 |
| - const result = isValidLiteralValue( |
94 |
| - fields[fieldName].type, |
95 |
| - fieldNodeMap[fieldName] && fieldNodeMap[fieldName].value, |
96 |
| - ); |
97 |
| - errors.push(...result.map(error => `In field "${fieldName}": ${error}`)); |
98 |
| - }); |
99 |
| - |
100 |
| - return errors; |
101 |
| - } |
102 |
| - |
103 |
| - if (isEnumType(type)) { |
104 |
| - if (valueNode.kind !== Kind.ENUM || !type.getValue(valueNode.value)) { |
105 |
| - return [`Expected type "${type.name}", found ${print(valueNode)}.`]; |
106 |
| - } |
107 |
| - |
108 |
| - return []; |
109 |
| - } |
110 |
| - |
111 |
| - if (isScalarType(type)) { |
112 |
| - // Scalars determine if a literal value is valid via parseLiteral(). |
113 |
| - try { |
114 |
| - const parseResult = type.parseLiteral(valueNode, null); |
115 |
| - if (isInvalid(parseResult)) { |
116 |
| - return [`Expected type "${type.name}", found ${print(valueNode)}.`]; |
117 |
| - } |
118 |
| - } catch (error) { |
119 |
| - const printed = print(valueNode); |
120 |
| - const message = error.message; |
121 |
| - return [`Expected type "${type.name}", found ${printed}; ${message}`]; |
122 |
| - } |
123 |
| - |
124 |
| - return []; |
125 |
| - } |
126 |
| - |
127 |
| - /* istanbul ignore next */ |
128 |
| - throw new Error(`Unknown type: ${(type: empty)}.`); |
| 28 | +): $ReadOnlyArray<GraphQLError> { |
| 29 | + const emptySchema = new GraphQLSchema({}); |
| 30 | + const emptyDoc = { kind: DOCUMENT, definitions: [] }; |
| 31 | + const typeInfo = new TypeInfo(emptySchema, undefined, type); |
| 32 | + const context = new ValidationContext(emptySchema, emptyDoc, typeInfo); |
| 33 | + const visitor = ValuesOfCorrectType(context); |
| 34 | + visit(valueNode, visitWithTypeInfo(typeInfo, visitor)); |
| 35 | + return context.getErrors(); |
129 | 36 | }
|
0 commit comments