|
| 1 | +import { describe, it } from 'mocha'; |
| 2 | + |
| 3 | +import { expectJSON } from '../../__testUtils__/expectJSON.js'; |
| 4 | + |
| 5 | +import { parse } from '../../language/parser.js'; |
| 6 | + |
| 7 | +import type { GraphQLSchema } from '../../type/schema.js'; |
| 8 | +import { validateSchema } from '../../type/validate.js'; |
| 9 | + |
| 10 | +import { validate } from '../../validation/validate.js'; |
| 11 | + |
| 12 | +import { buildSchema } from '../../utilities/buildASTSchema.js'; |
| 13 | + |
| 14 | +import { execute } from '../execute.js'; |
| 15 | + |
| 16 | +async function executeQuery(args: { |
| 17 | + schema: GraphQLSchema; |
| 18 | + query: string; |
| 19 | + rootValue?: unknown; |
| 20 | +}) { |
| 21 | + const { schema, query, rootValue } = args; |
| 22 | + const document = parse(query); |
| 23 | + return execute({ |
| 24 | + schema, |
| 25 | + document, |
| 26 | + rootValue, |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +describe('Execute: default arguments', () => { |
| 31 | + it('handles interfaces with fields with default arguments', async () => { |
| 32 | + const schema = buildSchema(` |
| 33 | + type Query { |
| 34 | + someInterface: SomeInterface |
| 35 | + } |
| 36 | +
|
| 37 | + interface SomeInterface { |
| 38 | + echo(value: String! = "default"): String |
| 39 | + } |
| 40 | +
|
| 41 | + type SomeType implements SomeInterface { |
| 42 | + echo(value: String!): String |
| 43 | + } |
| 44 | + `); |
| 45 | + |
| 46 | + const query = ` |
| 47 | + { |
| 48 | + someInterface { |
| 49 | + ... on SomeType { |
| 50 | + echo |
| 51 | + } |
| 52 | + echo |
| 53 | + } |
| 54 | + } |
| 55 | + `; |
| 56 | + |
| 57 | + const schemaErrors = validateSchema(schema); |
| 58 | + |
| 59 | + expectJSON(schemaErrors).toDeepEqual([]); |
| 60 | + |
| 61 | + const queryErrors = validate(schema, parse(query)); |
| 62 | + |
| 63 | + expectJSON(queryErrors).toDeepEqual([ |
| 64 | + { |
| 65 | + // This fails validation only for the object, but passes for the interface. |
| 66 | + message: |
| 67 | + 'Argument "SomeType.echo(value:)" of type "String!" is required, but it was not provided.', |
| 68 | + locations: [{ line: 5, column: 13 }], |
| 69 | + }, |
| 70 | + ]); |
| 71 | + |
| 72 | + const rootValue = { |
| 73 | + someInterface: { |
| 74 | + __typename: 'SomeType', |
| 75 | + echo: 'Runtime error raised, not called!', |
| 76 | + }, |
| 77 | + }; |
| 78 | + |
| 79 | + expectJSON(await executeQuery({ schema, query, rootValue })).toDeepEqual({ |
| 80 | + data: { |
| 81 | + someInterface: { |
| 82 | + echo: null, |
| 83 | + }, |
| 84 | + }, |
| 85 | + errors: [ |
| 86 | + { |
| 87 | + message: |
| 88 | + 'Argument "value" of required type "String!" was not provided.', |
| 89 | + path: ['someInterface', 'echo'], |
| 90 | + locations: [{ line: 5, column: 13 }], |
| 91 | + }, |
| 92 | + ], |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments