Skip to content

Commit 26d6863

Browse files
committed
Added support of custom directives
1 parent 21b77bd commit 26d6863

21 files changed

+114
-25
lines changed

graphql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/graph-gophers/graphql-go/errors"
11-
"github.com/graph-gophers/graphql-go/internal/common"
1211
"github.com/graph-gophers/graphql-go/internal/exec"
1312
"github.com/graph-gophers/graphql-go/internal/exec/resolvable"
1413
"github.com/graph-gophers/graphql-go/internal/exec/selected"
@@ -17,6 +16,7 @@ import (
1716
"github.com/graph-gophers/graphql-go/internal/validation"
1817
"github.com/graph-gophers/graphql-go/introspection"
1918
"github.com/graph-gophers/graphql-go/log"
19+
"github.com/graph-gophers/graphql-go/pkg/common"
2020
"github.com/graph-gophers/graphql-go/trace"
2121
)
2222

graphql_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
gqlerrors "github.com/graph-gophers/graphql-go/errors"
1212
"github.com/graph-gophers/graphql-go/example/starwars"
1313
"github.com/graph-gophers/graphql-go/gqltesting"
14+
"github.com/graph-gophers/graphql-go/pkg/common"
1415
)
1516

1617
type helloWorldResolver1 struct{}
@@ -45,6 +46,31 @@ func (r *helloSnakeResolver2) SayHello(ctx context.Context, args struct{ FullNam
4546
return "Hello " + args.FullName + "!", nil
4647
}
4748

49+
type customDirectiveResolver struct{}
50+
51+
func (r *customDirectiveResolver) MyTestQuery(ctx context.Context, directives common.DirectiveList) string {
52+
customDirective := directives.Get("customDirective")
53+
customAttribute, _ := customDirective.Args.Get("customAttribute")
54+
55+
return fmt.Sprintf(
56+
"Hello custom directive '%s' with attribute value '%s'!",
57+
customDirective.Name.Name,
58+
customAttribute.String(),
59+
)
60+
}
61+
62+
func (r *customDirectiveResolver) MyTestQueryWithArgs(ctx context.Context, args struct{ Arg1 string }, directives common.DirectiveList) string {
63+
customDirective := directives.Get("customDirective")
64+
customAttribute, _ := customDirective.Args.Get("customAttribute")
65+
66+
return fmt.Sprintf(
67+
"Hello with arg1 '%s' custom directive '%s' with attribute value '%s'!",
68+
args.Arg1,
69+
customDirective.Name.Name,
70+
customAttribute.String(),
71+
)
72+
}
73+
4874
type theNumberResolver struct {
4975
number int32
5076
}
@@ -213,6 +239,63 @@ func TestHelloWorld(t *testing.T) {
213239
})
214240
}
215241

242+
func TestCustomDirective(t *testing.T) {
243+
t.Parallel()
244+
245+
gqltesting.RunTests(t, []*gqltesting.Test{
246+
{
247+
Schema: graphql.MustParseSchema(`
248+
directive @customDirective(
249+
customAttribute: String!
250+
) on FIELD_DEFINITION
251+
252+
schema {
253+
query: Query
254+
}
255+
256+
type Query {
257+
myTestQuery: String! @customDirective(customAttribute: hi)
258+
}
259+
`, &customDirectiveResolver{}),
260+
Query: `
261+
{
262+
myTestQuery
263+
}
264+
`,
265+
ExpectedResult: `
266+
{
267+
"myTestQuery": "Hello custom directive 'customDirective' with attribute value 'hi'!"
268+
}
269+
`,
270+
},
271+
{
272+
Schema: graphql.MustParseSchema(`
273+
directive @customDirective(
274+
customAttribute: String!
275+
) on FIELD_DEFINITION
276+
277+
schema {
278+
query: Query
279+
}
280+
281+
type Query {
282+
myTestQueryWithArgs(arg1: String!): String! @customDirective(customAttribute: hi)
283+
}
284+
`, &customDirectiveResolver{}),
285+
Query: `
286+
{
287+
myTestQueryWithArgs(arg1: "world")
288+
}
289+
`,
290+
ExpectedResult: `
291+
{
292+
"myTestQueryWithArgs": "Hello with arg1 'world' custom directive 'customDirective' with attribute value 'hi'!"
293+
}
294+
`,
295+
},
296+
})
297+
}
298+
216299
func TestHelloSnake(t *testing.T) {
217300
t.Parallel()
218301

internal/exec/exec.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010
"time"
1111

1212
"github.com/graph-gophers/graphql-go/errors"
13-
"github.com/graph-gophers/graphql-go/internal/common"
1413
"github.com/graph-gophers/graphql-go/internal/exec/resolvable"
1514
"github.com/graph-gophers/graphql-go/internal/exec/selected"
1615
"github.com/graph-gophers/graphql-go/internal/query"
1716
"github.com/graph-gophers/graphql-go/internal/schema"
1817
"github.com/graph-gophers/graphql-go/log"
18+
"github.com/graph-gophers/graphql-go/pkg/common"
1919
"github.com/graph-gophers/graphql-go/trace"
2020
)
2121

