Skip to content

Commit 5c4a431

Browse files
Add apollo link handler to intercept graphQL request
1 parent 93ee4e9 commit 5c4a431

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,12 @@ export namespace NetworkLogger {
532532
* @param {function} handler
533533
*/
534534
function setProgressHandlerForRequest(handler: () => void): void;
535+
/**
536+
* Apollo Link Request Handler to track network log for graphQL using apollo
537+
* @param {any} operation
538+
* @param {any} forward
539+
*/
540+
function apolloLinkRequestHandler(operation: any, forward: any):any;
535541
}
536542
export class Trace {
537543
constructor(id: string, name?: string, attributes?: object);

modules/NetworkLogger.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@ import { NativeModules, Platform } from 'react-native';
22
import xhr from '../utils/XhrNetworkInterceptor';
33
import IBGEventEmitter from '../utils/IBGEventEmitter.js';
44
import InstabugConstants from '../utils/InstabugConstants';
5-
let { Instabug } = NativeModules;
6-
5+
let { Instabug, IBGAPM } = NativeModules;
76

87
var _networkDataObfuscationHandlerSet = false;
98
var _requestFilterExpression = false;
9+
const appendOperationName = (headers, operationName) => {
10+
try {
11+
let newHeaders = JSON.parse(JSON.stringify(headers));
12+
newHeaders[InstabugConstants.GRAPHQL_HEADER] = operationName;
13+
return newHeaders;
14+
} catch(e) {
15+
console.error(e);
16+
return headers;
17+
}
18+
};
1019

1120
/**
1221
* NetworkLogger
@@ -32,6 +41,7 @@ export default {
3241
try {
3342
if (Platform.OS === 'android') {
3443
Instabug.networkLog(JSON.stringify(network));
44+
IBGAPM.networkLog(JSON.stringify(network));
3545
} else {
3646
Instabug.networkLog(network);
3747
}
@@ -57,13 +67,15 @@ export default {
5767
}
5868
_networkDataObfuscationHandlerSet = true;
5969

60-
IBGEventEmitter.addListener(Instabug,
70+
IBGEventEmitter.addListener(
71+
Instabug,
6172
InstabugConstants.NETWORK_DATA_OBFUSCATION_HANDLER_EVENT,
6273
async data => {
6374
try {
6475
const newData = await handler(data);
6576
if (Platform.OS === 'android') {
6677
Instabug.networkLog(JSON.stringify(newData));
78+
IBGAPM.networkLog(JSON.stringify(newData));
6779
} else {
6880
Instabug.networkLog(newData);
6981
}
@@ -74,7 +86,6 @@ export default {
7486
);
7587
},
7688

77-
7889
/**
7990
* Omit requests from being logged based on either their request or response details
8091
* @param {string} expression
@@ -91,5 +102,15 @@ export default {
91102
xhr.setOnProgressCallback(handler);
92103
},
93104

105+
apolloLinkRequestHandler(operation, forward) {
106+
try {
107+
operation.setContext(({ headers = {} }) => ({
108+
headers: appendOperationName(headers, operation.operationName),
109+
}));
110+
} catch(e) {
111+
console.error(e);
112+
}
94113

114+
return forward(operation);
115+
},
95116
};

0 commit comments

Comments
 (0)