@@ -6,8 +6,6 @@ import * as Sentry from '@sentry/node';
6
6
import { Span } from '@sentry/types' ;
7
7
import { ExecutionArgs , Kind , OperationDefinitionNode , print , responsePathAsArray } from 'graphql' ;
8
8
9
- const tracingSpanSymbol = Symbol ( 'SENTRY_GRAPHQL' ) ;
10
-
11
9
export type SentryPluginOptions = {
12
10
startTransaction ?: boolean ;
13
11
renameTransaction ?: boolean ;
@@ -20,18 +18,20 @@ export type SentryPluginOptions = {
20
18
operationName ?: ( args : ExecutionArgs ) => string ;
21
19
} ;
22
20
23
- interface PluginContext {
24
- [ tracingSpanSymbol ] : Span ;
25
- }
26
-
27
- export const useSentry = (
28
- options : SentryPluginOptions = {
29
- startTransaction : true ,
30
- trackResolvers : true ,
21
+ export const useSentry = ( options : SentryPluginOptions = { } ) : Plugin => {
22
+ function pick < K extends keyof SentryPluginOptions > ( key : K , defaultValue ?: SentryPluginOptions [ K ] ) : SentryPluginOptions [ K ] {
23
+ return prioritize ( options [ key ] , defaultValue ) ;
31
24
}
32
- ) : Plugin < PluginContext > => {
25
+
26
+ const startTransaction = pick ( 'startTransaction' , true ) ;
27
+ const trackResolvers = pick ( 'trackResolvers' , true ) ;
28
+ const includeResolverArgs = pick ( 'includeResolverArgs' , false ) ;
29
+ const includeRawResult = pick ( 'includeRawResult' , false ) ;
30
+ const includeExecuteVariables = pick ( 'includeExecuteVariables' , false ) ;
31
+ const renameTransaction = pick ( 'renameTransaction' , false ) ;
32
+
33
33
return {
34
- onExecute ( { args, extendContext } ) {
34
+ onExecute ( { args } ) {
35
35
const rootOperation = args . document . definitions . find ( o => o . kind === Kind . OPERATION_DEFINITION ) as OperationDefinitionNode ;
36
36
const operationType = rootOperation . operation ;
37
37
const document = print ( args . document ) ;
@@ -48,7 +48,7 @@ export const useSentry = (
48
48
49
49
let rootSpan : Span ;
50
50
51
- if ( options . startTransaction ) {
51
+ if ( startTransaction ) {
52
52
rootSpan = Sentry . startTransaction ( {
53
53
name : transactionName ,
54
54
op,
@@ -75,30 +75,26 @@ export const useSentry = (
75
75
76
76
rootSpan = span ;
77
77
78
- if ( options . renameTransaction ) {
78
+ if ( renameTransaction ) {
79
79
scope ! . setTransactionName ( transactionName ) ;
80
80
}
81
81
}
82
82
83
83
rootSpan . setData ( 'document' , document ) ;
84
84
85
- extendContext ( {
86
- [ tracingSpanSymbol ] : rootSpan ,
87
- } ) ;
88
-
89
- const onResolverCalled : OnExecuteHookResult < PluginContext > [ 'onResolverCalled' ] = options . trackResolvers
90
- ? ( { args : resolversArgs , info, context } ) => {
91
- if ( context && context [ tracingSpanSymbol ] ) {
85
+ const onResolverCalled : OnExecuteHookResult [ 'onResolverCalled' ] = trackResolvers
86
+ ? ( { args : resolversArgs , info } ) => {
87
+ if ( rootSpan ) {
92
88
const { fieldName, returnType, parentType } = info ;
93
- const parent : typeof rootSpan = context [ tracingSpanSymbol ] ;
89
+ const parent = rootSpan ;
94
90
const tags : Record < string , string > = {
95
91
fieldName,
96
92
parentType : parentType . toString ( ) ,
97
93
returnType : returnType . toString ( ) ,
98
94
} ;
99
95
100
- if ( options . includeResolverArgs ) {
101
- tags . args = options . includeResolverArgs ? JSON . stringify ( resolversArgs || { } ) : '' ;
96
+ if ( includeResolverArgs ) {
97
+ tags . args = JSON . stringify ( resolversArgs || { } ) ;
102
98
}
103
99
104
100
const childSpan = parent . startChild ( {
@@ -107,7 +103,7 @@ export const useSentry = (
107
103
} ) ;
108
104
109
105
return ( { result } ) => {
110
- if ( options . includeRawResult ) {
106
+ if ( includeRawResult ) {
111
107
childSpan . setData ( 'result' , result ) ;
112
108
}
113
109
@@ -130,7 +126,7 @@ export const useSentry = (
130
126
return {
131
127
onResolverCalled,
132
128
onExecuteDone ( { result } ) {
133
- if ( options . includeRawResult ) {
129
+ if ( includeRawResult ) {
134
130
rootSpan . setData ( 'result' , result ) ;
135
131
}
136
132
@@ -144,11 +140,11 @@ export const useSentry = (
144
140
145
141
scope . setTags ( addedTags || { } ) ;
146
142
147
- if ( options . includeRawResult ) {
143
+ if ( includeRawResult ) {
148
144
scope . setExtra ( 'result' , result ) ;
149
145
}
150
146
151
- if ( options . includeExecuteVariables ) {
147
+ if ( includeExecuteVariables ) {
152
148
scope . setExtra ( 'variables' , args . variableValues ) ;
153
149
}
154
150
@@ -182,3 +178,16 @@ export const useSentry = (
182
178
} ,
183
179
} ;
184
180
} ;
181
+
182
+ /**
183
+ * Sets a priority for provided values from left (highest) to right (lowest)
184
+ */
185
+ function prioritize < T > ( ...values : T [ ] ) : T {
186
+ const picked = values . find ( val => typeof val !== 'undefined' ) ;
187
+
188
+ if ( typeof picked === 'undefined' ) {
189
+ return values [ values . length - 1 ] ;
190
+ }
191
+
192
+ return picked ;
193
+ }
0 commit comments