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

Error/redirection functionality for Smart Queries error hook #381

Open
it-sha opened this issue Feb 10, 2021 · 7 comments
Open

Error/redirection functionality for Smart Queries error hook #381

it-sha opened this issue Feb 10, 2021 · 7 comments

Comments

@it-sha
Copy link

it-sha commented Feb 10, 2021

What problem does this feature solve?

Hook asyncDate({error}){...} has the option to handle errors with redirection to the error page that works fine in client-side and server-side.

For Appollo Smart Queries it seems like we have no options to do that on server side
We have this problem from 2017 (ex. #42) but it seems like we have no solution for smart query and have to use code like

async asyncData({error, app, params}){
  const article = await app.apolloProvider.defaultClient.query({
    query: `someGql`,
    variables:{id: params.id}
  }).then(({data}) => data && data.Article)
  if(!article){
    return error({statusCode: 404, message: 'Not found'})
  }
  return {article}
}

but Smart Query is a much more elegant solution and gives many benefits.

What does the proposed changes look like?

apollo: {
    queryName: {
        query: someGql,
        update: (data) => data.allPets,

        error({ errorData, redirect }) {
            const statusCode = errorData.networkError.statusCode;
            if(statusCode = 404){
                redirect('/404')
            }
        },
    },
},
This feature request is available on Nuxt community (#c364)
@it-sha
Copy link
Author

it-sha commented Feb 11, 2021

Hi, @dohomi . Thank you for your help in #42

Could you please give any advice?
Do we have in 2021 any possibility to handle errors with Apollo Smart Query with redirection to 404 page that works not only for client-side but also for server-side rendering?

@jamesmorgan
Copy link

Also suffering from this problem - tried pretty much ever trick I can find on google to fix it. Only thing which works is not using smart queries and using asyncData with a catch/handler and redirect.

My use case is when hitting a 3rd party GQL endpoint, this something goes does and then fails with a Unexpected token < in JSON at position 0 as the service is returning some non JSON error page.

Atm when it happens makes the app fall over with a big bang, even with a custom error handler I cannot seem to stop it and doing a redirect also doesnt work, like the error to lower down the stack and non recoverable.

To avoid this I have moved all the queries to using asyncData but its a massive refactoring effort.

Would love to be able to properly handle these issues and still use a smart query.

@toddpadwick
Copy link

There are so many issues around Smart Queries and SSR error handling but no real solution that I can find 😔 .
As far as I understand, the main reason for opting for Nuxt is its SSR benefits (for SEO reasons). So unless I am missing something, Apollo Smart Queries are not fit for purpose.

We have a huge Nuxt app that we've been building and so I am dreading having to refactor all our smart queries into asyncData ones.

@jamesmorgan Did you end up refactoring or did you find a way to make Smart Queries work? If you did end up using async data it would be massively helpful if you could share an example. I'm not very familiar with using it.

@reinoldus
Copy link
Contributor

For those waiting for a fix: Probably not going to happen with nuxt2. I assume it's related to a similar issue about the fetch hook: nuxt/nuxt#7285

The reason why the fetch hook can't return 404 errors:
image

@jamesmorgan
Copy link

@jamesmorgan Did you end up refactoring or did you find a way to make Smart Queries work? If you did end up using async data it would be massively helpful if you could share an example. I'm not very familiar with using it.

@toddpadwick Unfortunately we changes all our smart queries to inline queries which we can than handle better when things go wrong. A rough example is along the lines of this:

    this.$apollo.addSmartQuery('mySmartProp', {
      client: this.client,
      query: MY_QUERY,
      error(error, vm, key, type, options) {
        // Handle when things fail
      }
    });

It was a fairly painful experience refactoring this but once its done its okay from our experience.

@reinoldus
Copy link
Contributor

@jamesmorgan in what hook do you add the queries?

Does it work fine with $apollo.queries.loading in the template?

@cschweda
Copy link

I have a similar question -- if an Apollo query returns 0 results from a page slug -- how do I redirect to an error page?

Using Nuxt (latest) and Apollo.

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

5 participants