FormidableLabs / next-urql

Convenience utilities for using urql with NextJS.
MIT License
56 stars 7 forks source link

How to set res.statusCode? #25

Closed ivan-kleshnin closed 4 years ago

ivan-kleshnin commented 4 years ago

Is there a way to set HTTP status code with Next-URQL (server-side)? How to do that?

Without a GraphQL client, when I manually invoked fetchGQL helper, I had something like:

function getInitialProps(ctx) {
  let resp = fetchGQL(...)

  if (resp.error) {
    if (!process.browser) {
      console.error(error)
      if (error.status) {
        ctx.res.statusCode = error.status // !!!
      }
    }
  }
}

Now I can't have access to ctx.res from a component function (serialization kills functions and circular objects) and I don't have access to results of fetching in getInitialProps. So basically I can't use that approach.

I guess it's possible to get HTTP response data via exchanges but I don't think I have access to ctx.res from where those are available. Please clarify.

kitten commented 4 years ago

Maybe the most effective way of doing this would be to utilise `react-ssr-prepass' again for this. I ran into this problem before and in theory one can add a hook that uses the result if it’s run server-side and triggers and throws an error. This can then be picked up by catching it with the prepass.

parkerziegler commented 4 years ago

👋 Hey @ivan-kleshnin we've moved next-urql over the urql monorepo. If you'd still like help on this issue or want to continue this discussion please raise it over in that repo and tag it with the next-urql tag. Thanks!