Skip to content
This repository was archived by the owner on Apr 1, 2024. It is now read-only.

Commit c7cae73

Browse files
committed
fixed some logic for introspection
1 parent 1fbf355 commit c7cae73

File tree

10 files changed

+92
-55
lines changed

10 files changed

+92
-55
lines changed

internal/gqlgen/gqlgen.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
gql "github.com/99designs/gqlgen/graphql"
1313
"github.com/Code-Hex/gqldoc/internal/graphql"
1414
"github.com/Code-Hex/gqldoc/internal/wrapper"
15-
gqlparser "github.com/gqlgo/gqlparser/v2"
16-
"github.com/gqlgo/gqlparser/v2/ast"
17-
"github.com/gqlgo/gqlparser/v2/parser"
18-
"github.com/gqlgo/gqlparser/v2/validator"
1915
"github.com/pkg/errors"
16+
gqlparser "github.com/vektah/gqlparser/v2"
17+
"github.com/vektah/gqlparser/v2/ast"
18+
"github.com/vektah/gqlparser/v2/parser"
19+
"github.com/vektah/gqlparser/v2/validator"
2020
)
2121

2222
type Params struct {
@@ -237,7 +237,8 @@ func (ec *executionContext) ___Directive(ctx context.Context, sel ast.SelectionS
237237
case "name":
238238
out.Values[i] = gql.MarshalString(obj.Name)
239239
case "description":
240-
out.Values[i] = gql.MarshalString(obj.Description)
240+
res := obj.Description()
241+
out.Values[i] = marshalNullableString(res)
241242
case "locations":
242243
out.Values[i] = ec.marshalArray(ctx, field.Selections, obj.Locations)
243244
case "args":
@@ -263,7 +264,8 @@ func (ec *executionContext) ___EnumValue(ctx context.Context, sel ast.SelectionS
263264
case "name":
264265
out.Values[i] = gql.MarshalString(obj.Name)
265266
case "description":
266-
out.Values[i] = gql.MarshalString(obj.Description)
267+
res := obj.Description()
268+
out.Values[i] = marshalNullableString(res)
267269
case "isDeprecated":
268270
out.Values[i] = gql.MarshalBoolean(obj.IsDeprecated())
269271
case "deprecationReason":
@@ -288,7 +290,8 @@ func (ec *executionContext) ___Field(ctx context.Context, sel ast.SelectionSet,
288290
case "name":
289291
out.Values[i] = gql.MarshalString(obj.Name)
290292
case "description":
291-
out.Values[i] = gql.MarshalString(obj.Description)
293+
res := obj.Description()
294+
out.Values[i] = marshalNullableString(res)
292295
case "args":
293296
out.Values[i] = ec.marshalArray(ctx, field.Selections, obj.Args)
294297
case "type":
@@ -318,7 +321,8 @@ func (ec *executionContext) ___InputValue(ctx context.Context, sel ast.Selection
318321
case "name":
319322
out.Values[i] = gql.MarshalString(obj.Name)
320323
case "description":
321-
out.Values[i] = gql.MarshalString(obj.Description)
324+
res := obj.Description()
325+
out.Values[i] = marshalNullableString(res)
322326
case "type":
323327
out.Values[i] = ec.marshalType(ctx, field.Selections, obj.Type)
324328
case "defaultValue":
@@ -354,7 +358,8 @@ func (ec *executionContext) ___Schema(ctx context.Context, sel ast.SelectionSet,
354358
res := obj.Directives()
355359
out.Values[i] = ec.marshalArray(ctx, field.Selections, res)
356360
case "description":
357-
out.Values[i] = gql.MarshalString(obj.Description())
361+
res := obj.Description()
362+
out.Values[i] = marshalNullableString(res)
358363
default:
359364
panic("unknown field " + strconv.Quote(field.Name))
360365
}
@@ -376,7 +381,8 @@ func (ec *executionContext) ___Type(ctx context.Context, sel ast.SelectionSet, o
376381
case "name":
377382
out.Values[i] = marshalNullableString(obj.Name())
378383
case "description":
379-
out.Values[i] = gql.MarshalString(obj.Description())
384+
res := obj.Description()
385+
out.Values[i] = marshalNullableString(res)
380386
case "fields":
381387
out.Values[i] = ec.___Type_fields(ctx, field, obj)
382388
case "interfaces":
@@ -393,8 +399,8 @@ func (ec *executionContext) ___Type(ctx context.Context, sel ast.SelectionSet, o
393399
case "ofType":
394400
res := obj.OfType()
395401
out.Values[i] = ec.marshalType(ctx, field.Selections, res)
396-
case "specifiedBy":
397-
res := obj.SpecifiedBy()
402+
case "specifiedByURL":
403+
res := obj.SpecifiedByURL()
398404
out.Values[i] = marshalNullableString(res)
399405
default:
400406
panic("unknown field " + strconv.Quote(field.Name))

internal/gqlgen/gqlgen_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"github.com/Code-Hex/gqldoc/loader"
1111
"github.com/goccy/go-yaml"
1212
"github.com/google/go-cmp/cmp"
13-
gqlparser "github.com/gqlgo/gqlparser/v2"
14-
"github.com/gqlgo/gqlparser/v2/ast"
13+
gqlparser "github.com/vektah/gqlparser/v2"
14+
"github.com/vektah/gqlparser/v2/ast"
1515
)
1616

1717
func TestExampleSchema(t *testing.T) {

internal/gqlgen/gqlgen_test.yml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ tests:
306306
]}
307307
}
308308
309-
- name: querying for specifiedBy
309+
- name: querying for specifiedByURL
310310
schema: |
311311
scalar UUID @specifiedBy(url: "https://tools.ietf.org/html/rfc4122")
312312
@@ -319,29 +319,29 @@ tests:
319319
__schema {
320320
types {
321321
name
322-
specifiedBy
322+
specifiedByURL
323323
}
324324
}
325325
}
326326
json: |
327327
{"__schema":
328328
{
329329
"types":[
330-
{"name":"Boolean","specifiedBy":null},
331-
{"name":"Float","specifiedBy":null},
332-
{"name":"ID","specifiedBy":null},
333-
{"name":"Int","specifiedBy":null},
334-
{"name":"Query","specifiedBy":null},
335-
{"name":"String","specifiedBy":null},
336-
{"name":"UUID","specifiedBy":"https://tools.ietf.org/html/rfc4122"},
337-
{"name":"__Directive","specifiedBy":null},
338-
{"name":"__DirectiveLocation","specifiedBy":null},
339-
{"name":"__EnumValue","specifiedBy":null},
340-
{"name":"__Field","specifiedBy":null},
341-
{"name":"__InputValue","specifiedBy":null},
342-
{"name":"__Schema","specifiedBy":null},
343-
{"name":"__Type","specifiedBy":null},
344-
{"name":"__TypeKind","specifiedBy":null}
330+
{"name":"Boolean","specifiedByURL":null},
331+
{"name":"Float","specifiedByURL":null},
332+
{"name":"ID","specifiedByURL":null},
333+
{"name":"Int","specifiedByURL":null},
334+
{"name":"Query","specifiedByURL":null},
335+
{"name":"String","specifiedByURL":null},
336+
{"name":"UUID","specifiedByURL":"https://tools.ietf.org/html/rfc4122"},
337+
{"name":"__Directive","specifiedByURL":null},
338+
{"name":"__DirectiveLocation","specifiedByURL":null},
339+
{"name":"__EnumValue","specifiedByURL":null},
340+
{"name":"__Field","specifiedByURL":null},
341+
{"name":"__InputValue","specifiedByURL":null},
342+
{"name":"__Schema","specifiedByURL":null},
343+
{"name":"__Type","specifiedByURL":null},
344+
{"name":"__TypeKind","specifiedByURL":null}
345345
]
346346
}
347347
}

internal/graphql/context_operation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"errors"
66

7-
"github.com/gqlgo/gqlparser/v2/ast"
7+
"github.com/vektah/gqlparser/v2/ast"
88
)
99

1010
type OperationContext struct {

internal/graphql/executable_schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package graphql
33
import (
44
"fmt"
55

6-
"github.com/gqlgo/gqlparser/v2/ast"
6+
"github.com/vektah/gqlparser/v2/ast"
77
)
88

99
// CollectFields returns the set of fields from an ast.SelectionSet where all collected fields satisfy at least one of the GraphQL types

internal/introspection/introspection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fragment FullType on __Type {
4848
kind
4949
name
5050
description
51-
specifiedBy
51+
specifiedByURL
5252
fields(includeDeprecated: true) {
5353
name
5454
description

internal/wrapper/schema.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"sort"
55
"strings"
66

7-
"github.com/gqlgo/gqlparser/v2/ast"
7+
"github.com/vektah/gqlparser/v2/ast"
88
)
99

1010
type WrapSchemaOption func(s *Schema)
@@ -48,8 +48,8 @@ func (s *Schema) SubscriptionType() *Type {
4848
return WrapTypeFromDef(s.schema, s.schema.Subscription)
4949
}
5050

51-
func (s *Schema) Description() string {
52-
return s.schema.Description
51+
func (s *Schema) Description() *string {
52+
return &s.schema.Description
5353
}
5454

5555
func (s *Schema) Directives() []Directive {
@@ -75,17 +75,17 @@ func (s *Schema) directiveFromDef(d *ast.DirectiveDefinition) Directive {
7575
for i, arg := range d.Arguments {
7676
args[i] = InputValue{
7777
Name: arg.Name,
78-
Description: arg.Description,
7978
DefaultValue: defaultValue(arg.DefaultValue),
8079
Type: WrapTypeFromType(s.schema, arg.Type),
80+
description: arg.Description,
8181
}
8282
}
8383

8484
return Directive{
8585
Name: d.Name,
86-
Description: d.Description,
8786
Locations: locs,
8887
Args: args,
8988
IsRepeatable: d.IsRepeatable,
89+
description: d.Description,
9090
}
9191
}

internal/wrapper/type.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package wrapper
33
import (
44
"strings"
55

6-
"github.com/gqlgo/gqlparser/v2/ast"
6+
"github.com/vektah/gqlparser/v2/ast"
77
)
88

99
type Type struct {
@@ -48,11 +48,11 @@ func (t *Type) Name() *string {
4848
return &t.def.Name
4949
}
5050

51-
func (t *Type) Description() string {
51+
func (t *Type) Description() *string {
5252
if t.def == nil {
53-
return ""
53+
return nil
5454
}
55-
return t.def.Description
55+
return &t.def.Description
5656
}
5757

5858
func (t *Type) Fields(includeDeprecated bool) []Field {
@@ -74,16 +74,16 @@ func (t *Type) Fields(includeDeprecated bool) []Field {
7474
args = append(args, InputValue{
7575
Type: WrapTypeFromType(t.schema, arg.Type),
7676
Name: arg.Name,
77-
Description: arg.Description,
7877
DefaultValue: defaultValue(arg.DefaultValue),
78+
description: arg.Description,
7979
})
8080
}
8181

8282
fields = append(fields, Field{
8383
Name: f.Name,
84-
Description: f.Description,
8584
Args: args,
8685
Type: WrapTypeFromType(t.schema, f.Type),
86+
description: f.Description,
8787
deprecation: f.Directives.ForName("deprecated"),
8888
})
8989
}
@@ -99,9 +99,9 @@ func (t *Type) InputFields() []InputValue {
9999
for _, f := range t.def.Fields {
100100
res = append(res, InputValue{
101101
Name: f.Name,
102-
Description: f.Description,
103102
Type: WrapTypeFromType(t.schema, f.Type),
104103
DefaultValue: defaultValue(f.DefaultValue),
104+
description: f.Description,
105105
})
106106
}
107107
return res
@@ -153,7 +153,7 @@ func (t *Type) EnumValues(includeDeprecated bool) []EnumValue {
153153

154154
res = append(res, EnumValue{
155155
Name: val.Name,
156-
Description: val.Description,
156+
description: val.Description,
157157
deprecation: val.Directives.ForName("deprecated"),
158158
})
159159
}
@@ -174,7 +174,7 @@ func (t *Type) OfType() *Type {
174174
return WrapTypeFromType(t.schema, t.typ.Elem)
175175
}
176176

177-
func (t *Type) SpecifiedBy() *string {
177+
func (t *Type) SpecifiedByURL() *string {
178178
if t.def == nil {
179179
return nil
180180
}

internal/wrapper/type_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package wrapper
33
import (
44
"testing"
55

6-
"github.com/gqlgo/gqlparser/v2/ast"
6+
"github.com/vektah/gqlparser/v2/ast"
77
)
88

99
func TestType(t *testing.T) {
@@ -32,8 +32,11 @@ func TestType(t *testing.T) {
3232

3333
t.Run("description", func(t *testing.T) {
3434
got := schemaType.Description()
35+
if got == nil {
36+
t.Fatal("expect non nil")
37+
}
3538
want := "test description"
36-
if got != want {
39+
if *got != want {
3740
t.Fatalf("want %q but got %q", want, got)
3841
}
3942
})

internal/wrapper/wrapper.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
package wrapper
22

3-
import "github.com/gqlgo/gqlparser/v2/ast"
3+
import "github.com/vektah/gqlparser/v2/ast"
44

55
type (
66
Directive struct {
77
Name string
8-
Description string
98
Locations []string
109
Args []InputValue
1110
IsRepeatable bool
11+
description string
1212
}
1313

1414
EnumValue struct {
1515
Name string
16-
Description string
16+
description string
1717
deprecation *ast.Directive
1818
}
1919

2020
Field struct {
2121
Name string
22-
Description string
2322
Type *Type
2423
Args []InputValue
2524
deprecation *ast.Directive
25+
description string
2626
}
2727

2828
InputValue struct {
2929
Name string
30-
Description string
3130
DefaultValue *string
3231
Type *Type
32+
description string
3333
}
3434
)
3535

@@ -58,6 +58,13 @@ func (f *EnumValue) DeprecationReason() *string {
5858
return &reason.Value.Raw
5959
}
6060

61+
func (f *EnumValue) Description() *string {
62+
if f.description == "" {
63+
return nil
64+
}
65+
return &f.description
66+
}
67+
6168
func (f *Field) IsDeprecated() bool {
6269
return f.deprecation != nil
6370
}
@@ -74,3 +81,24 @@ func (f *Field) DeprecationReason() *string {
7481

7582
return &reason.Value.Raw
7683
}
84+
85+
func (f *Field) Description() *string {
86+
if f.description == "" {
87+
return nil
88+
}
89+
return &f.description
90+
}
91+
92+
func (f *InputValue) Description() *string {
93+
if f.description == "" {
94+
return nil
95+
}
96+
return &f.description
97+
}
98+
99+
func (f *Directive) Description() *string {
100+
if f.description == "" {
101+
return nil
102+
}
103+
return &f.description
104+
}

0 commit comments

Comments
 (0)