Skip to content

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

Closed
@tukanoidd

Description

@tukanoidd

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions