|  | 
|  | 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 { buildSchema } from '../../utilities/buildASTSchema.js'; | 
|  | 8 | + | 
|  | 9 | +import type { ExecutionResult } from '../execute.js'; | 
|  | 10 | +import { execute } from '../execute.js'; | 
|  | 11 | + | 
|  | 12 | +const schema = buildSchema(` | 
|  | 13 | +  type Query { | 
|  | 14 | +    test(input: TestInputObject!): TestObject | 
|  | 15 | +  } | 
|  | 16 | +
 | 
|  | 17 | +  input TestInputObject @oneOf { | 
|  | 18 | +    a: String | 
|  | 19 | +    b: Int | 
|  | 20 | +  } | 
|  | 21 | +
 | 
|  | 22 | +  type TestObject { | 
|  | 23 | +    a: String | 
|  | 24 | +    b: Int | 
|  | 25 | +  } | 
|  | 26 | +`); | 
|  | 27 | + | 
|  | 28 | +function executeQuery( | 
|  | 29 | +  query: string, | 
|  | 30 | +  rootValue: unknown, | 
|  | 31 | +  variableValues?: { [variable: string]: unknown }, | 
|  | 32 | +): ExecutionResult | Promise<ExecutionResult> { | 
|  | 33 | +  return execute({ schema, document: parse(query), rootValue, variableValues }); | 
|  | 34 | +} | 
|  | 35 | + | 
|  | 36 | +describe('Execute: Handles OneOf Input Objects', () => { | 
|  | 37 | +  describe('OneOf Input Objects', () => { | 
|  | 38 | +    const rootValue = { | 
|  | 39 | +      test({ input }: { input: { a?: string; b?: number } }) { | 
|  | 40 | +        return input; | 
|  | 41 | +      }, | 
|  | 42 | +    }; | 
|  | 43 | + | 
|  | 44 | +    it('accepts a good default value', () => { | 
|  | 45 | +      const query = ` | 
|  | 46 | +        query ($input: TestInputObject! = {a: "abc"}) { | 
|  | 47 | +          test(input: $input) { | 
|  | 48 | +            a | 
|  | 49 | +            b | 
|  | 50 | +          } | 
|  | 51 | +        } | 
|  | 52 | +      `; | 
|  | 53 | +      const result = executeQuery(query, rootValue); | 
|  | 54 | + | 
|  | 55 | +      expectJSON(result).toDeepEqual({ | 
|  | 56 | +        data: { | 
|  | 57 | +          test: { | 
|  | 58 | +            a: 'abc', | 
|  | 59 | +            b: null, | 
|  | 60 | +          }, | 
|  | 61 | +        }, | 
|  | 62 | +      }); | 
|  | 63 | +    }); | 
|  | 64 | + | 
|  | 65 | +    it('rejects a bad default value', () => { | 
|  | 66 | +      const query = ` | 
|  | 67 | +        query ($input: TestInputObject! = {a: "abc", b: 123}) { | 
|  | 68 | +          test(input: $input) { | 
|  | 69 | +            a | 
|  | 70 | +            b | 
|  | 71 | +          } | 
|  | 72 | +        } | 
|  | 73 | +      `; | 
|  | 74 | +      const result = executeQuery(query, rootValue); | 
|  | 75 | + | 
|  | 76 | +      expectJSON(result).toDeepEqual({ | 
|  | 77 | +        data: { | 
|  | 78 | +          test: null, | 
|  | 79 | +        }, | 
|  | 80 | +        errors: [ | 
|  | 81 | +          { | 
|  | 82 | +            locations: [{ column: 23, line: 3 }], | 
|  | 83 | +            message: | 
|  | 84 | +              // This type of error would be caught at validation-time | 
|  | 85 | +              // hence the vague error message here. | 
|  | 86 | +              'Argument "input" of non-null type "TestInputObject!" must not be null.', | 
|  | 87 | +            path: ['test'], | 
|  | 88 | +          }, | 
|  | 89 | +        ], | 
|  | 90 | +      }); | 
|  | 91 | +    }); | 
|  | 92 | + | 
|  | 93 | +    it('accepts a good variable', () => { | 
|  | 94 | +      const query = ` | 
|  | 95 | +        query ($input: TestInputObject!) { | 
|  | 96 | +          test(input: $input) { | 
|  | 97 | +            a | 
|  | 98 | +            b | 
|  | 99 | +          } | 
|  | 100 | +        } | 
|  | 101 | +      `; | 
|  | 102 | +      const result = executeQuery(query, rootValue, { input: { a: 'abc' } }); | 
|  | 103 | + | 
|  | 104 | +      expectJSON(result).toDeepEqual({ | 
|  | 105 | +        data: { | 
|  | 106 | +          test: { | 
|  | 107 | +            a: 'abc', | 
|  | 108 | +            b: null, | 
|  | 109 | +          }, | 
|  | 110 | +        }, | 
|  | 111 | +      }); | 
|  | 112 | +    }); | 
|  | 113 | + | 
|  | 114 | +    it('accepts a good variable with an undefined key', () => { | 
|  | 115 | +      const query = ` | 
|  | 116 | +        query ($input: TestInputObject!) { | 
|  | 117 | +          test(input: $input) { | 
|  | 118 | +            a | 
|  | 119 | +            b | 
|  | 120 | +          } | 
|  | 121 | +        } | 
|  | 122 | +      `; | 
|  | 123 | +      const result = executeQuery(query, rootValue, { | 
|  | 124 | +        input: { a: 'abc', b: undefined }, | 
|  | 125 | +      }); | 
|  | 126 | + | 
|  | 127 | +      expectJSON(result).toDeepEqual({ | 
|  | 128 | +        data: { | 
|  | 129 | +          test: { | 
|  | 130 | +            a: 'abc', | 
|  | 131 | +            b: null, | 
|  | 132 | +          }, | 
|  | 133 | +        }, | 
|  | 134 | +      }); | 
|  | 135 | +    }); | 
|  | 136 | + | 
|  | 137 | +    it('rejects a variable with multiple non-null keys', () => { | 
|  | 138 | +      const query = ` | 
|  | 139 | +        query ($input: TestInputObject!) { | 
|  | 140 | +          test(input: $input) { | 
|  | 141 | +            a | 
|  | 142 | +            b | 
|  | 143 | +          } | 
|  | 144 | +        } | 
|  | 145 | +      `; | 
|  | 146 | +      const result = executeQuery(query, rootValue, { | 
|  | 147 | +        input: { a: 'abc', b: 123 }, | 
|  | 148 | +      }); | 
|  | 149 | + | 
|  | 150 | +      expectJSON(result).toDeepEqual({ | 
|  | 151 | +        errors: [ | 
|  | 152 | +          { | 
|  | 153 | +            locations: [{ column: 16, line: 2 }], | 
|  | 154 | +            message: | 
|  | 155 | +              'Variable "$input" got invalid value { a: "abc", b: 123 }; Exactly one key must be specified for OneOf type "TestInputObject".', | 
|  | 156 | +          }, | 
|  | 157 | +        ], | 
|  | 158 | +      }); | 
|  | 159 | +    }); | 
|  | 160 | + | 
|  | 161 | +    it('rejects a variable with multiple nullable keys', () => { | 
|  | 162 | +      const query = ` | 
|  | 163 | +        query ($input: TestInputObject!) { | 
|  | 164 | +          test(input: $input) { | 
|  | 165 | +            a | 
|  | 166 | +            b | 
|  | 167 | +          } | 
|  | 168 | +        } | 
|  | 169 | +      `; | 
|  | 170 | +      const result = executeQuery(query, rootValue, { | 
|  | 171 | +        input: { a: 'abc', b: null }, | 
|  | 172 | +      }); | 
|  | 173 | + | 
|  | 174 | +      expectJSON(result).toDeepEqual({ | 
|  | 175 | +        errors: [ | 
|  | 176 | +          { | 
|  | 177 | +            locations: [{ column: 16, line: 2 }], | 
|  | 178 | +            message: | 
|  | 179 | +              'Variable "$input" got invalid value { a: "abc", b: null }; Exactly one key must be specified for OneOf type "TestInputObject".', | 
|  | 180 | +          }, | 
|  | 181 | +        ], | 
|  | 182 | +      }); | 
|  | 183 | +    }); | 
|  | 184 | +  }); | 
|  | 185 | +}); | 
0 commit comments