@@ -207,6 +207,9 @@ func execFieldSelection(ctx context.Context, r *Request, s *resolvable.Schema, f
207207
if f.field.ArgsPacker != nil {
208208
in = append(in, f.field.PackedArgs)
209209
}
210+
if f.field.Directives != nil {
211+
in = append(in, reflect.ValueOf(f.field.Directives))
212+
}
210213
callOut := res.Method(f.field.MethodIndex).Call(in)
211214
result = callOut[0]
212215
if f.field.HasError && !callOut[1].IsNil() {

internal/exec/packer/packer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"strings"
88

99
"github.com/graph-gophers/graphql-go/errors"
10-
"github.com/graph-gophers/graphql-go/internal/common"
1110
"github.com/graph-gophers/graphql-go/internal/schema"
11+
"github.com/graph-gophers/graphql-go/pkg/common"
1212
)
1313

1414
type packer interface {

internal/exec/resolvable/meta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"fmt"
55
"reflect"
66

7-
"github.com/graph-gophers/graphql-go/internal/common"
87
"github.com/graph-gophers/graphql-go/internal/schema"
98
"github.com/graph-gophers/graphql-go/introspection"
9+
"github.com/graph-gophers/graphql-go/pkg/common"
1010
)
1111

1212
// Meta defines the details of the metadata schema for introspection.

internal/exec/resolvable/resolvable.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"reflect"
77
"strings"
88

9-
"github.com/graph-gophers/graphql-go/internal/common"
109
"github.com/graph-gophers/graphql-go/internal/exec/packer"
1110
"github.com/graph-gophers/graphql-go/internal/schema"
11+
"github.com/graph-gophers/graphql-go/pkg/common"
1212
)
1313

1414
type Schema struct {
@@ -327,6 +327,10 @@ func (b *execBuilder) makeFieldExec(typeName string, f *schema.Field, m reflect.
327327
in = in[1:]
328328
}
329329

330+
if len(f.Directives) > 0 && len(in) > 0 {
331+
in = in[1:]
332+
}
333+
330334
if len(in) > 0 {
331335
return nil, fmt.Errorf("too many parameters")
332336
}

internal/exec/selected/selected.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"sync"
77

88
"github.com/graph-gophers/graphql-go/errors"
9-
"github.com/graph-gophers/graphql-go/internal/common"
109
"github.com/graph-gophers/graphql-go/internal/exec/packer"
1110
"github.com/graph-gophers/graphql-go/internal/exec/resolvable"
1211
"github.com/graph-gophers/graphql-go/internal/query"
1312
"github.com/graph-gophers/graphql-go/internal/schema"
1413
"github.com/graph-gophers/graphql-go/introspection"
14+
"github.com/graph-gophers/graphql-go/pkg/common"
1515
)
1616

1717
type Request struct {

internal/exec/subscribe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
"time"
1010

1111
"github.com/graph-gophers/graphql-go/errors"
12-
"github.com/graph-gophers/graphql-go/internal/common"
1312
"github.com/graph-gophers/graphql-go/internal/exec/resolvable"
1413
"github.com/graph-gophers/graphql-go/internal/exec/selected"
1514
"github.com/graph-gophers/graphql-go/internal/query"
15+
"github.com/graph-gophers/graphql-go/pkg/common"
1616
)
1717

1818
type Response struct {

internal/query/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"text/scanner"
66

77
"github.com/graph-gophers/graphql-go/errors"
8-
"github.com/graph-gophers/graphql-go/internal/common"
8+
"github.com/graph-gophers/graphql-go/pkg/common"
99
)
1010

