-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backport[v16]: Implement OneOf Input Objects via
@oneOf
directive (#…
…4124) Co-authored-by: Erik Kessler <erik.kessler1@gmail.com> Co-authored-by: Benedikt Franke <benedikt.franke@mll.com> Co-authored-by: Michael Hayes <michael@hayes.io> Co-authored-by: Mike Ciesielka <maciesielka@comcast.net>
- Loading branch information
1 parent
c35130e
commit 29144f7
Showing
28 changed files
with
814 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
import { describe, it } from 'mocha'; | ||
|
||
import { expectJSON } from '../../__testUtils__/expectJSON'; | ||
|
||
import { parse } from '../../language/parser'; | ||
|
||
import { buildSchema } from '../../utilities/buildASTSchema'; | ||
|
||
import type { ExecutionResult } from '../execute'; | ||
import { execute } from '../execute'; | ||
|
||
const schema = buildSchema(` | ||
type Query { | ||
test(input: TestInputObject!): TestObject | ||
} | ||
input TestInputObject @oneOf { | ||
a: String | ||
b: Int | ||
} | ||
type TestObject { | ||
a: String | ||
b: Int | ||
} | ||
`); | ||
|
||
function executeQuery( | ||
query: string, | ||
rootValue: unknown, | ||
variableValues?: { [variable: string]: unknown }, | ||
): ExecutionResult | Promise<ExecutionResult> { | ||
return execute({ schema, document: parse(query), rootValue, variableValues }); | ||
} | ||
|
||
describe('Execute: Handles OneOf Input Objects', () => { | ||
describe('OneOf Input Objects', () => { | ||
const rootValue = { | ||
test({ input }: { input: { a?: string; b?: number } }) { | ||
return input; | ||
}, | ||
}; | ||
|
||
it('accepts a good default value', () => { | ||
const query = ` | ||
query ($input: TestInputObject! = {a: "abc"}) { | ||
test(input: $input) { | ||
a | ||
b | ||
} | ||
} | ||
`; | ||
const result = executeQuery(query, rootValue); | ||
|
||
expectJSON(result).toDeepEqual({ | ||
data: { | ||
test: { | ||
a: 'abc', | ||
b: null, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('rejects a bad default value', () => { | ||
const query = ` | ||
query ($input: TestInputObject! = {a: "abc", b: 123}) { | ||
test(input: $input) { | ||
a | ||
b | ||
} | ||
} | ||
`; | ||
const result = executeQuery(query, rootValue); | ||
|
||
expectJSON(result).toDeepEqual({ | ||
data: { | ||
test: null, | ||
}, | ||
errors: [ | ||
{ | ||
locations: [{ column: 23, line: 3 }], | ||
message: | ||
// This type of error would be caught at validation-time | ||
// hence the vague error message here. | ||
'Argument "input" of non-null type "TestInputObject!" must not be null.', | ||
path: ['test'], | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('accepts a good variable', () => { | ||
const query = ` | ||
query ($input: TestInputObject!) { | ||
test(input: $input) { | ||
a | ||
b | ||
} | ||
} | ||
`; | ||
const result = executeQuery(query, rootValue, { input: { a: 'abc' } }); | ||
|
||
expectJSON(result).toDeepEqual({ | ||
data: { | ||
test: { | ||
a: 'abc', | ||
b: null, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('accepts a good variable with an undefined key', () => { | ||
const query = ` | ||
query ($input: TestInputObject!) { | ||
test(input: $input) { | ||
a | ||
b | ||
} | ||
} | ||
`; | ||
const result = executeQuery(query, rootValue, { | ||
input: { a: 'abc', b: undefined }, | ||
}); | ||
|
||
expectJSON(result).toDeepEqual({ | ||
data: { | ||
test: { | ||
a: 'abc', | ||
b: null, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('rejects a variable with multiple non-null keys', () => { | ||
const query = ` | ||
query ($input: TestInputObject!) { | ||
test(input: $input) { | ||
a | ||
b | ||
} | ||
} | ||
`; | ||
const result = executeQuery(query, rootValue, { | ||
input: { a: 'abc', b: 123 }, | ||
}); | ||
|
||
expectJSON(result).toDeepEqual({ | ||
errors: [ | ||
{ | ||
locations: [{ column: 16, line: 2 }], | ||
message: | ||
'Variable "$input" got invalid value { a: "abc", b: 123 }; Exactly one key must be specified for OneOf type "TestInputObject".', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('rejects a variable with multiple nullable keys', () => { | ||
const query = ` | ||
query ($input: TestInputObject!) { | ||
test(input: $input) { | ||
a | ||
b | ||
} | ||
} | ||
`; | ||
const result = executeQuery(query, rootValue, { | ||
input: { a: 'abc', b: null }, | ||
}); | ||
|
||
expectJSON(result).toDeepEqual({ | ||
errors: [ | ||
{ | ||
locations: [{ column: 16, line: 2 }], | ||
message: | ||
'Variable "$input" got invalid value { a: "abc", b: null }; Exactly one key must be specified for OneOf type "TestInputObject".', | ||
}, | ||
], | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.