Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): Remove getOperationName helper from @urql/core #3062

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quick-seahorses-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Fix incorrect operation name being picked from queries that contain multiple operations.
6 changes: 6 additions & 0 deletions .changeset/young-lamps-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@urql/core': major
'@urql/exchange-execute': minor
---

Remove `getOperationName` export from `@urql/core`
7 changes: 2 additions & 5 deletions exchanges/execute/src/execute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
makeErrorResult,
makeOperation,
Client,
getOperationName,
OperationResult,
} from '@urql/core';

Expand All @@ -45,10 +44,8 @@ const exchangeArgs = {
client: {},
} as any;

const expectedQueryOperationName = getOperationName(queryOperation.query);
const expectedSubscribeOperationName = getOperationName(
subscriptionOperation.query
);
const expectedQueryOperationName = 'getUser';
const expectedSubscribeOperationName = 'subscribeToUser';

const fetchMock = (global as any).fetch as Mock;
const mockHttpResponseData = { key: 'value' };
Expand Down
12 changes: 10 additions & 2 deletions exchanges/execute/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
subscribe,
ExecutionArgs,
SubscriptionArgs,
Kind,
} from 'graphql';

import {
Expand All @@ -27,7 +28,6 @@ import {
mergeResultPatch,
Operation,
OperationResult,
getOperationName,
} from '@urql/core';

export interface ExecuteExchangeArgs {
Expand Down Expand Up @@ -150,14 +150,22 @@ export const executeExchange = ({
}
}

let operationName: string | undefined;
for (const node of operation.query.definitions) {
if (node.kind === Kind.OPERATION_DEFINITION) {
operationName = node.name ? node.name.value : undefined;
break;
}
}

return pipe(
makeExecuteSource(operation, {
schema,
document: operation.query,
rootValue,
contextValue,
variableValues,
operationName: getOperationName(operation.query),
operationName,
fieldResolver,
typeResolver,
subscribeFieldResolver,
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ export {
formatDocument,
maskTypename,
makeOperation,
getOperationName,
} from './utils';
4 changes: 2 additions & 2 deletions packages/core/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ export const createRequest = <
*/
export const getOperationName = (query: DocumentNode): string | undefined => {
for (const node of query.definitions) {
if (node.kind === Kind.OPERATION_DEFINITION && node.name) {
return node.name.value;
if (node.kind === Kind.OPERATION_DEFINITION) {
return node.name ? node.name.value : undefined;
}
}
};
Expand Down