Skip to content

Commit

Permalink
Allow preserve_resolver config to work for layout follow schema
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Coffman <steve@khanacademy.org>
  • Loading branch information
StevenACoffman committed Nov 9, 2024
1 parent 512c814 commit 94db9b8
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 13 deletions.
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

0 comments on commit 94db9b8

Please sign in to comment.