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

Allow preserve_resolver config to work for layout follow schema #3361

Merged
merged 1 commit into from
Nov 10, 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
2 changes: 1 addition & 1 deletion .github/workflows/check-coverage
Original file line number Diff line number Diff line change
Expand Up @@ -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'
3 changes: 2 additions & 1 deletion _examples/federation/subgraphs/subgraphs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion client/readme.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions codegen/config/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion plugin/federation/constants.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
22 changes: 17 additions & 5 deletions plugin/resolvergen/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"go/ast"
"io/fs"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: `
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion plugin/resolvergen/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Loading