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

Multiple calls to useAsyncQuery from composable function causes 500 error in SSR #614

Open
pixleight opened this issue Apr 11, 2024 · 2 comments
Labels

Comments

@pixleight
Copy link

Environment


  • Operating System: Linux
  • Node Version: v18.18.0
  • Nuxt Version: 3.11.2
  • CLI Version: 3.11.1
  • Nitro Version: 2.9.6
  • Package Manager: npm@10.2.3
  • Builder: -
  • User Config: devtools, modules, apollo
  • Runtime Modules: @nuxtjs/apollo@5.0.0-alpha.14
  • Build Modules: -

Describe the bug

Calling useAsyncQuery more than once inside a composable function results in a 500 error when loading a SSR route, with the following error message:

[nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function. This is probably not a Nuxt bug. Find out more at `https://nuxt.com/docs/guide/concepts/auto-imports#vue-and-nuxt-composables`.

Expected behaviour

Should be able to execute more than a single query in a composable function.

Reproduction

https://stackblitz.com/edit/github-tfy4rs?file=composables%2FuseMultipleQuery.ts

Example composable function:

export const useMultipleQuery = async () => {
  const dataOne = ref();
  const dataTwo = ref();

  const responseOne = await useAsyncQuery(gql`
    query Launches {
      launches {
        id
        mission_name
      }
    }
  `);
  dataOne.value = responseOne.data.value;

  const responseTwo = await useAsyncQuery(gql`
    query Rockets {
      rockets {
        id
        description
      }
    }
  `);
  dataTwo.value = responseTwo.data.value;

  return {
    dataOne,
    dataTwo,
  };
};

Additional context

Executing multiple queries with useAsyncQuery works as expected when inside <script setup> or loading a route in client-side context.

Logs

500

[nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function. This is probably not a Nuxt bug. Find out more at `https://nuxt.com/docs/guide/concepts/auto-imports#vue-and-nuxt-composables`.

at useApollo (./node_modules/@nuxtjs/apollo/dist/runtime/composables.mjs:75:41)
at prep (./node_modules/@nuxtjs/apollo/dist/runtime/composables.mjs:26:23)
at Module.useAsyncQuery (./node_modules/@nuxtjs/apollo/dist/runtime/composables.mjs:16:32)
at Module.useMultipleQuery (./composables/useMultipleQuery.ts:19:51)
at async setup (./app.js:19:12)
@pixleight pixleight added the bug label Apr 11, 2024
@filiphazardous
Copy link
Contributor

I also ran into this. I solved it by wrapping things in useNuxtApp().runWithContext(async () => { /* my code here */ })

I think this is more like a limitation of Nuxt, perhaps? After awaiting an async call, it's like I have been transported out of the usual Nuxt context and into a then-function of a Promise. So context probably has to be re-established.

@ronaldevers
Copy link

Relevant Nuxt documentation:

The context is unset by Nuxt/Vue after the first await leading to the error on the second.

I think you can either use runWithContext as suggested by @filiphazardous or for for example multiple await $fetch() calls, I've wrapped them in an async handler function that I use with useAsyncData(handler, ...).

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

No branches or pull requests

3 participants