-
Notifications
You must be signed in to change notification settings - Fork 9
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
Adds validation rule @lookup
should have nullable return type
#55
Open
PascalSenn
wants to merge
3
commits into
graphql:main
Choose a base branch
from
PascalSenn:pse/add-lookup-should-have-nullable-return-type
base: main
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
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,6 +14,65 @@ run in sequence to produce the composite execution schema. | |||||
|
||||||
### Pre Merge Validation | ||||||
|
||||||
#### `@lookup` Should Have Nullable Return Type | ||||||
|
||||||
**Error Code** | ||||||
|
||||||
LOOKUP_SHOULD_HAVE_NULLABLE_RETURN_TYPE | ||||||
|
||||||
**Severity** | ||||||
|
||||||
WARNING | ||||||
|
||||||
**Formal Specification** | ||||||
|
||||||
- Let {fields} be the set of all field definitions annotated with `@lookup` in the schema. | ||||||
- For each {field} in {fields}: | ||||||
- Let {type} be the return type of {field}. | ||||||
- {type} must be a nullable type. | ||||||
|
||||||
**Explanatory Text** | ||||||
|
||||||
Fields annotated with the `@lookup` directive are intended to retrieve a single entity based on provided arguments. | ||||||
To properly handle cases where the requested entity does not exist, such fields should have a nullable return type. | ||||||
This allows the field to return `null` when an entity matching the provided criteria is not found, following the standard GraphQL practices for representing missing data. | ||||||
|
||||||
In a distributed system, it is likely that some entities are not be found on other subgraphs, even when those subgraphs contribute fields to the type. | ||||||
Ensuring that `@lookup` fields have nullable return types also avoids GraphQL errors on subgraphs and prevents result erasure through non-null propagation. | ||||||
By allowing null to be returned when an entity is not found, the system can gracefully handle missing data without causing exceptions or unexpected behavior. | ||||||
|
||||||
Ensuring that `@lookup` fields have nullable return types allows gateways to distinguish between cases where an entity is not found (receiving null) and other error conditions that may have to be propagated to the client. | ||||||
|
||||||
For example, the following usage is recommended: | ||||||
|
||||||
```graphql example | ||||||
extend type Query { | ||||||
userById(id: ID!): User @lookup | ||||||
} | ||||||
|
||||||
type User { | ||||||
id: ID! | ||||||
name: String | ||||||
} | ||||||
``` | ||||||
|
||||||
In this example, `userById` returns a nullable `User` type, aligning with the recommendation. | ||||||
|
||||||
This counter-example demonstrates a invalid usage: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```graphql counter-example | ||||||
extend type Query { | ||||||
userById(id: ID!): User! @lookup | ||||||
} | ||||||
|
||||||
type User { | ||||||
id: ID! | ||||||
name: String | ||||||
} | ||||||
``` | ||||||
|
||||||
Here, `userById` returns a non-nullable `User!`, which does not align with the recommendation that a `@lookup` field should have a nullable return type. | ||||||
|
||||||
### Merge | ||||||
|
||||||
### Post Merge Validation | ||||||
|
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.