Skip to content

Commit

Permalink
Add a test for aliasing different cases
Browse files Browse the repository at this point in the history
fixes #376
  • Loading branch information
vektah committed Feb 9, 2019
1 parent c2ad3e5 commit 0709925
Show file tree
Hide file tree
Showing 8 changed files with 561 additions and 361 deletions.
696 changes: 423 additions & 273 deletions codegen/testserver/generated.go

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions codegen/testserver/gqlgen.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
schema: schema.graphql
schema:
- "*.graphql"

exec:
filename: generated.go
Expand Down Expand Up @@ -38,6 +39,9 @@ models:
model: "github.com/99designs/gqlgen/codegen/testserver.Error"
EmbeddedPointer:
model: "github.com/99designs/gqlgen/codegen/testserver.EmbeddedPointerModel"
Keywords:
ValidInput:
fields:
_: { fieldName: Underscore }
ValidType:
fields:
different_case: { fieldName: DifferentCaseOld }
36 changes: 22 additions & 14 deletions codegen/testserver/models-gen.go

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

5 changes: 1 addition & 4 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ func (r *queryResolver) NestedInputs(ctx context.Context, input [][]*OuterInput)
func (r *queryResolver) NestedOutputs(ctx context.Context) ([][]*OuterObject, error) {
panic("not implemented")
}
func (r *queryResolver) Keywords(ctx context.Context, input *Keywords) (bool, error) {
panic("not implemented")
}
func (r *queryResolver) Shapes(ctx context.Context) ([]Shape, error) {
panic("not implemented")
}
Expand Down Expand Up @@ -98,7 +95,7 @@ func (r *queryResolver) InputSlice(ctx context.Context, arg []string) (bool, err
func (r *queryResolver) ShapeUnion(ctx context.Context) (ShapeUnion, error) {
panic("not implemented")
}
func (r *queryResolver) KeywordArgs(ctx context.Context, breakArg string, defaultArg string, funcArg string, interfaceArg string, selectArg string, caseArg string, deferArg string, goArg string, mapArg string, structArg string, chanArg string, elseArg string, gotoArg string, packageArg string, switchArg string, constArg string, fallthroughArg string, ifArg string, rangeArg string, typeArg string, continueArg string, forArg string, importArg string, returnArg string, varArg string, _Arg string) (bool, error) {
func (r *queryResolver) ValidType(ctx context.Context) (*ValidType, error) {
panic("not implemented")
}

Expand Down
61 changes: 0 additions & 61 deletions codegen/testserver/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ type Query {
recursive(input: RecursiveInputSlice): Boolean
nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean
nestedOutputs: [[OuterObject]]
keywords(input: Keywords): Boolean!
shapes: [Shape]
errorBubble: Error
modelMethods: ModelMethods
Expand Down Expand Up @@ -88,66 +87,6 @@ type InnerObject {
id: Int!
}

input Keywords {
break: String!
default: String!
func: String!
interface: String!
select: String!
case: String!
defer: String!
go: String!
map: String!
struct: String!
chan: String!
else: String!
goto: String!
package: String!
switch: String!
const: String!
fallthrough: String!
if: String!
range: String!
type: String!
continue: String!
for: String!
import: String!
return: String!
var: String!
_: String!
}

extend type Query {
keywordArgs(
break: String!,
default: String!,
func: String!,
interface: String!,
select: String!,
case: String!,
defer: String!,
go: String!,
map: String!,
struct: String!,
chan: String!,
else: String!,
goto: String!,
package: String!,
switch: String!,
const: String!,
fallthrough: String!,
if: String!,
range: String!,
type: String!,
continue: String!,
for: String!,
import: String!,
return: String!,
var: String!,
_: String!,
): Boolean!
}

interface Shape {
area: Float
}
Expand Down
10 changes: 3 additions & 7 deletions codegen/testserver/stub.go

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

68 changes: 68 additions & 0 deletions codegen/testserver/validtypes.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
extend type Query {
validType: ValidType
}

""" These things are all valid, but without care generate invalid go code """
type ValidType {
differentCase: String!
different_case: String!
validInputKeywords(input: ValidInput): Boolean!
validArgs(
break: String!,
default: String!,
func: String!,
interface: String!,
select: String!,
case: String!,
defer: String!,
go: String!,
map: String!,
struct: String!,
chan: String!,
else: String!,
goto: String!,
package: String!,
switch: String!,
const: String!,
fallthrough: String!,
if: String!,
range: String!,
type: String!,
continue: String!,
for: String!,
import: String!,
return: String!,
var: String!,
_: String!,
): Boolean!
}

input ValidInput {
break: String!
default: String!
func: String!
interface: String!
select: String!
case: String!
defer: String!
go: String!
map: String!
struct: String!
chan: String!
else: String!
goto: String!
package: String!
switch: String!
const: String!
fallthrough: String!
if: String!
range: String!
type: String!
continue: String!
for: String!
import: String!
return: String!
var: String!
_: String!
}

38 changes: 38 additions & 0 deletions codegen/testserver/validtypes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package testserver

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

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

func TestValidType(t *testing.T) {
resolvers := &Stub{}
resolvers.QueryResolver.ValidType = func(ctx context.Context) (validType *ValidType, e error) {
return &ValidType{
DifferentCase: "new",
DifferentCaseOld: "old",
}, nil
}

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

t.Run("fields with differing cases can be distinguished", func(t *testing.T) {
var resp struct {
ValidType struct {
New string `json:"differentCase"`
Old string `json:"different_case"`
}
}
err := c.Post(`query { validType { differentCase, different_case } }`, &resp)
require.NoError(t, err)

require.Equal(t, "new", resp.ValidType.New)
require.Equal(t, "old", resp.ValidType.Old)
})
}

0 comments on commit 0709925

Please sign in to comment.