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

useQuery is crashing if clientId is set in the options #1557

Open
tukanoidd opened this issue Jun 5, 2024 · 0 comments
Open

useQuery is crashing if clientId is set in the options #1557

tukanoidd opened this issue Jun 5, 2024 · 0 comments

Comments

@tukanoidd
Copy link

tukanoidd commented Jun 5, 2024

Describe the bug
I'm working on a web app. I need to mock graphql requests in those tests. I use mock-apollo-client with @vue/apollo-composables. It only breaks in tests, not dev/production builds, and only when clientId is set manually (including literal "default" value) in the useQuery call.

Expected behavior
It just works?

Versions
vue: ^3.3.13
vue-apollo: ^4.0.1
@apollo/client: ^3.9.4
@vue/apollo-composable: ^4.0.1

Additional context
After hours of searching for information and debuggin the code, I think I found the source of the issue:

const providedApolloClients: ClientDict<TCacheShape> | null = inject(ApolloClients, null)
from the code it's see that by default it just sets the clients dict to null and for some reason decides to use that first to see if it can find the client. But because providedApolloClients is null, the resolveClientWithId function throws an error (
throw new Error(`No apolloClients injection found, tried to resolve '${clientId}' clientId`)
), making this check ( ) obsolete and not going forward with checking savedCurrentClients, which provideApolloClients actually sets, while resolveDefaultClient, which gets called in cases where clientId isn't defined, doesnt throw but instead just uses undefined (
: (providedApolloClient ?? undefined)
) and allows the logic to continue with accessing savedCurrentClients (
return resolveDefaultClient(savedCurrentClients, savedCurrentClients.default)
), where it ultimately gets the default client.

Proposed solution
Don't throw in resolveClientWithId and use a similar hack like in resolveDefaultClient, or throw the error after both resolvers fail, not after the first one.

EDIT: pretty sure it's the same issue as here, although I might be wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant