diff --git a/.github/workflows/check-coverage b/.github/workflows/check-coverage index 01662a8b9e1..9244270d602 100755 --- a/.github/workflows/check-coverage +++ b/.github/workflows/check-coverage @@ -7,4 +7,4 @@ go test -covermode atomic -coverprofile=/tmp/coverage.out.tmp -coverpkg=./... $( # ignore protobuf files cat /tmp/coverage.out.tmp | grep -v ".pb.go" > /tmp/coverage.out -goveralls -coverprofile=/tmp/coverage.out -service=github -ignore='_examples/*/*,_examples/*/*/*,integration/*,integration/*/*,codegen/testserver/*/*,plugin/federation/testdata/*/*/*,*/generated.go,*/*/generated.go,*/*/*/generated.go,graphql/executable_schema_mock.go' +goveralls -coverprofile=/tmp/coverage.out -service=github -ignore='_examples/*/*,_examples/*/*/*,integration/*,integration/*/*,codegen/testserver/*/*,plugin/resolvergen/testdata/*/*,plugin/modelgen/*/*,plugin/federation/testdata/*/*/*,*/generated.go,*/*/generated.go,*/*/*/generated.go,graphql/executable_schema_mock.go' diff --git a/_examples/federation/subgraphs/subgraphs.go b/_examples/federation/subgraphs/subgraphs.go index b60ae564c6c..b756dd55d68 100644 --- a/_examples/federation/subgraphs/subgraphs.go +++ b/_examples/federation/subgraphs/subgraphs.go @@ -7,11 +7,12 @@ import ( "log" "net/http" + "golang.org/x/sync/errgroup" + "github.com/99designs/gqlgen/graphql" "github.com/99designs/gqlgen/graphql/handler" "github.com/99designs/gqlgen/graphql/handler/debug" "github.com/99designs/gqlgen/graphql/playground" - "golang.org/x/sync/errgroup" ) type Config struct { diff --git a/client/readme.md b/client/readme.md index 755a143315f..b4043a4deb9 100644 --- a/client/readme.md +++ b/client/readme.md @@ -1,5 +1,8 @@ This client is used internally for testing. I wanted a simple graphql client sent user specified queries. You might want to look at: - - https://github.com/shurcooL/graphql: Uses reflection to build queries from structs. + - https://github.com/shurcooL/graphql: Uses reflection to build queries from structs. - https://github.com/machinebox/graphql: Probably would have been a perfect fit, but it uses form encoding instead of json... + - [Khan/genqlient](https://github.com/Khan/genqlient) - Generate go GraphQL client from GraphQL query + - [infiotinc/gqlgenc](https://github.com/infiotinc/gqlgenc) - Generate go GraphQL client from GraphQL query + - [Yamashou/gqlgenc](https://github.com/Yamashou/gqlgenc) - Generate go GraphQL client from GraphQL query diff --git a/codegen/config/resolver.go b/codegen/config/resolver.go index 05bdee0863e..fa0744e1086 100644 --- a/codegen/config/resolver.go +++ b/codegen/config/resolver.go @@ -56,9 +56,6 @@ func (r *ResolverConfig) Check() error { } else { r.Filename = abs(r.Filename) } - if r.PreserveResolver { - return fmt.Errorf("preserve_resolver=true cannot be used with layout=%s", r.Layout) - } default: return fmt.Errorf("invalid layout %s. must be %s or %s", r.Layout, LayoutSingleFile, LayoutFollowSchema) } diff --git a/docs/content/config.md b/docs/content/config.md index e0050a46c83..551faf7dec2 100644 --- a/docs/content/config.md +++ b/docs/content/config.md @@ -46,6 +46,8 @@ resolver: # omit_template_comment: false # Optional: Pass in a path to a new gotpl template to use for generating resolvers # resolver_template: [your/path/resolver.gotpl] + # Optional: turn on to avoid rewriting existing resolver(s) when generating + # preserve_resolver: false # Optional: turn on use ` + "`" + `gqlgen:"fieldName"` + "`" + ` tags in your models # struct_tag: json diff --git a/plugin/federation/constants.go b/plugin/federation/constants.go index 8571db1f8a1..2427b22c856 100644 --- a/plugin/federation/constants.go +++ b/plugin/federation/constants.go @@ -1,8 +1,9 @@ package federation import ( - "github.com/99designs/gqlgen/codegen/config" "github.com/vektah/gqlparser/v2/ast" + + "github.com/99designs/gqlgen/codegen/config" ) // The name of the field argument that is injected into the resolver to support @requires. diff --git a/plugin/resolvergen/resolver.go b/plugin/resolvergen/resolver.go index 83717f961ee..c090c1b30a5 100644 --- a/plugin/resolvergen/resolver.go +++ b/plugin/resolvergen/resolver.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "go/ast" - "io/fs" "os" "path/filepath" "strings" @@ -45,6 +44,7 @@ func (m *Plugin) GenerateCode(data *codegen.Data) error { case config.LayoutSingleFile: return m.generateSingleFile(data) case config.LayoutFollowSchema: + return m.generatePerSchema(data) } @@ -54,10 +54,10 @@ func (m *Plugin) GenerateCode(data *codegen.Data) error { func (m *Plugin) generateSingleFile(data *codegen.Data) error { file := File{} - if _, err := os.Stat(data.Config.Resolver.Filename); err == nil && + if fileExists(data.Config.Resolver.Filename) && data.Config.Resolver.PreserveResolver { // file already exists and config says not to update resolver - // with layout = single so just return + // so just return return nil } @@ -93,7 +93,7 @@ func (m *Plugin) generateSingleFile(data *codegen.Data) error { } } - if _, err := os.Stat(data.Config.Resolver.Filename); err == nil { + if fileExists(data.Config.Resolver.Filename) { file.name = data.Config.Resolver.Filename file.imports = rewriter.ExistingImports(file.name) file.RemainingSource = rewriter.RemainingSource(file.name) @@ -196,6 +196,11 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error { } for _, file := range files { + if fileExists(file.name) && + data.Config.Resolver.PreserveResolver { + // file already exists and config says not to update resolver + continue + } resolverBuild := &ResolverBuild{ File: file, PackageName: data.Config.Resolver.Package, @@ -229,7 +234,7 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error { } } - if _, err := os.Stat(data.Config.Resolver.Filename); errors.Is(err, fs.ErrNotExist) { + if !fileExists(data.Config.Resolver.Filename) { err := templates.Render(templates.Options{ PackageName: data.Config.Resolver.Package, FileNotice: ` @@ -317,3 +322,10 @@ func readResolverTemplate(customResolverTemplate string) string { } return string(contentBytes) } + +func fileExists(fileName string) bool { + if _, err := os.Stat(fileName); err == nil { + return true + } + return false +} diff --git a/plugin/resolvergen/resolver_test.go b/plugin/resolvergen/resolver_test.go index bfb107b99a4..659f537b3f8 100644 --- a/plugin/resolvergen/resolver_test.go +++ b/plugin/resolvergen/resolver_test.go @@ -157,7 +157,7 @@ func overWriteFile(t *testing.T, sourceFile, destinationFile string) { input, err := os.ReadFile(sourceFile) require.NoError(t, err) - err = os.WriteFile(destinationFile, input, 0644) + err = os.WriteFile(destinationFile, input, 0o644) require.NoError(t, err) }