nuxt-modules / apollo

Nuxt.js module to use Vue-Apollo. The Apollo integration for GraphQL.
https://apollo.nuxtjs.org
MIT License
929 stars 195 forks source link

Error to resolve ApolloClients #488

Closed zoedsoupe closed 4 months ago

zoedsoupe commented 1 year ago

Environment

Describe the bug

I have this config at nux.config.ts:

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  apollo: {
    autoImports: true,
    authType: "Bearer",
    authHeader: "Authorization",
    tokenStorage: "cookie",
    proxyCookies: true,
    clients: {
      default: {
        httpEndpoint: "https://pescarte.uenf.br/api",
      },
      local: {
        httpEndpoint: "http://localhost:4000/api",
      },
    },
  },
  // ...
});

and this is my setup at my login page:

<script setup lang="ts">
const query = gql`
  mutation Login($cpf: String!, $password: String!) {
    login(cpf: $cpf, password: $password) {
      token
      user {
        id
        birthdate
        firstName
        middleName
        lastName
        cpf
        role
      }
    }
  }
`;

const { mutate: login } = useMutation<Login>(query, { clientId: "local" });

async function handleSubmit(values) {
  const { onLogin } = useApollo();
  const { cpf, password } = values;

  const variables = { cpf, password };

  const result = await login(variables);

  console.log(result);
}
</script>

However when the login form is submitted, it occurs this error: Uncaught (in promise) Error: No apolloClients injection found, tried to resolve 'local' clientId

Expected behaviour

ApolloClient should be resolved and queries/mutations executed normally

Reproduction

No response

Additional context

If I try to use the default ApolloClient it also fails with the same error.

The whole code can be found at: https://github.com/peapescarte/plataforma

Logs

useApolloClient.ts:27 

       Uncaught (in promise) Error: No apolloClients injection found, tried to resolve 'local' clientId
    at resolveClientWithId (useApolloClient.ts:27:11)
    at resolveImpl (useApolloClient.ts:51:24)
    at resolveClient (useApolloClient.ts:66:20)
    at mutate (useMutation.ts:78:20)
    at handleSubmit (acessar.vue:69:3)
    at vee-validate.js:1802:1
resolveClientWithId @ useApolloClient.ts:27
resolveImpl @ useApolloClient.ts:51
resolveClient @ useApolloClient.ts:66
mutate @ useMutation.ts:78
handleSubmit @ acessar.vue:69
(anonymous) @ vee-validate.js:1802
Promise.catch (async)
callWithAsyncErrorHandling @ chunk-3NMN3MUW.js:1590
invoker @ chunk-3NMN3MUW.js:8198
zoedsoupe commented 1 year ago

Noticed that this address #444