@@ -12,17 +12,19 @@ interface GraphQLClientOptions {
12
12
endpoints : Array < string > ;
13
13
}
14
14
15
+ interface GraphQLRequestPayload {
16
+ query : string ;
17
+ operationName ?: string ;
18
+ variables ?: Record < string , any > ;
19
+ }
20
+
15
21
const INTEGRATION_NAME = 'GraphQLClient' ;
16
22
17
23
const _graphqlClientIntegration = ( ( options : GraphQLClientOptions ) => {
18
24
return {
19
25
name : INTEGRATION_NAME ,
20
26
setup ( client ) {
21
- client . on ( 'spanStart' , span => {
22
- client . emit ( 'outgoingRequestSpanStart' , span ) ;
23
- } ) ;
24
-
25
- client . on ( 'outgoingRequestSpanStart' , span => {
27
+ client . on ( 'outgoingRequestSpanStart' , ( span , { body } ) => {
26
28
const spanJSON = spanToJSON ( span ) ;
27
29
28
30
const spanAttributes = spanJSON . data || { } ;
@@ -38,19 +40,17 @@ const _graphqlClientIntegration = ((options: GraphQLClientOptions) => {
38
40
39
41
if ( isTracedGraphqlEndpoint ) {
40
42
const httpMethod = spanAttributes [ SEMANTIC_ATTRIBUTE_HTTP_REQUEST_METHOD ] || spanAttributes [ 'http.method' ] ;
41
- const graphqlBody = spanAttributes [ ' body' ] ;
43
+ const graphqlBody = body as GraphQLRequestPayload ;
42
44
43
45
// Standard graphql request shape: https://graphql.org/learn/serving-over-http/#post-request
44
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
45
- const graphqlQuery = graphqlBody && ( graphqlBody [ 'query' ] as string ) ;
46
-
47
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
48
- const graphqlOperationName = graphqlBody && ( graphqlBody [ 'operationName' ] as string ) ;
46
+ const graphqlQuery = graphqlBody . query ;
47
+ const graphqlOperationName = graphqlBody . operationName ;
49
48
50
49
const { operationName = graphqlOperationName , operationType } = parseGraphQLQuery ( graphqlQuery ) ;
51
50
const newOperation = operationName ? `${ operationType } ${ operationName } ` : `${ operationType } ` ;
52
51
53
52
span . updateName ( `${ httpMethod } ${ httpUrl } (${ newOperation } )` ) ;
53
+ span . setAttribute ( 'body' , JSON . stringify ( graphqlBody ) ) ;
54
54
}
55
55
}
56
56
} ) ;
0 commit comments