|
2 | 2 |
|
3 | 3 | import { describe, it } from 'mocha';
|
4 | 4 |
|
| 5 | +import { GraphQLSchema } from '../../type/schema'; |
| 6 | +import { GraphQLString } from '../../type/scalars'; |
| 7 | +import { GraphQLScalarType, GraphQLObjectType } from '../../type/definition'; |
| 8 | + |
5 | 9 | import { ValuesOfCorrectType } from '../rules/ValuesOfCorrectType';
|
6 | 10 |
|
7 |
| -import { expectValidationErrors } from './harness'; |
| 11 | +import { |
| 12 | + expectValidationErrors, |
| 13 | + expectValidationErrorsWithSchema, |
| 14 | +} from './harness'; |
8 | 15 |
|
9 | 16 | function expectErrors(queryStr) {
|
10 | 17 | return expectValidationErrors(ValuesOfCorrectType, queryStr);
|
11 | 18 | }
|
12 | 19 |
|
| 20 | +function expectErrorsWithSchema(schema, queryStr) { |
| 21 | + return expectValidationErrorsWithSchema( |
| 22 | + schema, |
| 23 | + ValuesOfCorrectType, |
| 24 | + queryStr, |
| 25 | + ); |
| 26 | +} |
| 27 | + |
13 | 28 | function expectValid(queryStr) {
|
14 | 29 | expectErrors(queryStr).to.deep.equal([]);
|
15 | 30 | }
|
@@ -942,6 +957,45 @@ describe('Validate: Values of correct type', () => {
|
942 | 957 | );
|
943 | 958 | });
|
944 | 959 |
|
| 960 | + it('reports error for custom scalar that returns undefined', () => { |
| 961 | + const customScalar = new GraphQLScalarType({ |
| 962 | + name: 'CustomScalar', |
| 963 | + parseValue() { |
| 964 | + return undefined; |
| 965 | + }, |
| 966 | + }); |
| 967 | + |
| 968 | + const schema = new GraphQLSchema({ |
| 969 | + query: new GraphQLObjectType({ |
| 970 | + name: 'Query', |
| 971 | + fields: { |
| 972 | + invalidArg: { |
| 973 | + type: GraphQLString, |
| 974 | + args: { |
| 975 | + arg: { type: customScalar }, |
| 976 | + }, |
| 977 | + }, |
| 978 | + }, |
| 979 | + }), |
| 980 | + }); |
| 981 | + |
| 982 | + const expectedErrors = expectErrorsWithSchema( |
| 983 | + schema, |
| 984 | + ` |
| 985 | + { |
| 986 | + invalidArg(arg: 123) |
| 987 | + } |
| 988 | + `, |
| 989 | + ); |
| 990 | + |
| 991 | + expectedErrors.to.deep.equal([ |
| 992 | + { |
| 993 | + message: 'Expected value of type "CustomScalar", found 123.', |
| 994 | + locations: [{ line: 3, column: 27 }], |
| 995 | + }, |
| 996 | + ]); |
| 997 | + }); |
| 998 | + |
945 | 999 | it('allows custom scalar to accept complex literals', () => {
|
946 | 1000 | expectValid(`
|
947 | 1001 | {
|
|
0 commit comments