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

Closed
tukanoidd opened this issue Jun 5, 2024 · 1 comment · Fixed by #1570, Shoutzor/frontend#175, Shoutzor/frontend#179 or Shoutzor/frontend#181

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

@svelue
Copy link

svelue commented Aug 13, 2024

I have the same problem using
@apollo/client 3.11.4
@vue/composable 4.0.2
vue 3.4.37

It seems like everything is working as expected with at least vue 3.4.26 to I'm not sure if this is an apollo or vue issue?!

@Akryum Akryum closed this as completed in d141ed6 Aug 19, 2024
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

Successfully merging a pull request may close this issue.

2 participants