File: selection_args_test.go

package info (click to toggle)
golang-github-graph-gophers-graphql-go 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,516 kB
  • sloc: sh: 373; javascript: 21; makefile: 5
file content (181 lines) | stat: -rw-r--r-- 4,259 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package graphql_test

import (
	"context"
	"fmt"
	"testing"

	"github.com/graph-gophers/graphql-go"
)

// Date is a custom scalar implementing decode.Unmarshaler.
type Date struct{ Value string }

func (d *Date) ImplementsGraphQLType(name string) bool { return name == "Date" }
func (d *Date) UnmarshalGraphQL(input any) error {
	s, ok := input.(string)
	if !ok {
		return fmt.Errorf("Date expects string got %T", input)
	}
	d.Value = s
	return nil
}

// harness captures decoded argument structs from inside resolvers.
type harness struct {
	got any
}

type parentResolver struct{}

func (p *parentResolver) ScalarField(ctx context.Context, args struct{ X int32 }) int32 {
	return args.X
}

func (p *parentResolver) StringField(ctx context.Context, args struct{ S string }) string {
	return args.S
}

func (p *parentResolver) EnumField(ctx context.Context, args struct{ Color string }) string {
	return args.Color
}

func (p *parentResolver) CustomField(ctx context.Context, args struct{ D Date }) string {
	return args.D.Value
}

func (p *parentResolver) ComplexField(ctx context.Context, args complexArgs) string {
	return "ok"
}

// decoded argument holder structs
type scalarArgs struct{ X int32 }

type stringArgs struct{ S string }

type enumArgs struct{ Color string }

type customArgs struct{ D Date }

type complexArgs struct {
	R struct {
		Start int32
		End   int32
	}
	Colors []string
}

type queryResolver struct {
	h    *harness
	path string
}

func (q *queryResolver) Parent(ctx context.Context) *parentResolver {
	// decode any child arguments and assign to the harness
	dec := func(path string, dst any) {
		if ok, _ := graphql.DecodeSelectedFieldArgs(ctx, path, dst); ok && q.h.got == nil {
			q.h.got = dst
		}
	}
	switch q.path {
	case "scalarField":
		dec(q.path, &scalarArgs{})
	case "stringField":
		dec(q.path, &stringArgs{})
	case "enumField":
		dec(q.path, &enumArgs{})
	case "customField":
		dec(q.path, &customArgs{})
	case "complexField":
		dec(q.path, &complexArgs{})
	}
	return &parentResolver{}
}

func TestDecodeSelectedFieldArgs(t *testing.T) {
	schemaSDL := `
		scalar Date
		enum Color { RED GREEN BLUE }
		input Range { start: Int! end: Int! }
		type Query { parent: Parent! }
		type Parent {
			scalarField(x: Int!): Int!
			stringField(s: String!): String!
			enumField(color: Color!): String!
			customField(d: Date!): String!
			complexField(r: Range!, colors: [Color!]!): String!
		}
	`

	tests := []struct {
		name   string
		query  string
		path   string
		expect func(t *testing.T, v any)
	}{
		{
			name:  "scalar int",
			query: `query { parent { scalarField(x: 42) } }`,
			path:  "scalarField",
			expect: func(t *testing.T, v any) {
				got := v.(*scalarArgs)
				if got.X != 42 {
					t.Errorf("want 42 got %d", got.X)
				}
			},
		},
		{
			name:  "string",
			query: `query { parent { stringField(s: "abc") } }`,
			path:  "stringField",
			expect: func(t *testing.T, v any) {
				got := v.(*stringArgs)
				if got.S != "abc" {
					t.Errorf("want abc got %s", got.S)
				}
			},
		},
		{
			name:  "custom scalar",
			query: `query { parent { customField(d: "2025-01-02") } }`,
			path:  "customField",
			expect: func(t *testing.T, v any) {
				got := v.(*customArgs)
				if got.D.Value != "2025-01-02" {
					t.Errorf("want date got %s", got.D.Value)
				}
			},
		},
		{
			name:  "complex",
			query: `query { parent { complexField(r: { start: 1, end: 5 }, colors: [GREEN, BLUE]) } }`,
			path:  "complexField",
			expect: func(t *testing.T, v any) {
				got := v.(*complexArgs)
				if got.R.Start != 1 || got.R.End != 5 {
					t.Errorf("range mismatch: %+v", got.R)
				}
				if len(got.Colors) != 2 || got.Colors[0] != "GREEN" || got.Colors[1] != "BLUE" {
					t.Errorf("colors mismatch: %#v", got.Colors)
				}
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			h := &harness{}
			q := &queryResolver{h: h, path: tt.path}
			schema := graphql.MustParseSchema(schemaSDL, q)
			res := schema.Exec(context.Background(), tt.query, "", nil)
			if len(res.Errors) > 0 {
				t.Fatalf("unexpected errors: %+v", res.Errors)
			}
			if h.got == nil {
				t.Errorf("resolver did not capture decoded args (path %s)", tt.path)
				return
			}
			tt.expect(t, h.got)
		})
	}
}