Skip to content

Commit

Permalink
Merge pull request #588 from 99designs/fix-default-scalar-implementat…
Browse files Browse the repository at this point in the history
…ion-regression

Fix default scalar implementation regression
  • Loading branch information
vektah authored Mar 6, 2019
2 parents 737a59a + b27139e commit 8e00703
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 2 deletions.
90 changes: 90 additions & 0 deletions codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func (r *queryResolver) DeprecatedField(ctx context.Context) (string, error) {
func (r *queryResolver) Panics(ctx context.Context) (*Panics, error) {
panic("not implemented")
}
func (r *queryResolver) DefaultScalar(ctx context.Context, arg string) (string, error) {
panic("not implemented")
}
func (r *queryResolver) ValidType(ctx context.Context) (*ValidType, error) {
panic("not implemented")
}
Expand Down
6 changes: 6 additions & 0 deletions codegen/testserver/scalar_default.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extend type Query {
defaultScalar(arg: DefaultScalarImplementation! = "default"): DefaultScalarImplementation!
}

""" This doesnt have an implementation in the typemap, so it should act like a string """
scalar DefaultScalarImplementation
34 changes: 34 additions & 0 deletions codegen/testserver/scalar_default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package testserver

import (
"context"
"net/http/httptest"
"testing"

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/handler"
"github.com/stretchr/testify/require"
)

func TestDefaultScalarImplementation(t *testing.T) {
resolvers := &Stub{}

srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(Config{Resolvers: resolvers})))
c := client.New(srv.URL)

resolvers.QueryResolver.DefaultScalar = func(ctx context.Context, arg string) (i string, e error) {
return arg, nil
}

t.Run("with arg value", func(t *testing.T) {
var resp struct{ DefaultScalar string }
c.MustPost(`query { defaultScalar(arg: "fff") }`, &resp)
require.Equal(t, "fff", resp.DefaultScalar)
})

t.Run("with default value", func(t *testing.T) {
var resp struct{ DefaultScalar string }
c.MustPost(`query { defaultScalar }`, &resp)
require.Equal(t, "default", resp.DefaultScalar)
})
}
4 changes: 4 additions & 0 deletions codegen/testserver/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"go/types"
"sort"

"github.com/99designs/gqlgen/internal/code"

"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/codegen/templates"
"github.com/99designs/gqlgen/internal/code"
"github.com/99designs/gqlgen/plugin"
"github.com/vektah/gqlparser/ast"
)
Expand All @@ -17,6 +16,7 @@ type ModelBuild struct {
Interfaces []*Interface
Models []*Object
Enums []*Enum
Scalars []string
}

type Interface struct {
Expand Down Expand Up @@ -159,6 +159,8 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
}

b.Enums = append(b.Enums, it)
case ast.Scalar:
b.Scalars = append(b.Scalars, schemaType.Name)
}
}

Expand All @@ -175,6 +177,9 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
for _, it := range b.Interfaces {
cfg.Models.Add(it.Name, cfg.Model.ImportPath()+"."+it.Name)
}
for _, it := range b.Scalars {
cfg.Models.Add(it, "github.com/99designs/gqlgen/graphql.String")
}

if len(b.Models) == 0 && len(b.Enums) == 0 {
return nil
Expand Down

0 comments on commit 8e00703

Please sign in to comment.