Skip to content

Commit e7876d7

Browse files
committed
feat(browser): Add support for queries with operation name
Signed-off-by: Kaung Zin Hein <83657429+Zen-cronic@users.noreply.github.com>
1 parent 22c2c92 commit e7876d7

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

packages/browser/src/integrations/graphqlClient.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ interface GraphQLRequestPayload {
2424
}
2525

2626
interface GraphQLOperation {
27-
operationType: string | undefined;
28-
operationName: string | undefined;
27+
operationType?: string;
28+
operationName?: string;
2929
}
3030

3131
const INTEGRATION_NAME = 'GraphQLClient';
@@ -144,14 +144,22 @@ export function getRequestPayloadXhrOrFetch(hint: XhrHint | FetchHint): string |
144144
* Exported for tests only.
145145
*/
146146
export function parseGraphQLQuery(query: string): GraphQLOperation {
147-
const queryRe = /^(?:\s*)(query|mutation|subscription)(?:\s*)(\w+)(?:\s*)[{(]/;
147+
const namedQueryRe = /^(?:\s*)(query|mutation|subscription)(?:\s*)(\w+)(?:\s*)[{(]/;
148+
const unnamedQueryRe = /^(?:\s*)(query|mutation|subscription)(?:\s*)[{(]/;
148149

149-
const matched = query.match(queryRe);
150+
const namedMatch = query.match(namedQueryRe);
151+
if (namedMatch) {
152+
return {
153+
operationType: namedMatch[1],
154+
operationName: namedMatch[2],
155+
};
156+
}
150157

151-
if (matched) {
158+
const unnamedMatch = query.match(unnamedQueryRe);
159+
if (unnamedMatch) {
152160
return {
153-
operationType: matched[1],
154-
operationName: matched[2],
161+
operationType: unnamedMatch[1],
162+
operationName: undefined,
155163
};
156164
}
157165
return {

packages/browser/test/integrations/graphqlClient.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ describe('GraphqlClient', () => {
3232
}
3333
}`;
3434

35-
// TODO: support name-less queries
36-
// const queryFour = ` query {
37-
// items {
38-
// id
39-
// }
40-
// }`;
35+
const queryFour = `query {
36+
items {
37+
id
38+
}
39+
}`;
4140

4241
test.each([
4342
['should handle query type', queryOne, { operationName: 'Test', operationType: 'query' }],
@@ -47,7 +46,7 @@ describe('GraphqlClient', () => {
4746
queryThree,
4847
{ operationName: 'OnTestItemAdded', operationType: 'subscription' },
4948
],
50-
// TODO: ['should handle query without name', queryFour, { operationName: undefined, operationType: 'query' }],
49+
['should handle query without name', queryFour, { operationName: undefined, operationType: 'query' }],
5150
])('%s', (_, input, output) => {
5251
expect(parseGraphQLQuery(input)).toEqual(output);
5352
});
@@ -64,6 +63,7 @@ describe('GraphqlClient', () => {
6463
query: 'query Test {\r\n items {\r\n id\r\n }\r\n }',
6564
operationName: 'Test',
6665
variables: {},
66+
extensions: {},
6767
};
6868

6969
expect(getGraphQLRequestPayload(JSON.stringify(requestBody))).toEqual(requestBody);

0 commit comments

Comments
 (0)