-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TT-5460] Removed most validation of entities
[changelog] internal: Removed most validation of entities.
- Loading branch information
David Stutt
committed
Jun 15, 2022
1 parent
bcceac4
commit 6059c34
Showing
12 changed files
with
189 additions
and
591 deletions.
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package sdlmerge | ||
|
||
import ( | ||
"github.com/jensneuse/graphql-go-tools/pkg/ast" | ||
"github.com/jensneuse/graphql-go-tools/pkg/astvisitor" | ||
"github.com/jensneuse/graphql-go-tools/pkg/engine/plan" | ||
"github.com/jensneuse/graphql-go-tools/pkg/operationreport" | ||
) | ||
|
||
type collectEntitiesVisitor struct { | ||
*astvisitor.Walker | ||
document *ast.Document | ||
normalizer *normalizer | ||
} | ||
|
||
func newCollectEntitiesVisitor(n *normalizer) *collectEntitiesVisitor { | ||
return &collectEntitiesVisitor{ | ||
normalizer: n, | ||
} | ||
} | ||
|
||
func (c *collectEntitiesVisitor) Register(walker *astvisitor.Walker) { | ||
c.Walker = walker | ||
walker.RegisterEnterDocumentVisitor(c) | ||
walker.RegisterEnterInterfaceTypeDefinitionVisitor(c) | ||
walker.RegisterEnterObjectTypeDefinitionVisitor(c) | ||
} | ||
|
||
func (c *collectEntitiesVisitor) EnterDocument(operation, _ *ast.Document) { | ||
c.document = operation | ||
} | ||
|
||
func (c *collectEntitiesVisitor) EnterInterfaceTypeDefinition(ref int) { | ||
interfaceType := c.document.InterfaceTypeDefinitions[ref] | ||
if !interfaceType.HasDirectives { | ||
return | ||
} | ||
name := c.document.InterfaceTypeDefinitionNameString(ref) | ||
c.resolveEntity(name, interfaceType.Directives.Refs) | ||
} | ||
|
||
func (c *collectEntitiesVisitor) EnterObjectTypeDefinition(ref int) { | ||
objectType := c.document.ObjectTypeDefinitions[ref] | ||
if !objectType.HasDirectives { | ||
return | ||
} | ||
name := c.document.ObjectTypeDefinitionNameString(ref) | ||
c.resolveEntity(name, objectType.Directives.Refs) | ||
} | ||
|
||
func (c *collectEntitiesVisitor) resolveEntity(name string, directiveRefs []int) { | ||
entitySet := c.normalizer.entitySet | ||
if _, exists := entitySet[name]; exists { | ||
c.Walker.StopWithExternalErr(operationreport.ErrEntitiesMustNotBeDuplicated(name)) | ||
} | ||
for _, directiveRef := range directiveRefs { | ||
if c.document.DirectiveNameString(directiveRef) != plan.FederationKeyDirectiveName { | ||
continue | ||
} | ||
entitySet[name] = true | ||
return | ||
} | ||
} |
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,86 @@ | ||
package sdlmerge | ||
|
||
import ( | ||
"github.com/jensneuse/graphql-go-tools/internal/pkg/unsafeparser" | ||
"github.com/jensneuse/graphql-go-tools/pkg/astvisitor" | ||
"github.com/jensneuse/graphql-go-tools/pkg/operationreport" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestCollectEntities(t *testing.T) { | ||
t.Run("Valid entities are collected", func(t *testing.T) { | ||
collectEntities(t, newCollectEntitiesVisitor(newTestNormalizer(false)), ` | ||
type Dog @key(fields: "name") @key(fields: "id") { | ||
id: ID! | ||
name: String! | ||
} | ||
type Cat @key(fields: "species") { | ||
id: ID! | ||
species: String! | ||
} | ||
`, map[string]bool{ | ||
"Dog": true, | ||
"Cat": true, | ||
}) | ||
}) | ||
|
||
t.Run("Valid entities are collected", func(t *testing.T) { | ||
collectEntitiesAndExpectError(t, newCollectEntitiesVisitor(newTestNormalizer(false)), ` | ||
type Dog @key(fields: "name") @key(fields: "id") { | ||
id: ID! | ||
name: String! | ||
} | ||
type Dog @key(fields: "name") @key(fields: "id") { | ||
id: ID! | ||
name: String! | ||
} | ||
type Cat @key(fields: "species") { | ||
id: ID! | ||
species: String! | ||
} | ||
`, duplicateEntityErrorMessage("Dog")) | ||
}) | ||
} | ||
|
||
var collectEntities = func(t *testing.T, visitor *collectEntitiesVisitor, operation string, expectedEntities map[string]bool) { | ||
operationDocument := unsafeparser.ParseGraphqlDocumentString(operation) | ||
report := operationreport.Report{} | ||
walker := astvisitor.NewWalker(48) | ||
|
||
visitor.Register(&walker) | ||
|
||
walker.Walk(&operationDocument, nil, &report) | ||
|
||
if report.HasErrors() { | ||
t.Fatal(report.Error()) | ||
} | ||
|
||
got := visitor.normalizer.entitySet | ||
|
||
assert.Equal(t, expectedEntities, got) | ||
} | ||
|
||
var collectEntitiesAndExpectError = func(t *testing.T, visitor *collectEntitiesVisitor, operation string, expectedError string) { | ||
operationDocument := unsafeparser.ParseGraphqlDocumentString(operation) | ||
report := operationreport.Report{} | ||
walker := astvisitor.NewWalker(48) | ||
|
||
visitor.Register(&walker) | ||
|
||
walker.Walk(&operationDocument, nil, &report) | ||
|
||
var got string | ||
if report.HasErrors() { | ||
if report.InternalErrors == nil { | ||
got = report.ExternalErrors[0].Message | ||
} else { | ||
got = report.InternalErrors[0].Error() | ||
} | ||
} | ||
|
||
assert.Equal(t, expectedError, got) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.