forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce Oneof behavior at execution time #3
Open
erikkessler1
wants to merge
3
commits into
oneof-validation
Choose a base branch
from
oneof-execution
base: oneof-validation
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,6 @@ words: | |
- graphiql | ||
- sublinks | ||
- instanceof | ||
- oneof | ||
|
||
# Different names used inside tests | ||
- Skywalker | ||
|
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,273 @@ | ||
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 @oneOf { | ||
a: String | ||
b: Int | ||
} | ||
|
||
schema { | ||
query: Query | ||
} | ||
`); | ||
|
||
function executeQuery( | ||
query: string, | ||
rootValue: unknown, | ||
variableValues?: { [variable: string]: unknown }, | ||
): ExecutionResult | Promise<ExecutionResult> { | ||
return execute({ schema, document: parse(query), rootValue, variableValues }); | ||
} | ||
|
||
async function executeQueryAsync( | ||
query: string, | ||
rootValue: unknown, | ||
variableValues?: { [variable: string]: unknown }, | ||
): Promise<ExecutionResult> { | ||
const result = await execute({ | ||
schema, | ||
document: parse(query), | ||
rootValue, | ||
variableValues, | ||
}); | ||
return result; | ||
} | ||
|
||
describe('Execute: Handles OneOf Input Objects and OneOf 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: | ||
'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('rejects a bad variable', () => { | ||
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.', | ||
}, | ||
], | ||
}); | ||
}); | ||
}); | ||
|
||
describe('OneOf Objects', () => { | ||
const query = ` | ||
query ($input: TestInputObject! = {a: "abc"}) { | ||
test(input: $input) { | ||
a | ||
b | ||
} | ||
} | ||
`; | ||
|
||
it('works with a single, non-null value', () => { | ||
const rootValue = { | ||
test: { | ||
a: null, | ||
b: 123, | ||
}, | ||
}; | ||
const result = executeQuery(query, rootValue); | ||
|
||
expectJSON(result).toDeepEqual({ | ||
data: { | ||
test: { | ||
a: null, | ||
b: 123, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('works with a single, non-null, async value', async () => { | ||
const rootValue = { | ||
test() { | ||
return { | ||
a: null, | ||
b: () => new Promise((resolve) => resolve(123)), | ||
}; | ||
}, | ||
}; | ||
const result = await executeQueryAsync(query, rootValue); | ||
|
||
expectJSON(result).toDeepEqual({ | ||
data: { | ||
test: { | ||
a: null, | ||
b: 123, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('errors when there are no non-null values', () => { | ||
const rootValue = { | ||
test: { | ||
a: null, | ||
b: null, | ||
}, | ||
}; | ||
const result = executeQuery(query, rootValue); | ||
|
||
expectJSON(result).toDeepEqual({ | ||
data: { test: null }, | ||
errors: [ | ||
{ | ||
locations: [{ column: 11, line: 3 }], | ||
message: | ||
'OneOf Object "TestObject" must have exactly one non-null field but got 0.', | ||
path: ['test'], | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('errors when there are multiple non-null values', () => { | ||
const rootValue = { | ||
test: { | ||
a: 'abc', | ||
b: 456, | ||
}, | ||
}; | ||
const result = executeQuery(query, rootValue); | ||
|
||
expectJSON(result).toDeepEqual({ | ||
data: { test: null }, | ||
errors: [ | ||
{ | ||
locations: [{ column: 11, line: 3 }], | ||
message: | ||
'OneOf Object "TestObject" must have exactly one non-null field but got 2.', | ||
path: ['test'], | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('errors when there are multiple non-null, async values', async () => { | ||
const rootValue = { | ||
test() { | ||
return { | ||
a: 'abc', | ||
b: () => new Promise((resolve) => resolve(123)), | ||
}; | ||
}, | ||
}; | ||
const result = await executeQueryAsync(query, rootValue); | ||
|
||
expectJSON(result).toDeepEqual({ | ||
data: { test: null }, | ||
errors: [ | ||
{ | ||
locations: [{ column: 11, line: 3 }], | ||
message: | ||
'OneOf Object "TestObject" must have exactly one non-null field but got 2.', | ||
path: ['test'], | ||
}, | ||
], | ||
}); | ||
}); | ||
}); | ||
}); |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we also need test coverage for field argument defaults and input object input field defaults that use oneOf input objects?
EDIT: I guess the validation for those scenarios is still pending based on graphql#3049.