Skip to content
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

fix: allow to collect DS information per operation and ensure corrected errors are passed #899

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions v2/pkg/engine/postprocess/postprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postprocess
import (
"github.com/wundergraph/graphql-go-tools/v2/pkg/engine/plan"
"github.com/wundergraph/graphql-go-tools/v2/pkg/engine/resolve"
"slices"
)

type ResponseTreeProcessor interface {
Expand All @@ -16,6 +17,7 @@ type FetchTreeProcessor interface {

type Processor struct {
disableExtractFetches bool
collectDataSourceInfo bool
resolveInputTemplates *resolveInputTemplates
dedupe *deduplicateSingleFetches
processResponseTree []ResponseTreeProcessor
Expand All @@ -30,6 +32,7 @@ type processorOptions struct {
disableExtractFetches bool
disableCreateParallelNodes bool
disableAddMissingNestedDependencies bool
collectDataSourceInfo bool
}

type ProcessorOption func(*processorOptions)
Expand Down Expand Up @@ -59,6 +62,12 @@ func DisableResolveInputTemplates() ProcessorOption {
}
}

func CollectDataSourceInfo() ProcessorOption {
return func(o *processorOptions) {
o.collectDataSourceInfo = true
}
}

func DisableExtractFetches() ProcessorOption {
return func(o *processorOptions) {
o.disableExtractFetches = true
Expand All @@ -83,6 +92,7 @@ func NewProcessor(options ...ProcessorOption) *Processor {
o(opts)
}
return &Processor{
collectDataSourceInfo: opts.collectDataSourceInfo,
disableExtractFetches: opts.disableExtractFetches,
resolveInputTemplates: &resolveInputTemplates{
disable: opts.disableResolveInputTemplates,
Expand Down Expand Up @@ -150,6 +160,15 @@ func (p *Processor) createFetchTree(res *resolve.GraphQLResponse) {
}
fetches := ex.extractFetches(res)
children := make([]*resolve.FetchTreeNode, len(fetches))

if p.collectDataSourceInfo {
var list = make([]resolve.DataSourceInfo, 0, len(fetches))
for _, fetch := range fetches {
list = append(list, fetch.Fetch.DataSourceInfo())
}
res.DataSources = slices.Compact(list)
}

for i := range fetches {
children[i] = &resolve.FetchTreeNode{
Kind: resolve.FetchTreeNodeKindSingle,
Expand Down
276 changes: 275 additions & 1 deletion v2/pkg/engine/postprocess/postprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package postprocess

import (
"fmt"
"github.com/wundergraph/graphql-go-tools/v2/pkg/ast"
"reflect"
"testing"

Expand Down Expand Up @@ -334,7 +335,280 @@ func TestProcess_ExtractFetches(t *testing.T) {
},
}

processor := NewProcessor(DisableDeduplicateSingleFetches(), DisableCreateConcreteSingleFetchTypes(), DisableMergeFields(), DisableCreateParallelNodes(), DisableAddMissingNestedDependencies())
processor := NewProcessor(
DisableDeduplicateSingleFetches(),
DisableCreateConcreteSingleFetchTypes(),
DisableMergeFields(),
DisableCreateParallelNodes(),
DisableAddMissingNestedDependencies(),
)

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := processor.Process(c.pre)

if !assert.Equal(t, c.expected, actual) {
formatterConfig := map[reflect.Type]interface{}{
reflect.TypeOf([]byte{}): func(b []byte) string { return fmt.Sprintf(`"%s"`, string(b)) },
}

prettyCfg := &pretty.Config{
Diffable: true,
IncludeUnexported: false,
Formatter: formatterConfig,
}

if diff := prettyCfg.Compare(c.expected, actual); diff != "" {
t.Errorf("Plan does not match(-want +got)\n%s", diff)
}
}
})
}
}

func TestProcess_ExtractServiceNames(t *testing.T) {
type TestCase struct {
name string
pre plan.Plan
expected plan.Plan
}

cases := []TestCase{
{
name: "Collect all service names",
pre: &plan.SynchronousResponsePlan{
Response: &resolve.GraphQLResponse{
Data: &resolve.Object{
Fields: []*resolve.Field{
{
Name: []byte("field1"),
Value: &resolve.String{
Path: []string{"field1"},
},
},
},
Fetches: []resolve.Fetch{
&resolve.SingleFetch{
Info: &resolve.FetchInfo{
DataSourceID: "user-service",
DataSourceName: "user-service",
OperationType: ast.OperationTypeQuery,
RootFields: []resolve.GraphCoordinate{
{
TypeName: "Query",
FieldName: "user",
},
},
},
FetchDependencies: resolve.FetchDependencies{FetchID: 1},
},
&resolve.SingleFetch{
Info: &resolve.FetchInfo{
DataSourceID: "product-service",
DataSourceName: "product-service",
OperationType: ast.OperationTypeQuery,
RootFields: []resolve.GraphCoordinate{
{
TypeName: "Query",
FieldName: "product",
},
},
},
FetchDependencies: resolve.FetchDependencies{FetchID: 2},
},
&resolve.SingleFetch{
Info: &resolve.FetchInfo{
DataSourceID: "review-service",
DataSourceName: "review-service",
OperationType: ast.OperationTypeQuery,
RootFields: []resolve.GraphCoordinate{
{
TypeName: "Query",
FieldName: "review",
},
},
},
FetchDependencies: resolve.FetchDependencies{FetchID: 3}},
},
},
},
},
expected: &plan.SynchronousResponsePlan{
Response: &resolve.GraphQLResponse{
DataSources: []resolve.DataSourceInfo{
{ID: "user-service", Name: "user-service"},
{ID: "product-service", Name: "product-service"},
{ID: "review-service", Name: "review-service"},
},
Data: &resolve.Object{
Fields: []*resolve.Field{
{
Name: []byte("field1"),
Value: &resolve.String{
Path: []string{"field1"},
},
},
},
},
Fetches: resolve.Sequence(
resolve.Single(&resolve.SingleFetch{
Info: &resolve.FetchInfo{
DataSourceID: "user-service",
DataSourceName: "user-service",
OperationType: ast.OperationTypeQuery,
RootFields: []resolve.GraphCoordinate{
{
TypeName: "Query",
FieldName: "user",
},
},
},
FetchDependencies: resolve.FetchDependencies{FetchID: 1}},
),
resolve.Single(
&resolve.SingleFetch{
Info: &resolve.FetchInfo{
DataSourceID: "product-service",
DataSourceName: "product-service",
OperationType: ast.OperationTypeQuery,
RootFields: []resolve.GraphCoordinate{
{
TypeName: "Query",
FieldName: "product",
},
},
},
FetchDependencies: resolve.FetchDependencies{FetchID: 2},
},
),
resolve.Single(
&resolve.SingleFetch{
Info: &resolve.FetchInfo{
DataSourceID: "review-service",
DataSourceName: "review-service",
OperationType: ast.OperationTypeQuery,
RootFields: []resolve.GraphCoordinate{
{
TypeName: "Query",
FieldName: "review",
},
},
},
FetchDependencies: resolve.FetchDependencies{FetchID: 3},
},
),
),
},
},
},
{
name: "Deduplicate the same service names",
pre: &plan.SynchronousResponsePlan{
Response: &resolve.GraphQLResponse{
Data: &resolve.Object{
Fields: []*resolve.Field{
{
Name: []byte("field1"),
Value: &resolve.String{
Path: []string{"field1"},
},
},
},
Fetches: []resolve.Fetch{
&resolve.SingleFetch{
Info: &resolve.FetchInfo{
DataSourceID: "product-service",
DataSourceName: "product-service",
OperationType: ast.OperationTypeQuery,
RootFields: []resolve.GraphCoordinate{
{
TypeName: "Query",
FieldName: "product",
},
},
},
FetchDependencies: resolve.FetchDependencies{FetchID: 2},
},
&resolve.SingleFetch{
Info: &resolve.FetchInfo{
DataSourceID: "product-service",
DataSourceName: "product-service",
OperationType: ast.OperationTypeQuery,
RootFields: []resolve.GraphCoordinate{
{
TypeName: "Query",
FieldName: "products",
},
},
},
FetchDependencies: resolve.FetchDependencies{FetchID: 2},
},
},
},
},
},
expected: &plan.SynchronousResponsePlan{
Response: &resolve.GraphQLResponse{
DataSources: []resolve.DataSourceInfo{
{ID: "product-service", Name: "product-service"},
},
Data: &resolve.Object{
Fields: []*resolve.Field{
{
Name: []byte("field1"),
Value: &resolve.String{
Path: []string{"field1"},
},
},
},
},
Fetches: resolve.Sequence(
resolve.Single(
&resolve.SingleFetch{
Info: &resolve.FetchInfo{
DataSourceID: "product-service",
DataSourceName: "product-service",
OperationType: ast.OperationTypeQuery,
RootFields: []resolve.GraphCoordinate{
{
TypeName: "Query",
FieldName: "product",
},
},
},
FetchDependencies: resolve.FetchDependencies{FetchID: 2},
},
),
resolve.Single(
&resolve.SingleFetch{
Info: &resolve.FetchInfo{
DataSourceID: "product-service",
DataSourceName: "product-service",
OperationType: ast.OperationTypeQuery,
RootFields: []resolve.GraphCoordinate{
{
TypeName: "Query",
FieldName: "products",
},
},
},
FetchDependencies: resolve.FetchDependencies{FetchID: 2},
},
),
),
},
},
},
}

processor := NewProcessor(
DisableDeduplicateSingleFetches(),
DisableCreateConcreteSingleFetchTypes(),
DisableMergeFields(),
DisableCreateParallelNodes(),
DisableAddMissingNestedDependencies(),
CollectDataSourceInfo(),
)

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
Expand Down
Loading
Loading