From 974437dfa4b96c5e79ae3a6477bbe4c6bd4a6066 Mon Sep 17 00:00:00 2001 From: Roman Odermatt Date: Wed, 18 Jan 2023 09:50:49 +0100 Subject: [PATCH] allow source and schema manipulation --- codegen/generated!.gotpl | 14 +++++++++++++- codegen/input.gotpl | 3 ++- codegen/root_.gotpl | 15 +++++++++++++-- plugin/federation/federation.gotpl | 2 +- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/codegen/generated!.gotpl b/codegen/generated!.gotpl index 647b0e46a6f..a23350d6709 100644 --- a/codegen/generated!.gotpl +++ b/codegen/generated!.gotpl @@ -97,10 +97,22 @@ resolvers ResolverRoot directives DirectiveRoot complexity ComplexityRoot + sources []*ast.Source + afterUnmarshal []func(ctx context.Context, obj interface{}, value interface{}) error } + func (es *executableSchema) afterUnmarshalInput(ctx context.Context, obj interface{}, value interface{}) error { + for _, fn := range es.afterUnmarshal { + if err := fn(ctx, obj, value); err != nil { + return err + } + } + return nil + } + + func (e *executableSchema) Schema() *ast.Schema { - return parsedSchema + return gqlparser.MustLoadSchema(e.sources...) } func (e *executableSchema) Complexity(typeName, field string, childComplexity int, rawArgs map[string]interface{}) (int, bool) { diff --git a/codegen/input.gotpl b/codegen/input.gotpl index 85480d1f28a..e82eacc2592 100644 --- a/codegen/input.gotpl +++ b/codegen/input.gotpl @@ -75,7 +75,8 @@ } } - return {{$it}}, nil + err := ec.afterUnmarshalInput(ctx, &{{$it}}, obj) + return {{$it}}, err } {{- end }} {{ end }} diff --git a/codegen/root_.gotpl b/codegen/root_.gotpl index 5f97a574b01..f0930d8473f 100644 --- a/codegen/root_.gotpl +++ b/codegen/root_.gotpl @@ -20,6 +20,7 @@ func NewExecutableSchema(cfg Config) graphql.ExecutableSchema { resolvers: cfg.Resolvers, directives: cfg.Directives, complexity: cfg.Complexity, + sources: sources, } } @@ -69,10 +70,20 @@ type executableSchema struct { resolvers ResolverRoot directives DirectiveRoot complexity ComplexityRoot + sources []*ast.Source + afterUnmarshal []func(ctx context.Context, obj interface{}, value interface{}) error } +func (es *executableSchema) afterUnmarshalInput(ctx context.Context, obj interface{}, value interface{}) error { + for _, fn := range es.afterUnmarshal { + if err := fn(ctx, obj, value); err != nil { + return err + } + } + return nil +} func (e *executableSchema) Schema() *ast.Schema { - return parsedSchema + return gqlparser.MustLoadSchema(e.sources...) } func (e *executableSchema) Complexity(typeName, field string, childComplexity int, rawArgs map[string]interface{}) (int, bool) { @@ -231,4 +242,4 @@ var sources = []*ast.Source{ {Name: {{$source.RelativePath|quote}}, Input: {{if (not $source.Embeddable)}}{{$source.Source|rawQuote}}{{else}}sourceData({{$source.RelativePath|quote}}){{end}}, BuiltIn: {{$source.BuiltIn}}}, {{- end }} } -var parsedSchema = gqlparser.MustLoadSchema(sources...) +// var parsedSchema = gqlparser.MustLoadSchema(sources...) diff --git a/plugin/federation/federation.gotpl b/plugin/federation/federation.gotpl index 7cf84287eb6..1fc3539c9bb 100644 --- a/plugin/federation/federation.gotpl +++ b/plugin/federation/federation.gotpl @@ -18,7 +18,7 @@ func (ec *executionContext) __resolve__service(ctx context.Context) (fedruntime. var sdl []string - for _, src := range sources { + for _, src := range ec.sources { if src.BuiltIn { continue }