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