Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	plugin/modelgen/models.go
  • Loading branch information
tprebs committed Oct 12, 2021
2 parents 45fa3ce + 393f755 commit 2a7c232
Show file tree
Hide file tree
Showing 38 changed files with 802 additions and 58 deletions.
1 change: 1 addition & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type (
Query string `json:"query"`
Variables map[string]interface{} `json:"variables,omitempty"`
OperationName string `json:"operationName,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
HTTP *http.Request `json:"-"`
}

Expand Down
27 changes: 27 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,30 @@ func TestAddCookie(t *testing.T) {
client.AddCookie(&http.Cookie{Name: "foo", Value: "value"}),
)
}

func TestAddExtensions(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
require.Equal(t, `{"query":"user(id:1){name}","extensions":{"persistedQuery":{"sha256Hash":"ceec2897e2da519612279e63f24658c3e91194cbb2974744fa9007a7e1e9f9e7","version":1}}}`, string(b))
err = json.NewEncoder(w).Encode(map[string]interface{}{
"data": map[string]interface{}{
"Name": "Bob",
},
})
if err != nil {
panic(err)
}
})

c := client.New(h)

var resp struct {
Name string
}
c.MustPost("user(id:1){name}", &resp,
client.Extensions(map[string]interface{}{"persistedQuery": map[string]interface{}{"version": 1, "sha256Hash": "ceec2897e2da519612279e63f24658c3e91194cbb2974744fa9007a7e1e9f9e7"}}),
)
}
7 changes: 7 additions & 0 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func Operation(name string) Option {
}
}

// Extensions sets the extensions to be sent with the outgoing request
func Extensions(extensions map[string]interface{}) Option {
return func(bd *Request) {
bd.Extensions = extensions
}
}

// Path sets the url that this request will be made against, useful if you are mounting your entire router
// and need to specify the url to the graphql endpoint.
func Path(url string) Option {
Expand Down
36 changes: 29 additions & 7 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ func (c *Config) injectTypesFromSchema() error {
SkipRuntime: true,
}

c.Directives["extraTag"] = DirectiveConfig{
SkipRuntime: true,
}

for _, schemaType := range c.Schema.Types {
if schemaType == c.Schema.Query || schemaType == c.Schema.Mutation || schemaType == c.Schema.Subscription {
continue
Expand All @@ -253,9 +257,15 @@ func (c *Config) injectTypesFromSchema() error {

if schemaType.Kind == ast.Object || schemaType.Kind == ast.InputObject {
for _, field := range schemaType.Fields {
typeMapField := TypeMapField{
ExtraTag: c.Models[schemaType.Name].Fields[field.Name].ExtraTag,
FieldName: c.Models[schemaType.Name].Fields[field.Name].FieldName,
Resolver: c.Models[schemaType.Name].Fields[field.Name].Resolver,
}
directive := false
if fd := field.Directives.ForName("goField"); fd != nil {
forceResolver := c.Models[schemaType.Name].Fields[field.Name].Resolver
fieldName := c.Models[schemaType.Name].Fields[field.Name].FieldName
forceResolver := typeMapField.Resolver
fieldName := typeMapField.FieldName

if ra := fd.Arguments.ForName("forceResolver"); ra != nil {
if fr, err := ra.Value.Value(nil); err == nil {
Expand All @@ -269,17 +279,28 @@ func (c *Config) injectTypesFromSchema() error {
}
}

typeMapField.FieldName = fieldName
typeMapField.Resolver = forceResolver
directive = true
}

if ex := field.Directives.ForName("extraTag"); ex != nil {
args := []string{}
for _, arg := range ex.Arguments {
args = append(args, arg.Name+`:"`+arg.Value.Raw+`"`)
}
typeMapField.ExtraTag = strings.Join(args, " ")
directive = true
}

if directive {
if c.Models[schemaType.Name].Fields == nil {
c.Models[schemaType.Name] = TypeMapEntry{
Model: c.Models[schemaType.Name].Model,
Fields: map[string]TypeMapField{},
}
}

c.Models[schemaType.Name].Fields[field.Name] = TypeMapField{
FieldName: fieldName,
Resolver: forceResolver,
}
c.Models[schemaType.Name].Fields[field.Name] = typeMapField
}
}
}
Expand All @@ -296,6 +317,7 @@ type TypeMapEntry struct {
type TypeMapField struct {
Resolver bool `yaml:"resolver"`
FieldName string `yaml:"fieldName"`
ExtraTag string `yaml:"extraTag"`
GeneratedMethod string `yaml:"-"`
}

Expand Down
2 changes: 1 addition & 1 deletion codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func (f *Field) GoNameUnexported() string {
}

func (f *Field) ShortInvocation() string {
return fmt.Sprintf("%s().%s(%s)", f.Object.Definition.Name, f.GoFieldName, f.CallArgs())
return fmt.Sprintf("%s().%s(%s)", strings.Title(f.Object.Definition.Name), f.GoFieldName, f.CallArgs())
}

func (f *Field) ArgsFunc() string {
Expand Down
6 changes: 3 additions & 3 deletions codegen/generated!.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Config struct {
type ResolverRoot interface {
{{- range $object := .Objects -}}
{{ if $object.HasResolvers -}}
{{$object.Name}}() {{$object.Name}}Resolver
{{ucFirst $object.Name}}() {{ucFirst $object.Name}}Resolver
{{ end }}
{{- end }}
}
Expand All @@ -46,7 +46,7 @@ type DirectiveRoot struct {
type ComplexityRoot struct {
{{ range $object := .Objects }}
{{ if not $object.IsReserved -}}
{{ $object.Name|go }} struct {
{{ucFirst $object.Name|go }} struct {
{{ range $_, $fields := $object.UniqueFields }}
{{- $field := index $fields 0 -}}
{{ if not $field.IsReserved -}}
Expand All @@ -60,7 +60,7 @@ type ComplexityRoot struct {

{{ range $object := .Objects -}}
{{ if $object.HasResolvers }}
type {{$object.Name}}Resolver interface {
type {{ucFirst $object.Name}}Resolver interface {
{{ range $field := $object.Fields -}}
{{- if $field.IsResolver }}
{{- $field.GoFieldName}}{{ $field.ShortResolverDeclaration }}
Expand Down
2 changes: 1 addition & 1 deletion codegen/input.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
asMap[k] = v
}
{{ range $field := .Fields}}
{{- if $field.Default}}
{{- if notNil "Default" $field }}
if _, present := asMap[{{$field.Name|quote}}] ; !present {
asMap[{{$field.Name|quote}}] = {{ $field.Default | dump }}
}
Expand Down
2 changes: 1 addition & 1 deletion codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (b *builder) buildObject(typ *ast.Definition) (*Object, error) {
Stream: typ == b.Schema.Subscription,
Directives: dirs,
ResolverInterface: types.NewNamed(
types.NewTypeName(0, b.Config.Exec.Pkg(), typ.Name+"Resolver", nil),
types.NewTypeName(0, b.Config.Exec.Pkg(), strings.Title(typ.Name)+"Resolver", nil),
nil,
nil,
),
Expand Down
5 changes: 4 additions & 1 deletion codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,26 +458,29 @@ var commonInitialisms = map[string]bool{
"ID": true,
"IP": true,
"JSON": true,
"KVK": true,
"LHS": true,
"PGP": true,
"QPS": true,
"QR": true,
"RAM": true,
"RHS": true,
"RPC": true,
"SLA": true,
"SMTP": true,
"SQL": true,
"SSH": true,
"SVG": true,
"TCP": true,
"TLS": true,
"TTL": true,
"UDP": true,
"UI": true,
"UID": true,
"UUID": true,
"URI": true,
"URL": true,
"UTF8": true,
"UUID": true,
"VM": true,
"XML": true,
"XMPP": true,
Expand Down
20 changes: 20 additions & 0 deletions codegen/testserver/defaults.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extend type Query {
defaultParameters(
falsyBoolean: Boolean = false
truthyBoolean: Boolean = true
): DefaultParametersMirror!
}

extend type Mutation {
defaultInput(input: DefaultInput!): DefaultParametersMirror!
}

input DefaultInput {
falsyBoolean: Boolean = false
truthyBoolean: Boolean = true
}

type DefaultParametersMirror {
falsyBoolean: Boolean
truthyBoolean: Boolean
}
68 changes: 68 additions & 0 deletions codegen/testserver/defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package testserver

import (
"context"
"testing"

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

func assertDefaults(t *testing.T, ret *DefaultParametersMirror) {
require.NotNil(t, ret)
require.NotNil(t, ret.FalsyBoolean)
require.Equal(t, *ret.FalsyBoolean, false)
require.NotNil(t, ret.TruthyBoolean)
require.Equal(t, *ret.TruthyBoolean, true)
}

func TestDefaults(t *testing.T) {
resolvers := &Stub{}
srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))
c := client.New(srv)

t.Run("default field parameters", func(t *testing.T) {
resolvers.QueryResolver.DefaultParameters = func(
ctx context.Context,
falsyBoolean, truthyBoolean *bool,
) (*DefaultParametersMirror, error) {
return &DefaultParametersMirror{
FalsyBoolean: falsyBoolean,
TruthyBoolean: truthyBoolean,
}, nil
}

var resp struct{ DefaultParameters *DefaultParametersMirror }
err := c.Post(`query {
defaultParameters {
falsyBoolean
truthyBoolean
}
}`, &resp)
require.NoError(t, err)
assertDefaults(t, resp.DefaultParameters)
})

t.Run("default input fields", func(t *testing.T) {
resolvers.MutationResolver.DefaultInput = func(
ctx context.Context,
input DefaultInput,
) (*DefaultParametersMirror, error) {
return &DefaultParametersMirror{
FalsyBoolean: input.FalsyBoolean,
TruthyBoolean: input.TruthyBoolean,
}, nil
}

var resp struct{ DefaultInput *DefaultParametersMirror }
err := c.Post(`mutation {
defaultInput(input: {}) {
falsyBoolean
truthyBoolean
}
}`, &resp)
require.NoError(t, err)
assertDefaults(t, resp.DefaultInput)
})
}
Loading

0 comments on commit 2a7c232

Please sign in to comment.