Skip to content

Add getMainDefinition back to @apollo/client/utilities #12725

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

Merged
merged 18 commits into from
Jun 24, 2025

Conversation

jerelmiller
Copy link
Member

Fixes #12722

We recommend this in our docs so we should continue to support this as a public utility.

Copy link

changeset-bot bot commented Jun 19, 2025

🦋 Changeset detected

Latest commit: cf54a50

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

pkg-pr-new bot commented Jun 19, 2025

npm i https://pkg.pr.new/apollographql/apollo-client/@apollo/client@12725

commit: cf54a50

Copy link
Contributor

github-actions bot commented Jun 19, 2025

size-limit report 📦

Path Size
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (CJS) 42.65 KB (-0.13% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) (CJS) 37.8 KB (+0.01% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" 32.66 KB (-0.01% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) 26.99 KB (-0.09% 🔽)
import { ApolloProvider } from "@apollo/client/react" 5.74 KB (0%)
import { ApolloProvider } from "@apollo/client/react" (production) 959 B (0%)
import { useQuery } from "@apollo/client/react" 7.51 KB (0%)
import { useQuery } from "@apollo/client/react" (production) 2.72 KB (0%)
import { useLazyQuery } from "@apollo/client/react" 6.97 KB (0%)
import { useLazyQuery } from "@apollo/client/react" (production) 2.19 KB (0%)
import { useMutation } from "@apollo/client/react" 6.54 KB (0%)
import { useMutation } from "@apollo/client/react" (production) 1.76 KB (0%)
import { useSubscription } from "@apollo/client/react" 6.65 KB (0%)
import { useSubscription } from "@apollo/client/react" (production) 1.85 KB (0%)
import { useSuspenseQuery } from "@apollo/client/react" 8.51 KB (0%)
import { useSuspenseQuery } from "@apollo/client/react" (production) 3.73 KB (0%)
import { useBackgroundQuery } from "@apollo/client/react" 8.25 KB (0%)
import { useBackgroundQuery } from "@apollo/client/react" (production) 3.5 KB (0%)
import { useLoadableQuery } from "@apollo/client/react" 8.57 KB (+0.05% 🔺)
import { useLoadableQuery } from "@apollo/client/react" (production) 3.82 KB (+0.13% 🔺)
import { useReadQuery } from "@apollo/client/react" 6.51 KB (0%)
import { useReadQuery } from "@apollo/client/react" (production) 1.68 KB (0%)
import { useFragment } from "@apollo/client/react" 6.53 KB (0%)
import { useFragment } from "@apollo/client/react" (production) 1.74 KB (0%)

@@ -24,19 +21,13 @@ export function getMainDefinition(

for (let definition of queryDoc.definitions) {
if (definition.kind === "OperationDefinition") {
const operation = (definition as OperationDefinitionNode).operation;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of these type-casts were unnecessary since TypeScript already narrowed the type based on the .kind check above.

operation is also a non-null property with only 3 operation types, so I went ahead and removed the check here since we can assume GraphQL will always return a valid operation type.

@phryneas
Copy link
Member

As an alternative, what do you think about adding operationType as a property on Operation?

That should cover all our documented use cases and even save users from fiddling on AST nodes.

@jerelmiller jerelmiller force-pushed the jerel/get-main-definition branch from f94b559 to 702e9d3 Compare June 23, 2025 19:05
@jerelmiller jerelmiller changed the title Move getMainDefinition back to @apollo/client/utilities Add getMainDefinition back to @apollo/client/utilities Jun 23, 2025
@github-actions github-actions bot added the auto-cleanup 🤖 label Jun 24, 2025
@jerelmiller jerelmiller merged commit 89ac725 into release-4.0 Jun 24, 2025
42 of 43 checks passed
@jerelmiller jerelmiller deleted the jerel/get-main-definition branch June 24, 2025 14:58
@github-actions github-actions bot mentioned this pull request Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants