-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Semantic nullability rfc implementation #4337
Open
JoviDeCroock
wants to merge
10
commits into
16.x.x
Choose a base branch
from
semantic-nullability-rfc-implementation
base: 16.x.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
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 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
17461b9
Implement first version
JoviDeCroock 0f13010
Fix a few tests
JoviDeCroock 869ca46
Propose single wrapping type (#4339)
JoviDeCroock e0c2425
Fix linting
JoviDeCroock 7121006
fixed tests that were using print (#4341)
twof 2321f93
Cleanup and code coverage
JoviDeCroock 4c8a02b
Remove SemanticNonNull from TypeNode
JoviDeCroock ac213fa
Remove unused funcs
JoviDeCroock 1861f71
Be stricter about types
JoviDeCroock 855e4d7
Remove errorPropagation option
JoviDeCroock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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,174 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { GraphQLError } from '../../error/GraphQLError'; | ||
|
||
import type { ExecutableDefinitionNode, FieldNode } from '../../language/ast'; | ||
import { parse } from '../../language/parser'; | ||
|
||
import { | ||
GraphQLNonNull, | ||
GraphQLObjectType, | ||
GraphQLSemanticNonNull, | ||
} from '../../type/definition'; | ||
import { GraphQLString } from '../../type/scalars'; | ||
import { GraphQLSchema } from '../../type/schema'; | ||
|
||
import { execute } from '../execute'; | ||
|
||
describe('Execute: Handles Semantic Nullability', () => { | ||
const DeepDataType = new GraphQLObjectType({ | ||
name: 'DeepDataType', | ||
fields: { | ||
f: { type: new GraphQLNonNull(GraphQLString) }, | ||
}, | ||
}); | ||
|
||
const DataType: GraphQLObjectType = new GraphQLObjectType({ | ||
name: 'DataType', | ||
fields: () => ({ | ||
a: { type: GraphQLString }, | ||
b: { type: new GraphQLSemanticNonNull(GraphQLString) }, | ||
c: { type: new GraphQLNonNull(GraphQLString) }, | ||
d: { type: new GraphQLSemanticNonNull(DeepDataType) }, | ||
}), | ||
}); | ||
|
||
it('SemanticNonNull throws error on null without error', async () => { | ||
const data = { | ||
b: () => null, | ||
}; | ||
|
||
const document = parse(` | ||
query { | ||
b | ||
} | ||
`); | ||
|
||
const result = await execute({ | ||
schema: new GraphQLSchema({ query: DataType }), | ||
document, | ||
rootValue: data, | ||
}); | ||
|
||
const executable = document.definitions[0] as ExecutableDefinitionNode; | ||
const selectionSet = executable.selectionSet.selections[0]; | ||
|
||
expect(result).to.deep.equal({ | ||
data: { | ||
b: null, | ||
}, | ||
errors: [ | ||
new GraphQLError( | ||
'Cannot return null for semantic-non-nullable field DataType.b.', | ||
{ | ||
nodes: selectionSet, | ||
path: ['b'], | ||
}, | ||
), | ||
], | ||
}); | ||
}); | ||
|
||
it('SemanticNonNull succeeds on null with error', async () => { | ||
const data = { | ||
b: () => { | ||
throw new Error('Something went wrong'); | ||
}, | ||
}; | ||
|
||
const document = parse(` | ||
query { | ||
b | ||
} | ||
`); | ||
|
||
const executable = document.definitions[0] as ExecutableDefinitionNode; | ||
const selectionSet = executable.selectionSet.selections[0]; | ||
|
||
const result = await execute({ | ||
schema: new GraphQLSchema({ query: DataType }), | ||
document, | ||
rootValue: data, | ||
}); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { | ||
b: null, | ||
}, | ||
errors: [ | ||
new GraphQLError('Something went wrong', { | ||
nodes: selectionSet, | ||
path: ['b'], | ||
}), | ||
], | ||
}); | ||
}); | ||
|
||
it('SemanticNonNull halts null propagation', async () => { | ||
const deepData = { | ||
f: () => null, | ||
}; | ||
|
||
const data = { | ||
d: () => deepData, | ||
}; | ||
|
||
const document = parse(` | ||
query { | ||
d { | ||
f | ||
} | ||
} | ||
`); | ||
|
||
const result = await execute({ | ||
schema: new GraphQLSchema({ query: DataType }), | ||
document, | ||
rootValue: data, | ||
}); | ||
|
||
const executable = document.definitions[0] as ExecutableDefinitionNode; | ||
const dSelectionSet = executable.selectionSet.selections[0] as FieldNode; | ||
const fSelectionSet = dSelectionSet.selectionSet?.selections[0]; | ||
|
||
expect(result).to.deep.equal({ | ||
data: { | ||
d: null, | ||
}, | ||
errors: [ | ||
new GraphQLError( | ||
'Cannot return null for non-nullable field DeepDataType.f.', | ||
{ | ||
nodes: fSelectionSet, | ||
path: ['d', 'f'], | ||
}, | ||
), | ||
], | ||
}); | ||
}); | ||
|
||
it('SemanticNullable allows non-null values', async () => { | ||
const data = { | ||
a: () => 'Apple', | ||
}; | ||
|
||
const document = parse(` | ||
query { | ||
a | ||
} | ||
`); | ||
|
||
const result = await execute({ | ||
schema: new GraphQLSchema({ query: DataType }), | ||
document, | ||
rootValue: data, | ||
}); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { | ||
a: 'Apple', | ||
}, | ||
}); | ||
}); | ||
}); |
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.
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.
Outstanding TODO. We might want to say something here about how this is being used and include a link to the RFC or spec edits.
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.
I cannot find where this setting is used.
i.e. I would expect something similar for this PR as in #4271 where there is a change within
handleFieldError()
https://github.com/twof/graphql-js/blob/163785d2957d0098e11092482eac8bed765689f3/src/execution/execute.ts#L610If I am correct, I assume there are also missing tests around this behavior, which I think is also missing from #4271 and #4338
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.
In the original PR it's used on line 609 https://github.com/graphql/graphql-js/pull/4192/files#diff-1d705c6a4c73cd3ce46190029e75abd3015b49de3ec250357dcf9b44a35cb7d0R609 exactly as you describe:
graphql-js/src/execution/execute.ts
Lines 607 to 611 in eb9b6c8