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

Unhandled Errors with Query SSR (Next.js) #271

Open
apjoseph opened this issue Aug 25, 2020 · 4 comments
Open

Unhandled Errors with Query SSR (Next.js) #271

apjoseph opened this issue Aug 25, 2020 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@apjoseph
Copy link

It looks like GraphQLClient is leaving server errors unhandled instead of passing them to the query result:

This can be reproduced easily by simply running the 5-nextjs example from this repo with the Apollo server middleware in index.js commented out. The 404 error is left unhandled instead of being displayed as text in the component.

export const AllTodosView = observer(() => {
  const { error, data, loading, query } = useQuery(store  => {
    return store.queryTodos({}, todoSelector)
  })
  if (error) return error.message
  if (!data) return "Loading..."
  return (
    <>
      {data && <TodosList todos={data.todos} />}
      {loading ? (
        "Loading..."
      ) : (
        <button onClick={query!.refetch}>Refetch</button>
      )}
    </>
  )
})
@apjoseph apjoseph changed the title Unhandled Errors GraphQL Client throws Unhandled Errors Aug 25, 2020
@apjoseph
Copy link
Author

Looks like this issue is being caused by __pushPromise on MSTGQLStore. Promise resolves and page renders correctly, if this this line is commented out. Am a bit perplexed as to how this is happening since the error is caught and handled within the chain.

@odjhey
Copy link

odjhey commented Sep 1, 2020

mst-gql/src/Query.ts

Lines 117 to 124 in 926d8a7

promise = promise.then((data: any) => {
// cache query and response
if (this.fetchPolicy !== "no-cache") {
this.store.__cacheResponse(this.queryKey, this.store.deflate(data))
}
return this.store.merge(data)
})
this.promise = promise

im not sure of the right solution but i have patched mine to add a catch block for this promise that looks like so

 promise = promise.then((data: any) => { 
   // cache query and response 
   if (this.fetchPolicy !== "no-cache") { 
     this.store.__cacheResponse(this.queryKey, this.store.deflate(data)) 
   } 
   return this.store.merge(data) 
- })
+ }).catch((error) => {
+   this.loading = false
+   this.error = error
+ })
 this.promise = promise 

let me know if this solves your issue.

@apjoseph apjoseph changed the title GraphQL Client throws Unhandled Errors GraphQL Client throws Unhandled Errors (serverside - next.js) Sep 3, 2020
@apjoseph apjoseph changed the title GraphQL Client throws Unhandled Errors (serverside - next.js) Unhandled Errors with Query SSR (Next.js) Sep 3, 2020
@apjoseph
Copy link
Author

apjoseph commented Sep 3, 2020

@odjhey that doesn't have any impact

@Benz19 Benz19 added the bug Something isn't working label Nov 7, 2022
@Benz19
Copy link
Collaborator

Benz19 commented Nov 7, 2022

Hi @apjoseph Im trying to help clean up all the open issues.. Did you still have this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants