1
1
'use strict'
2
2
3
+ const { expect } = require ( 'chai' )
4
+ const semver = require ( 'semver' )
3
5
const agent = require ( '../../dd-trace/test/plugins/agent' )
4
6
const plugin = require ( '../src' )
5
7
@@ -175,6 +177,7 @@ describe('Plugin', () => {
175
177
176
178
it ( 'should instrument parsing' , done => {
177
179
const source = `query MyQuery { hello(name: "world") }`
180
+ const variableValues = { who : 'world' }
178
181
179
182
agent
180
183
. use ( traces => {
@@ -189,11 +192,12 @@ describe('Plugin', () => {
189
192
. then ( done )
190
193
. catch ( done )
191
194
192
- graphql . graphql ( schema , source , null , null , { who : 'world' } ) . catch ( done )
195
+ graphql . graphql ( { schema, source, variableValues } ) . catch ( done )
193
196
} )
194
197
195
198
it ( 'should instrument validation' , done => {
196
199
const source = `query MyQuery { hello(name: "world") }`
200
+ const variableValues = { who : 'world' }
197
201
198
202
agent
199
203
. use ( traces => {
@@ -208,11 +212,12 @@ describe('Plugin', () => {
208
212
. then ( done )
209
213
. catch ( done )
210
214
211
- graphql . graphql ( schema , source , null , null , { who : 'world' } ) . catch ( done )
215
+ graphql . graphql ( { schema, source, variableValues } ) . catch ( done )
212
216
} )
213
217
214
218
it ( 'should instrument execution' , done => {
215
219
const source = `query MyQuery { hello(name: "world") }`
220
+ const variableValues = { who : 'world' }
216
221
217
222
agent
218
223
. use ( traces => {
@@ -229,11 +234,12 @@ describe('Plugin', () => {
229
234
. then ( done )
230
235
. catch ( done )
231
236
232
- graphql . graphql ( schema , source , null , null , { who : 'world' } ) . catch ( done )
237
+ graphql . graphql ( { schema, source, variableValues } ) . catch ( done )
233
238
} )
234
239
235
240
it ( 'should not include variables by default' , done => {
236
241
const source = `query MyQuery($who: String!) { hello(name: $who) }`
242
+ const variableValues = { who : 'world' }
237
243
238
244
agent
239
245
. use ( traces => {
@@ -243,7 +249,7 @@ describe('Plugin', () => {
243
249
. then ( done )
244
250
. catch ( done )
245
251
246
- graphql . graphql ( schema , source , null , null , { who : 'world' } ) . catch ( done )
252
+ graphql . graphql ( { schema, source, variableValues } ) . catch ( done )
247
253
} )
248
254
249
255
it ( 'should instrument schema resolvers' , done => {
@@ -267,7 +273,7 @@ describe('Plugin', () => {
267
273
. then ( done )
268
274
. catch ( done )
269
275
270
- graphql . graphql ( schema , source ) . catch ( done )
276
+ graphql . graphql ( { schema, source } ) . catch ( done )
271
277
} )
272
278
273
279
it ( 'should instrument nested field resolvers' , done => {
@@ -325,7 +331,7 @@ describe('Plugin', () => {
325
331
. then ( done )
326
332
. catch ( done )
327
333
328
- graphql . graphql ( schema , source ) . catch ( done )
334
+ graphql . graphql ( { schema, source } ) . catch ( done )
329
335
} )
330
336
331
337
it ( 'should instrument list field resolvers' , done => {
@@ -375,7 +381,7 @@ describe('Plugin', () => {
375
381
. then ( done )
376
382
. catch ( done )
377
383
378
- graphql . graphql ( schema , source ) . catch ( done )
384
+ graphql . graphql ( { schema, source } ) . catch ( done )
379
385
} )
380
386
381
387
it ( 'should instrument mutations' , done => {
@@ -390,7 +396,7 @@ describe('Plugin', () => {
390
396
. then ( done )
391
397
. catch ( done )
392
398
393
- graphql . graphql ( schema , source ) . catch ( done )
399
+ graphql . graphql ( { schema, source } ) . catch ( done )
394
400
} )
395
401
396
402
it ( 'should instrument subscriptions' , done => {
@@ -405,13 +411,13 @@ describe('Plugin', () => {
405
411
. then ( done )
406
412
. catch ( done )
407
413
408
- graphql . graphql ( schema , source ) . catch ( done )
414
+ graphql . graphql ( { schema, source } ) . catch ( done )
409
415
} )
410
416
411
417
it ( 'should handle a circular schema' , done => {
412
418
const source = `{ human { pets { owner { name } } } }`
413
419
414
- graphql . graphql ( schema , source )
420
+ graphql . graphql ( { schema, source } )
415
421
. then ( ( result ) => {
416
422
expect ( result . data . human . pets [ 0 ] . owner . name ) . to . equal ( 'test' )
417
423
} )
@@ -427,6 +433,7 @@ describe('Plugin', () => {
427
433
` )
428
434
429
435
const source = `{ hello }`
436
+ const rootValue = { hello : 'world' }
430
437
431
438
agent
432
439
. use ( traces => {
@@ -439,7 +446,7 @@ describe('Plugin', () => {
439
446
. then ( done )
440
447
. catch ( done )
441
448
442
- graphql . graphql ( schema , source , { hello : 'world' } ) . catch ( done )
449
+ graphql . graphql ( { schema, source, rootValue } ) . catch ( done )
443
450
} )
444
451
445
452
it ( 'should instrument the execution field resolver without a rootValue resolver' , done => {
@@ -484,14 +491,15 @@ describe('Plugin', () => {
484
491
. then ( done )
485
492
. catch ( done )
486
493
487
- graphql . graphql ( schema , source ) . catch ( done )
494
+ graphql . graphql ( { schema, source } ) . catch ( done )
488
495
} )
489
496
490
- graphql . graphql ( schema , source ) . catch ( done )
497
+ graphql . graphql ( { schema, source } ) . catch ( done )
491
498
} )
492
499
493
500
it ( 'should run parsing, validation and execution in the current context' , done => {
494
501
const source = `query MyQuery { hello(name: "world") }`
502
+ const variableValues = { who : 'world' }
495
503
const span = tracer . startSpan ( 'test.request' )
496
504
497
505
agent
@@ -520,7 +528,7 @@ describe('Plugin', () => {
520
528
. catch ( done )
521
529
522
530
tracer . scope ( ) . activate ( span , ( ) => {
523
- graphql . graphql ( schema , source , null , null , { who : 'world' } )
531
+ graphql . graphql ( { schema, source, variableValues } )
524
532
. then ( ( ) => span . finish ( ) )
525
533
. catch ( done )
526
534
} )
@@ -576,11 +584,11 @@ describe('Plugin', () => {
576
584
} )
577
585
578
586
it ( 'should handle unsupported operations' , ( ) => {
579
- const query = `query MyQuery { hello(name: "world") }`
587
+ const source = `query MyQuery { hello(name: "world") }`
580
588
const subscription = `subscription { human { name } }`
581
589
582
- return graphql . graphql ( schema , query )
583
- . then ( ( ) => graphql . graphql ( schema , subscription ) )
590
+ return graphql . graphql ( { schema, source } )
591
+ . then ( ( ) => graphql . graphql ( { schema, source : subscription } ) )
584
592
. then ( result => {
585
593
expect ( result ) . to . not . have . property ( 'errors' )
586
594
} )
@@ -631,7 +639,7 @@ describe('Plugin', () => {
631
639
. then ( done )
632
640
. catch ( done )
633
641
634
- graphql . execute ( schema , document )
642
+ graphql . execute ( { schema, document } )
635
643
} )
636
644
637
645
it ( 'should handle parsing exceptions' , done => {
@@ -767,7 +775,7 @@ describe('Plugin', () => {
767
775
. then ( done )
768
776
. catch ( done )
769
777
770
- Promise . resolve ( graphql . execute ( schema , document , rootValue ) )
778
+ Promise . resolve ( graphql . execute ( { schema, document, rootValue } ) )
771
779
. then ( res => {
772
780
error = res . errors [ 0 ]
773
781
} )
@@ -934,7 +942,7 @@ describe('Plugin', () => {
934
942
. then ( done )
935
943
. catch ( done )
936
944
937
- graphql . graphql ( schema , source ) . catch ( done )
945
+ graphql . graphql ( { schema, source } ) . catch ( done )
938
946
} )
939
947
940
948
it ( 'should handle single fragment definitions' , done => {
@@ -958,9 +966,42 @@ describe('Plugin', () => {
958
966
. then ( done )
959
967
. catch ( done )
960
968
961
- graphql . graphql ( schema , source ) . catch ( done )
969
+ graphql . graphql ( { schema, source } ) . catch ( done )
962
970
} )
963
971
972
+ // https://github.com/graphql/graphql-js/pull/2904
973
+ if ( ! semver . intersects ( version , '>=16' ) ) {
974
+ it ( 'should instrument using positional arguments' , done => {
975
+ const source = `query MyQuery { hello(name: "world") }`
976
+ const variableValues = { who : 'world' }
977
+
978
+ agent
979
+ . use ( traces => {
980
+ const spans = sort ( traces [ 0 ] )
981
+
982
+ expect ( spans [ 0 ] ) . to . have . property ( 'service' , 'test' )
983
+ expect ( spans [ 0 ] ) . to . have . property ( 'name' , 'graphql.execute' )
984
+ expect ( spans [ 0 ] ) . to . have . property ( 'resource' , 'query MyQuery{hello(name:"")}' )
985
+ expect ( spans [ 0 ] ) . to . have . property ( 'type' , 'graphql' )
986
+ expect ( spans [ 0 ] . meta ) . to . not . have . property ( 'graphql.source' )
987
+ expect ( spans [ 0 ] . meta ) . to . have . property ( 'graphql.operation.type' , 'query' )
988
+ expect ( spans [ 0 ] . meta ) . to . have . property ( 'graphql.operation.name' , 'MyQuery' )
989
+ } )
990
+ . then ( done )
991
+ . catch ( done )
992
+
993
+ graphql . graphql ( schema , source , null , null , variableValues ) . catch ( done )
994
+ } )
995
+ } else {
996
+ it ( 'should not support positional arguments' , done => {
997
+ const source = `query MyQuery { hello(name: "world") }`
998
+ const variableValues = { who : 'world' }
999
+
1000
+ graphql . graphql ( schema , source , null , null , variableValues )
1001
+ . then ( ( ) => done ( new Error ( 'Expected error.' ) ) , ( ) => done ( ) )
1002
+ } )
1003
+ }
1004
+
964
1005
// it('should not disable signature with invalid arguments', done => {
965
1006
// agent
966
1007
// .use(traces => {
@@ -1035,7 +1076,7 @@ describe('Plugin', () => {
1035
1076
. then ( done )
1036
1077
. catch ( done )
1037
1078
1038
- graphql . graphql ( schema , source ) . catch ( done )
1079
+ graphql . graphql ( { schema, source } ) . catch ( done )
1039
1080
} )
1040
1081
1041
1082
it ( 'should apply the filter callback to the variables' , done => {
@@ -1044,6 +1085,7 @@ describe('Plugin', () => {
1044
1085
hello(title: $title, name: $who)
1045
1086
}
1046
1087
`
1088
+ const variableValues = { title : 'planet' , who : 'world' }
1047
1089
1048
1090
agent
1049
1091
. use ( traces => {
@@ -1057,7 +1099,7 @@ describe('Plugin', () => {
1057
1099
. then ( done )
1058
1100
. catch ( done )
1059
1101
1060
- graphql . graphql ( schema , source , null , null , { title : 'planet' , who : 'world' } ) . catch ( done )
1102
+ graphql . graphql ( { schema, source, variableValues } ) . catch ( done )
1061
1103
} )
1062
1104
} )
1063
1105
@@ -1085,6 +1127,7 @@ describe('Plugin', () => {
1085
1127
hello(title: $title, name: $who)
1086
1128
}
1087
1129
`
1130
+ const variableValues = { title : 'planet' , who : 'world' }
1088
1131
1089
1132
agent
1090
1133
. use ( traces => {
@@ -1096,7 +1139,7 @@ describe('Plugin', () => {
1096
1139
. then ( done )
1097
1140
. catch ( done )
1098
1141
1099
- graphql . graphql ( schema , source , null , null , { title : 'planet' , who : 'world' } ) . catch ( done )
1142
+ graphql . graphql ( { schema, source, variableValues } ) . catch ( done )
1100
1143
} )
1101
1144
} )
1102
1145
@@ -1139,7 +1182,7 @@ describe('Plugin', () => {
1139
1182
. then ( done )
1140
1183
. catch ( done )
1141
1184
1142
- graphql . graphql ( schema , source ) . catch ( done )
1185
+ graphql . graphql ( { schema, source } ) . catch ( done )
1143
1186
} )
1144
1187
1145
1188
it ( 'should run the resolvers in the execution scope' , done => {
@@ -1217,7 +1260,7 @@ describe('Plugin', () => {
1217
1260
. then ( done )
1218
1261
. catch ( done )
1219
1262
1220
- graphql . graphql ( schema , source ) . catch ( done )
1263
+ graphql . graphql ( { schema, source } ) . catch ( done )
1221
1264
} )
1222
1265
} )
1223
1266
@@ -1271,7 +1314,7 @@ describe('Plugin', () => {
1271
1314
. then ( done )
1272
1315
. catch ( done )
1273
1316
1274
- graphql . graphql ( schema , source ) . catch ( done )
1317
+ graphql . graphql ( { schema, source } ) . catch ( done )
1275
1318
} )
1276
1319
} )
1277
1320
@@ -1304,7 +1347,7 @@ describe('Plugin', () => {
1304
1347
. then ( done )
1305
1348
. catch ( done )
1306
1349
1307
- graphql . graphql ( schema , source ) . catch ( done )
1350
+ graphql . graphql ( { schema, source } ) . catch ( done )
1308
1351
} )
1309
1352
} )
1310
1353
0 commit comments