-
Notifications
You must be signed in to change notification settings - Fork 155
feat: implement static validation for defer/stream #1322
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
Open
ysmolski
wants to merge
11
commits into
feat/defer
Choose a base branch
from
yury/eng-7977-add-validation-rules-for-defer-and-stream
base: feat/defer
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 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
41a05e9
add draft tests
ysmolski 7309a83
add a stream on lists rule
ysmolski 0c59559
add labels checker
ysmolski ed26fd2
rename rules
ysmolski 1dd3a7d
check initialCount
ysmolski 194aaa5
check relative to operation types
ysmolski 6cee126
refine error messages and tests
ysmolski 2197873
reformat tests
ysmolski 12c1e88
implement merging for streams
ysmolski aded752
disable and comment tests for stream ifs
ysmolski 222c084
fix from comments
ysmolski 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 hidden or 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 |
|---|---|---|
|
|
@@ -5,4 +5,5 @@ | |
| .DS_Store | ||
| pkg/parser/testdata/lotto.graphql | ||
| *node_modules* | ||
| *vendor* | ||
| *vendor* | ||
| /.cursorrules | ||
This file contains hidden or 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 hidden or 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
128 changes: 128 additions & 0 deletions
128
v2/pkg/astvalidation/operation_rule_defer_stream_on_root_fields.go
This file contains hidden or 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,128 @@ | ||
| package astvalidation | ||
|
|
||
| import ( | ||
| "bytes" | ||
|
|
||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/ast" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/astvisitor" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/lexer/literal" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/operationreport" | ||
| ) | ||
|
|
||
| // DeferStreamOnValidOperations validates that defer/stream directives are used on valid operations: | ||
| // - Query operations: @defer and @stream are allowed everywhere (root and nested fields) | ||
| // - Mutation operations: @defer and @stream are NOT allowed on root fields, but allowed on nested fields | ||
| // - Subscription operations: @defer and @stream are NOT allowed anywhere (root or nested fields) | ||
| // Directives with if: false are allowed (disabled directives). | ||
| // Directives with if: $variable are allowed (dynamic directives that can't be statically determined). | ||
| func DeferStreamOnValidOperations() Rule { | ||
| return func(walker *astvisitor.Walker) { | ||
| visitor := deferStreamOnValidOpsVisitor{ | ||
| Walker: walker, | ||
| } | ||
| walker.RegisterEnterDocumentVisitor(&visitor) | ||
| walker.RegisterEnterOperationVisitor(&visitor) | ||
| walker.RegisterEnterDirectiveVisitor(&visitor) | ||
| } | ||
| } | ||
|
|
||
| type deferStreamOnValidOpsVisitor struct { | ||
| *astvisitor.Walker | ||
|
|
||
| operation, definition *ast.Document | ||
| currentOperationType ast.OperationType | ||
| } | ||
|
|
||
| func (d *deferStreamOnValidOpsVisitor) EnterDocument(operation, definition *ast.Document) { | ||
| d.operation = operation | ||
| d.definition = definition | ||
| } | ||
|
|
||
| func (d *deferStreamOnValidOpsVisitor) EnterOperationDefinition(ref int) { | ||
| d.currentOperationType = d.operation.OperationDefinitions[ref].OperationType | ||
| } | ||
|
|
||
| func (d *deferStreamOnValidOpsVisitor) EnterDirective(ref int) { | ||
| directiveName := d.operation.DirectiveNameBytes(ref) | ||
|
|
||
| // Only validate @defer and @stream directives | ||
| if !bytes.Equal(directiveName, literal.DEFER) && !bytes.Equal(directiveName, literal.STREAM) { | ||
| return | ||
| } | ||
|
|
||
| if ifValue, hasIf := d.operation.DirectiveArgumentValueByName(ref, literal.IF); hasIf { | ||
| switch ifValue.Kind { | ||
| case ast.ValueKindBoolean: | ||
| // If "if: false", the directive is disabled, so it's allowed | ||
| if !d.operation.BooleanValue(ifValue.Ref) { | ||
| return | ||
| } | ||
| case ast.ValueKindVariable: | ||
| // If if: $variable, we can't statically determine if it's enabled, | ||
| // so we allow it (it might be false at runtime) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| directivePosition := d.operation.Directives[ref].At | ||
|
|
||
| // For subscriptions, @defer and @stream are not allowed anywhere (root or nested) | ||
| if d.currentOperationType == ast.OperationTypeSubscription { | ||
| d.StopWithExternalErr(operationreport.ErrDeferStreamDirectiveNotAllowedOnSubs( | ||
| directiveName, | ||
| directivePosition, | ||
| )) | ||
| return | ||
| } | ||
|
|
||
| // For queries, @defer and @stream are allowed everywhere | ||
| if d.currentOperationType == ast.OperationTypeQuery { | ||
| return | ||
| } | ||
|
|
||
| if len(d.Ancestors) == 0 { | ||
| return | ||
| } | ||
| // The directive's immediate parent (the node it's attached to) | ||
| ancestor := d.Ancestors[len(d.Ancestors)-1] | ||
|
|
||
| // Determine if this is a root level directive | ||
| isRootLevel := false | ||
|
|
||
| switch ancestor.Kind { | ||
| case ast.NodeKindInlineFragment: | ||
| // For inline fragments with @defer, check if it's directly in the operation's selection set | ||
| // At root level, ancestors should be: [OperationDefinition, SelectionSet, InlineFragment] | ||
| // For nested: [OperationDefinition, SelectionSet, Field, ..., SelectionSet, InlineFragment] | ||
| if len(d.Ancestors) == 3 { | ||
| // Check if pattern is [OperationDefinition, SelectionSet, InlineFragment] | ||
| if d.Ancestors[0].Kind == ast.NodeKindOperationDefinition && | ||
| d.Ancestors[1].Kind == ast.NodeKindSelectionSet && | ||
| d.Ancestors[2].Kind == ast.NodeKindInlineFragment { | ||
| isRootLevel = true | ||
| } | ||
| } | ||
| case ast.NodeKindField: | ||
| // For fields with @stream, check if we're directly in the operation's selection set | ||
| // Count how many SelectionSets we've traversed (depth of nesting) | ||
| // A root-level field has only one SelectionSet ancestor (the operation's selection set) | ||
| selectionSetCount := 0 | ||
| for _, a := range d.Ancestors { | ||
| if a.Kind == ast.NodeKindSelectionSet { | ||
| selectionSetCount++ | ||
| } | ||
| } | ||
| // If there's only one SelectionSet in the ancestor chain, we're at root level | ||
| isRootLevel = selectionSetCount == 1 | ||
| } | ||
|
|
||
| // For mutations, @defer and @stream are not allowed on root fields | ||
| if isRootLevel { | ||
| operationTypeName := d.currentOperationType.Name() | ||
| d.StopWithExternalErr(operationreport.ErrDeferStreamDirectiveNotAllowedOnRootField( | ||
| directiveName, | ||
| operationTypeName, | ||
| directivePosition, | ||
| )) | ||
| } | ||
| } | ||
109 changes: 109 additions & 0 deletions
109
v2/pkg/astvalidation/operation_rule_defer_stream_unique_labels.go
This file contains hidden or 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,109 @@ | ||
| package astvalidation | ||
|
|
||
| import ( | ||
| "bytes" | ||
|
|
||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/ast" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/astvisitor" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/lexer/literal" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/lexer/position" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/operationreport" | ||
| ) | ||
|
|
||
| // DeferStreamHaveUniqueLabels validates that defer and stream directive labels are: | ||
| // 1. Unique across all defer and stream directives within an operation | ||
| // 2. Not using variables (must be static string values) | ||
| func DeferStreamHaveUniqueLabels() Rule { | ||
| return func(walker *astvisitor.Walker) { | ||
| visitor := deferStreamLabelsVisitor{ | ||
| Walker: walker, | ||
| } | ||
| walker.RegisterEnterDocumentVisitor(&visitor) | ||
| walker.RegisterEnterOperationVisitor(&visitor) | ||
| walker.RegisterEnterDirectiveVisitor(&visitor) | ||
| } | ||
| } | ||
|
|
||
| type labelPosition struct { | ||
| directiveRef int | ||
| position position.Position | ||
| } | ||
|
|
||
| type deferStreamLabelsVisitor struct { | ||
| *astvisitor.Walker | ||
|
|
||
| operation, definition *ast.Document | ||
|
|
||
| // Track seen labels with their directive refs and positions for duplicate detection. | ||
| seenLabels map[string]labelPosition | ||
| } | ||
|
|
||
| func (d *deferStreamLabelsVisitor) EnterDocument(operation, definition *ast.Document) { | ||
| d.operation = operation | ||
| d.definition = definition | ||
| } | ||
|
|
||
| func (d *deferStreamLabelsVisitor) EnterOperationDefinition(ref int) { | ||
| d.seenLabels = make(map[string]labelPosition) | ||
| } | ||
|
|
||
| func (d *deferStreamLabelsVisitor) EnterDirective(ref int) { | ||
| directiveName := d.operation.DirectiveNameBytes(ref) | ||
|
|
||
| if !bytes.Equal(directiveName, literal.DEFER) && !bytes.Equal(directiveName, literal.STREAM) { | ||
| return | ||
| } | ||
|
|
||
| labelValue, hasLabel := d.operation.DirectiveArgumentValueByName(ref, literal.LABEL) | ||
| if !hasLabel { | ||
| // No label is okay, directives can be used without labels | ||
| return | ||
| } | ||
|
|
||
| directivePosition := d.operation.Directives[ref].At | ||
|
|
||
| // Labels must be static strings, not variables | ||
| if labelValue.Kind == ast.ValueKindVariable { | ||
| d.StopWithExternalErr(operationreport.ErrDeferStreamDirectiveLabelMustBeStatic(directiveName, directivePosition)) | ||
| return | ||
| } | ||
|
|
||
| if labelValue.Kind != ast.ValueKindString { | ||
| // This should be caught by other validation rules, but skip if not a string | ||
| return | ||
| } | ||
|
|
||
| if ifValue, hasIf := d.operation.DirectiveArgumentValueByName(ref, literal.IF); hasIf { | ||
| switch ifValue.Kind { | ||
| case ast.ValueKindBoolean: | ||
| // If "if: false", ignore the directive | ||
| if !d.operation.BooleanValue(ifValue.Ref) { | ||
| return | ||
| } | ||
| case ast.ValueKindVariable: | ||
| // If if: $variable, we can't statically determine if it's enabled, | ||
| // so we ignore this until variable's value is provided. | ||
| return | ||
| } | ||
| } | ||
|
|
||
| labelString := d.operation.StringValueContentString(labelValue.Ref) | ||
|
|
||
| if previous, exists := d.seenLabels[labelString]; exists { | ||
| previousDirectiveName := d.operation.DirectiveNameBytes(previous.directiveRef) | ||
| d.StopWithExternalErr(operationreport.ErrDeferStreamDirectiveLabelMustBeUnique( | ||
| directiveName, | ||
| previousDirectiveName, | ||
| labelString, | ||
| previous.position, | ||
| directivePosition, | ||
| )) | ||
| return | ||
| } | ||
|
|
||
| // Record this label with its position | ||
| d.seenLabels[labelString] = labelPosition{ | ||
| directiveRef: ref, | ||
| position: directivePosition, | ||
| } | ||
ysmolski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.