Skip to content

Commit

Permalink
add initial test
Browse files Browse the repository at this point in the history
  • Loading branch information
devsergiy committed Oct 11, 2023
1 parent 1e9298a commit 6a824b0
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions v2/pkg/engine/plan/interface_selection_rewriter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package plan

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/wundergraph/graphql-go-tools/v2/internal/pkg/unsafeparser"
"github.com/wundergraph/graphql-go-tools/v2/internal/pkg/unsafeprinter"
"github.com/wundergraph/graphql-go-tools/v2/pkg/ast"
)

func TestInterfaceSelectionRewriter_RewriteOperation(t *testing.T) {
run := func(t *testing.T, definition string, dsConfiguration *DataSourceConfiguration, operation string, expectedOperation string) {
t.Helper()

op := unsafeparser.ParseGraphqlDocumentString(operation)
def := unsafeparser.ParseGraphqlDocumentStringWithBaseSchema(definition)

fieldRef := ast.InvalidRef
for ref, _ := range op.Fields {

Check failure on line 21 in v2/pkg/engine/plan/interface_selection_rewriter_test.go

View workflow job for this annotation

GitHub Actions / Linters

File is not `gofmt`-ed with `-s` (gofmt)
if op.FieldNameString(ref) == "iface" {
fieldRef = ref
break
}
}

node, _ := def.Index.FirstNodeByNameStr("Query")

rewriter := newInterfaceSelectionRewriter(&op, &def)
rewriter.RewriteOperation(fieldRef, node, dsConfiguration)

printedOp := unsafeprinter.PrettyPrint(&op, &def)
expectedPretty := unsafeprinter.Prettify(expectedOperation)

require.Equal(t, expectedPretty, printedOp)
}

t.Run("simple", func(t *testing.T) {
definition := `
interface Node {
id: ID!
name: String!
}
type User implements Node {
id: ID!
name: String!
}
type Admin implements Node {
id: ID!
name: String! @external
}
type Query {
iface: Node!
}
`

operation := `
query {
iface {
name
}
}`

expectedOperation := `
query {
iface {
... on User {
name
}
... on Admin {
name
}
}
}
`

dsConfiguration := dsb().
RootNode("Query", "iface").
RootNode("User", "id", "name").
RootNode("Admin", "id").ds

dsConfiguration.FederationMetaData.Keys = FederationFieldConfigurations{
{
TypeName: "User",
SelectionSet: "id",
},
{
TypeName: "Admin",
SelectionSet: "id",
},
}

run(t, definition, dsConfiguration, operation, expectedOperation)
})
}

0 comments on commit 6a824b0

Please sign in to comment.