forked from ravilushqa/otelgqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gqlgen_test.go
617 lines (512 loc) · 19.7 KB
/
gqlgen_test.go
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
// Copyright Ravil Galaktionov
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package otelgqlgen
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/stretchr/testify/assert"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
)
const (
testQueryName = "NamedQuery"
namelessQueryName = "nameless-operation"
testComplexity = 5
)
func TestChildSpanFromGlobalTracer(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if !span.IsValid() {
t.Fatalf("invalid span wrapping handler: %#v", span)
}
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
srv.Use(Middleware())
r := httptest.NewRequest("GET", "/foo?query={name}", nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
testSpans(t, spanRecorder, namelessQueryName, codes.Unset)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
}
func TestExecutedOperationNameAsSpanNameWithOperationNameParameter(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if !span.IsValid() {
t.Fatalf("invalid span wrapping handler: %#v", span)
}
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
srv.Use(Middleware())
query := url.QueryEscape("query A {__typename} query B {__typename} query C {__typename}")
r := httptest.NewRequest("GET", fmt.Sprintf("/foo?operationName=C&query=%s", query), nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
testSpans(t, spanRecorder, "C", codes.Unset)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
}
func TestExecutedOperationNameAsSpanNameWithoutOperationNameParameter(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if !span.IsValid() {
t.Fatalf("invalid span wrapping handler: %#v", span)
}
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
srv.Use(Middleware())
query := url.QueryEscape("query ThisIsOperationName {__typename}")
r := httptest.NewRequest("GET", fmt.Sprintf("/foo?query=%s", query), nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
testSpans(t, spanRecorder, "ThisIsOperationName", codes.Unset)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
}
func TestChildSpanFromGlobalTracerWithNamed(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if !span.IsValid() {
t.Fatalf("invalid span wrapping handler: %#v", span)
}
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
srv.Use(Middleware())
body := strings.NewReader(fmt.Sprintf("{\"operationName\":\"%s\",\"variables\":{},\"query\":\"query %s {\\n name\\n}\\n\"}", testQueryName, testQueryName))
r := httptest.NewRequest("POST", "/foo", body)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
testSpans(t, spanRecorder, testQueryName, codes.Unset)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
}
func TestChildSpanFromCustomTracer(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if !span.IsValid() {
t.Fatalf("invalid span wrapping handler: %#v", span)
}
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
srv.Use(Middleware(WithTracerProvider(provider)))
r := httptest.NewRequest("GET", "/foo?query={name}", nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
testSpans(t, spanRecorder, namelessQueryName, codes.Unset)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
}
func TestChildSpanWithComplexityExtension(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if !span.IsValid() {
t.Fatalf("invalid span wrapping handler: %#v", span)
}
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
srv.Use(Middleware(WithComplexityExtensionName("APQ")))
r := httptest.NewRequest("GET", "/foo?query={name}", nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
testSpans(t, spanRecorder, namelessQueryName, codes.Unset)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
}
func TestChildSpanWithDropFromFields(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if !span.IsValid() {
t.Fatalf("invalid span wrapping handler: %#v", span)
}
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
srv.Use(Middleware(WithCreateSpanFromFields(func(ctx *graphql.FieldContext) bool {
return ctx.IsResolver || ctx.IsMethod
})))
r := httptest.NewRequest("GET", "/foo?query={name}", nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
spans := spanRecorder.Ended()
if got, expected := len(spans), 1; got != expected {
t.Fatalf("got %d spans, expected %d", got, expected)
}
responseSpan := spans[0]
if !responseSpan.SpanContext().IsValid() {
t.Fatalf("invalid span created: %#v", responseSpan.SpanContext())
}
if responseSpan.Name() != namelessQueryName {
t.Errorf("expected name on span %s; got: %q", namelessQueryName, responseSpan.Name())
}
for _, s := range spanRecorder.Ended() {
assert.Equal(t, s.Status().Code, codes.Unset)
}
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
}
func TestGetSpanNotInstrumented(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if span.IsValid() {
t.Fatalf("unexpected span: %#v", span)
}
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
r := httptest.NewRequest("GET", "/foo?query={name}", nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
}
func TestChildSpanFromGlobalTracerWithError(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServerError(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if !span.IsValid() {
t.Fatalf("invalid span wrapping handler: %#v", span)
}
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
srv.Use(Middleware())
var gqlErrors gqlerror.List
var respErrors gqlerror.List
srv.AroundResponses(func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
resp := next(ctx)
gqlErrors = graphql.GetErrors(ctx)
respErrors = resp.Errors
return resp
})
r := httptest.NewRequest("GET", "/foo?query={name}", nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
testSpans(t, spanRecorder, namelessQueryName, codes.Error)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
assert.Equal(t, 1, len(gqlErrors))
assert.Equal(t, gqlErrors, respErrors)
}
func TestChildSpanFromGlobalTracerWithComplexity(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if !span.IsValid() {
t.Fatalf("invalid span wrapping handler: %#v", span)
}
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
srv.Use(Middleware())
srv.Use(extension.FixedComplexityLimit(testComplexity))
r := httptest.NewRequest("GET", "/foo?query={name}", nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
testSpans(t, spanRecorder, namelessQueryName, codes.Unset)
// second span because it's response span where stored RequestComplexityLimit attribute
attributes := spanRecorder.Ended()[1].Attributes()
var found bool
for _, a := range attributes {
if a.Key == ("gql.request.complexityLimit") {
found = true
assert.Equal(t, int(a.Value.AsInt64()), testComplexity)
}
}
assert.True(t, found)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
}
func TestOperationName(t *testing.T) {
operation := "ExampleOperationName"
ctx := SetOperationName(context.Background(), operation)
assert.Equal(t, operation, GetOperationName(ctx))
}
func TestOperationNameInvalidInputJSON(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
srv.Use(Middleware())
// invalid json body
body := strings.NewReader("")
r := httptest.NewRequest("POST", "/foo", body)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
assert.Equal(t, http.StatusBadRequest, w.Code, w.Body.String())
assert.JSONEq(t, `{"errors":[{"message":"json request body could not be decoded: EOF body:"}],"data":null}`, w.Body.String())
}
func TestVariablesAttributes(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if !span.IsValid() {
t.Fatalf("invalid span wrapping handler: %#v", span)
}
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
srv.Use(Middleware())
body := strings.NewReader("{\"variables\":{\"id\":1},\"query\":\"query ($id: Int!) {\\n find(id: $id)\\n}\\n\"}")
r := httptest.NewRequest("POST", "/foo", body)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
testSpans(t, spanRecorder, namelessQueryName, codes.Unset)
spans := spanRecorder.Ended()
assert.Len(t, spans[1].Attributes(), 2)
assert.Equal(t, attribute.Key("gql.request.query"), spans[1].Attributes()[0].Key)
assert.Equal(t, attribute.Key("gql.request.variables.id"), spans[1].Attributes()[1].Key)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
}
func TestVariablesAttributesCustomBuilder(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if !span.IsValid() {
t.Fatalf("invalid span wrapping handler: %#v", span)
}
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
srv.Use(Middleware(WithRequestVariablesAttributesBuilder(func(requestVariables map[string]interface{}) []attribute.KeyValue {
variables := make([]attribute.KeyValue, 0, len(requestVariables))
for k, v := range requestVariables {
variables = append(variables,
attribute.String(k, fmt.Sprintf("%+v", v)),
)
}
return variables
})))
body := strings.NewReader("{\"variables\":{\"id\":1},\"query\":\"query ($id: Int!) {\\n find(id: $id)\\n}\\n\"}")
r := httptest.NewRequest("POST", "/foo", body)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
testSpans(t, spanRecorder, namelessQueryName, codes.Unset)
spans := spanRecorder.Ended()
assert.Len(t, spans[1].Attributes(), 2)
assert.Equal(t, attribute.Key("gql.request.query"), spans[1].Attributes()[0].Key)
assert.Equal(t, attribute.Key("id"), spans[1].Attributes()[1].Key)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
}
func TestVariablesAttributesDisabled(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if !span.IsValid() {
t.Fatalf("invalid span wrapping handler: %#v", span)
}
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
})
srv.Use(Middleware(WithoutVariables()))
body := strings.NewReader("{\"variables\":{\"id\":1},\"query\":\"query ($id: Int!) {\\n find(id: $id)\\n}\\n\"}")
r := httptest.NewRequest("POST", "/foo", body)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
testSpans(t, spanRecorder, namelessQueryName, codes.Unset)
spans := spanRecorder.Ended()
assert.Len(t, spans[1].Attributes(), 1)
assert.Equal(t, attribute.Key("gql.request.query"), spans[1].Attributes()[0].Key)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
}
func TestNilResponse(t *testing.T) {
spanRecorder := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
otel.SetTracerProvider(provider)
srv := newMockServer(func(ctx context.Context) (interface{}, error) {
span := trace.SpanContextFromContext(ctx)
if !span.IsValid() {
t.Fatalf("invalid span wrapping handler: %#v", span)
}
return (*graphql.Response)(nil), nil
})
srv.Use(Middleware())
r := httptest.NewRequest("GET", "/foo?query={name}", nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
testSpans(t, spanRecorder, namelessQueryName, codes.Unset)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
}
// newMockServer provides a server for use in resolver tests that isn't relying on generated code.
// It isn't a perfect reproduction of a generated server, but it aims to be good enough to
// test the handler package without relying on codegen.
func newMockServer(resolver func(ctx context.Context) (interface{}, error)) *handler.Server {
schema := gqlparser.MustLoadSchema(&ast.Source{Input: `
type Query {
name: String!
find(id: Int!): String!
}
type Mutation {
name: String!
}
type Subscription {
name: String!
}
`})
srv := handler.New(&graphql.ExecutableSchemaMock{
ExecFunc: func(ctx context.Context) graphql.ResponseHandler {
rc := graphql.GetOperationContext(ctx)
switch rc.Operation.Operation {
case ast.Query:
ran := false
return func(ctx context.Context) *graphql.Response {
if ran {
return nil
}
ran = true
// Field execution happens inside the generated code, lets simulate some of it.
ctx = graphql.WithFieldContext(ctx, &graphql.FieldContext{
Object: "Query",
Field: graphql.CollectedField{
Field: &ast.Field{
Name: "name",
Alias: "alias",
Definition: schema.Types["Query"].Fields.ForName("name"),
ObjectDefinition: &ast.Definition{
Kind: "kind",
Description: "description",
Name: "name",
},
},
},
})
res, err := graphql.GetOperationContext(ctx).ResolverMiddleware(ctx, resolver)
if err != nil {
panic(err)
}
return res.(*graphql.Response)
}
default:
return graphql.OneShot(graphql.ErrorResponse(ctx, "unsupported GraphQL operation"))
}
},
SchemaFunc: func() *ast.Schema {
return schema
},
ComplexityFunc: func(typeName string, fieldName string, childComplexity int, args map[string]interface{}) (int, bool) {
return childComplexity, true
},
})
srv.AddTransport(&transport.GET{})
srv.AddTransport(&transport.POST{})
return srv
}
// newMockServerError provides a server for use in resolver error tests that isn't relying on generated code.
// It isn't a perfect reproduction of a generated server, but it aims to be good enough to
// test the handler package without relying on codegen.
func newMockServerError(resolver func(ctx context.Context) (interface{}, error)) *handler.Server {
schema := gqlparser.MustLoadSchema(&ast.Source{Input: `
type Query {
name: String!
}
`})
srv := handler.New(&graphql.ExecutableSchemaMock{
ExecFunc: func(ctx context.Context) graphql.ResponseHandler {
rc := graphql.GetOperationContext(ctx)
switch rc.Operation.Operation {
case ast.Query:
ran := false
return func(ctx context.Context) *graphql.Response {
if ran {
return nil
}
ran = true
// Field execution happens inside the generated code, lets simulate some of it.
ctx = graphql.WithFieldContext(ctx, &graphql.FieldContext{
Object: "Query",
Field: graphql.CollectedField{
Field: &ast.Field{
Name: "name",
Alias: "alias",
Definition: schema.Types["Query"].Fields.ForName("name"),
ObjectDefinition: &ast.Definition{
Kind: "kind",
Description: "description",
Name: "name",
},
},
},
})
graphql.AddError(ctx, fmt.Errorf("resolver error"))
res, err := graphql.GetOperationContext(ctx).ResolverMiddleware(ctx, resolver)
if err != nil {
panic(err)
}
return res.(*graphql.Response)
}
default:
return graphql.OneShot(graphql.ErrorResponse(ctx, "unsupported GraphQL operation"))
}
},
SchemaFunc: func() *ast.Schema {
return schema
},
})
srv.AddTransport(&transport.GET{})
return srv
}
func testSpans(t *testing.T, spanRecorder *tracetest.SpanRecorder, spanName string, spanCode codes.Code) {
spans := spanRecorder.Ended()
if got, expected := len(spans), 2; got != expected {
t.Fatalf("got %d spans, expected %d", got, expected)
}
responseSpan := spans[1]
if !responseSpan.SpanContext().IsValid() {
t.Fatalf("invalid span created: %#v", responseSpan.SpanContext())
}
if responseSpan.Name() != spanName {
t.Errorf("expected name on span %s; got: %q", spanName, responseSpan.Name())
}
for _, s := range spanRecorder.Ended() {
assert.Equal(t, s.Status().Code, spanCode)
}
}