1111
type Document struct {

internal/schema/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"text/scanner"
66

77
"github.com/graph-gophers/graphql-go/errors"
8-
"github.com/graph-gophers/graphql-go/internal/common"
8+
"github.com/graph-gophers/graphql-go/pkg/common"
99
)
1010

1111
// Schema represents a GraphQL service's collective type system capabilities.

internal/schema/schema_internal_test.go

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

66
"github.com/graph-gophers/graphql-go/errors"
7-
"github.com/graph-gophers/graphql-go/internal/common"
7+
"github.com/graph-gophers/graphql-go/pkg/common"
88
)
99

1010
func TestParseInterfaceDef(t *testing.T) {

internal/validation/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"text/scanner"
1010

1111
"github.com/graph-gophers/graphql-go/errors"
12-
"github.com/graph-gophers/graphql-go/internal/common"
1312
"github.com/graph-gophers/graphql-go/internal/query"
1413
"github.com/graph-gophers/graphql-go/internal/schema"
14+
"github.com/graph-gophers/graphql-go/pkg/common"
1515
)
1616

1717
type varSet map[*common.InputValue]struct{}

introspection/introspection.go

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

6-
"github.com/graph-gophers/graphql-go/internal/common"
76
"github.com/graph-gophers/graphql-go/internal/schema"
7+
"github.com/graph-gophers/graphql-go/pkg/common"
88
)
99

1010
type Schema struct {
File renamed without changes.
File renamed without changes.

internal/common/lexer.go renamed to pkg/common/lexer.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ func NewLexer(s string, useStringDescriptions bool) *Lexer {
3030
}
3131
sc.Init(strings.NewReader(s))
3232

33-
3433
l := Lexer{sc: sc, useStringDescriptions: useStringDescriptions}
3534
l.sc.Error = l.CatchScannerError
3635

internal/common/lexer_test.go renamed to pkg/common/lexer_test.go

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

6-
"github.com/graph-gophers/graphql-go/internal/common"
6+
"github.com/graph-gophers/graphql-go/pkg/common"
77
)
88

99
type consumeTestCase struct {
@@ -94,21 +94,21 @@ func TestConsume(t *testing.T) {
9494
}
9595
}
9696

97-
var multilineStringTests = []consumeTestCase {
97+
var multilineStringTests = []consumeTestCase{
9898
{
99-
description: "Oneline strings are okay",
100-
definition: `"Hello World"`,
101-
expected: "",
102-
failureExpected: false,
103-
useStringDescriptions: true,
99+
description: "Oneline strings are okay",
100+
definition: `"Hello World"`,
101+
expected: "",
102+
failureExpected: false,
103+
useStringDescriptions: true,
104104
},
105105
{
106106
description: "Multiline strings are not allowed",
107107
definition: `"Hello
108108
World"`,
109-
expected: `graphql: syntax error: literal not terminated (line 1, column 1)`,
110-
failureExpected: true,
111-
useStringDescriptions: true,
109+
expected: `graphql: syntax error: literal not terminated (line 1, column 1)`,
110+
failureExpected: true,
111+
useStringDescriptions: true,
112112
},
113113
}
114114

@@ -130,5 +130,5 @@ func TestMultilineString(t *testing.T) {
130130
t.Fatalf("Test '%s' failed with error: '%s'", test.description, err.Error())
131131
}
132132
})
133-
}
133+
}
134134
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

subscriptions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66
"reflect"
77

88
qerrors "github.com/graph-gophers/graphql-go/errors"
9-
"github.com/graph-gophers/graphql-go/internal/common"
109
"github.com/graph-gophers/graphql-go/internal/exec"
1110
"github.com/graph-gophers/graphql-go/internal/exec/resolvable"
1211
"github.com/graph-gophers/graphql-go/internal/exec/selected"
1312
"github.com/graph-gophers/graphql-go/internal/query"
1413
"github.com/graph-gophers/graphql-go/internal/validation"
1514
"github.com/graph-gophers/graphql-go/introspection"
15+
"github.com/graph-gophers/graphql-go/pkg/common"
1616
)
1717

1818
// Subscribe returns a response channel for the given subscription with the schema's

0 commit comments

Comments
 (0)