Skip to content

Commit 89ac725

Browse files
authored
Add getMainDefinition back to @apollo/client/utilities (#12725)
1 parent 699c830 commit 89ac725

File tree

12 files changed

+61
-14
lines changed

12 files changed

+61
-14
lines changed

.api-reports/api-report-link.api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { DocumentNode } from 'graphql';
1010
import type { FormattedExecutionResult } from 'graphql';
1111
import type { GraphQLFormattedError } from 'graphql';
1212
import type { Observable } from 'rxjs';
13+
import type { OperationTypeNode } from 'graphql';
1314

1415
// @public (undocumented)
1516
export interface AdditionalFetchResultTypes<TData = Record<string, any>, TExtensions = Record<string, any>> {
@@ -82,6 +83,8 @@ export interface GraphQLRequest<TVariables = Record<string, any>> {
8283
// (undocumented)
8384
operationName?: string;
8485
// (undocumented)
86+
operationType?: OperationTypeNode;
87+
// (undocumented)
8588
query: DocumentNode;
8689
// (undocumented)
8790
variables?: TVariables;
@@ -101,6 +104,8 @@ export interface Operation {
101104
// (undocumented)
102105
operationName: string;
103106
// (undocumented)
107+
operationType: OperationTypeNode | undefined;
108+
// (undocumented)
104109
query: DocumentNode;
105110
// (undocumented)
106111
setContext: {

.api-reports/api-report-utilities.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { DocumentNode as DocumentNode_2 } from '@apollo/client';
1111
import type { FieldNode } from 'graphql';
1212
import type { FieldPolicy } from '@apollo/client/cache';
1313
import type { FormattedExecutionResult } from 'graphql';
14+
import { getMainDefinition } from '@apollo/client/utilities/internal';
1415
import { Observable } from 'rxjs';
1516
import type { Primitive } from '@apollo/client/utilities/internal';
1617
import type { Reference as Reference_2 } from '@apollo/client/cache';
@@ -120,6 +121,8 @@ interface DocumentTransformOptions {
120121
getCacheKey?: (document: DocumentNode) => DocumentTransformCacheKey | undefined;
121122
}
122123

124+
export { getMainDefinition }
125+
123126
// @beta
124127
export interface HKT {
125128
// (undocumented)

.api-reports/api-report.api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,8 @@ export interface GraphQLRequest<TVariables = Record<string, any>> {
11271127
// (undocumented)
11281128
operationName?: string;
11291129
// (undocumented)
1130+
operationType?: OperationTypeNode;
1131+
// (undocumented)
11301132
query: DocumentNode;
11311133
// (undocumented)
11321134
variables?: TVariables;
@@ -1953,6 +1955,8 @@ export interface Operation {
19531955
// (undocumented)
19541956
operationName: string;
19551957
// (undocumented)
1958+
operationType: OperationTypeNode | undefined;
1959+
// (undocumented)
19561960
query: DocumentNode;
19571961
// (undocumented)
19581962
setContext: {

.changeset/lucky-camels-compete.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Export `getMainDefinition` from `@apollo/client/utilities`.

.changeset/thirty-lemons-rhyme.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
"@apollo/client": minor
3+
---
4+
5+
Add `operationType` to `operation` in `ApolloLink`. This means that determining whether a `query` is a specific operation type can now be compared with this property instead of using `getMainDefinition`.
6+
7+
```diff
8+
- import { getMainDefinition } from "@apollo/client/utilities";
9+
+ import { OperationTypeNode } from "graphql";
10+
11+
ApolloLink.split(
12+
- ({ query }) => {
13+
- const definition = getMainDefinition(query);
14+
- return (
15+
- definition.kind === 'OperationDefinition' &&
16+
- definition.operation === 'subscription'
17+
- );
18+
- return
19+
- },
20+
+ ({ operationType }) => {
21+
+ return operationType === OperationTypeNode.SUBSCRIPTION;
22+
+ },
23+
conditionTrueLink,
24+
conditionFalseLink,
25+
);
26+
```

.size-limits.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 43730,
3-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38702,
4-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 33448,
5-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27662
2+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 43674,
3+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38705,
4+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 33447,
5+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27639
66
}

src/__tests__/__snapshots__/exports.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ Array [
394394
"cacheSizes",
395395
"canonicalStringify",
396396
"concatPagination",
397+
"getMainDefinition",
397398
"isFormattedExecutionResult",
398399
"isMutationOperation",
399400
"isQueryOperation",

src/core/QueryManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ export class QueryManager {
894894
query: serverQuery,
895895
variables,
896896
operationName,
897+
operationType,
897898
context: {
898899
...this.defaultContext,
899900
...context,

src/link/core/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { FormattedExecutionResult, GraphQLFormattedError } from "graphql";
1+
import type {
2+
FormattedExecutionResult,
3+
GraphQLFormattedError,
4+
OperationTypeNode,
5+
} from "graphql";
26
import type { DocumentNode } from "graphql";
37
import type { Observable } from "rxjs";
48

@@ -20,6 +24,7 @@ export interface GraphQLRequest<TVariables = Record<string, any>> {
2024
query: DocumentNode;
2125
variables?: TVariables;
2226
operationName?: string;
27+
operationType?: OperationTypeNode;
2328
context?: DefaultContext;
2429
extensions?: Record<string, any>;
2530
}
@@ -28,6 +33,7 @@ export interface Operation {
2833
query: DocumentNode;
2934
variables: Record<string, any>;
3035
operationName: string;
36+
operationType: OperationTypeNode | undefined;
3137
extensions: Record<string, any>;
3238
setContext: {
3339
(context: Partial<OperationContext>): void;

src/link/utils/validateOperation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export function validateOperation(operation: GraphQLRequest): GraphQLRequest {
55
const OPERATION_FIELDS = [
66
"query",
77
"operationName",
8+
"operationType",
89
"variables",
910
"extensions",
1011
"context",

0 commit comments

Comments
 (0)