-
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 all 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
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
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.
I think anything printed with
allowSemanticNullability
, will need to be prefixed with the document directive (@SemanticNullability
) because otherwise the contents are liable to misinterpretation.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.
It's less allow and more just on or off, right? What it's set to changes how a type is interpreted:
Document directives must come at the top of the document, and in my mental model they effectively change the "mode" of the rest of the parsing. You might think of other document directives that might change the mode, e.g.
@commentsAreDescriptions
to re-enable the ancient parsing feature where comments (rather than strings) before fields would become descriptions; or@prefixedDirectives
which might put directives before things rather than after. (I'm not proposing either of these be made real, I'm just trying to show that the space for document directives is not just a set of size 1.)Once you hit your first non-ignored non-document-directive token the mode is then frozen, and you can pass that mode into
parseType
and other parsing related functions.Thus I think it would be more like:
I'm not sure "mode" is really the right name to use for the result of applying document level directives, suggestions welcome. It could be that we just take the document-level directives and store their literal values into the object; for example:
might have a "mode" of:
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.
(Note that this mode should also be an output of parsing, e.g. we might use a document directive in an executable document to enable
@noErrorPropagation
- and we'll need to see that. Originally I thought that@noErrorPropagation
would go on the operation itself rather than the document, but the issue with that is that fragments will have two modes if they're used in two operations with different@noErrorPropagation
settings, so codegen becomes complicated. Instead, making it a document directive means it applies to all operations and fragments in a single document in unison.)