Error Policy in Apollo Client React seems doesn't work

1

I have a problem when test Apollo. When I try to query with apollo and graphql, i want response return error and partial data, so I set property errorPolicy:‘all’. But it’s not work. I don’t know why? Help, please! Here my code:

query {
animal { name age },
school { name numberfd }
}

const { loading,data,error} = useQuery(GET_DASHBOARD_DATA, {
errorPolicy:‘all’,
onCompleted: (res) => {console.log(“complete”,res)},
onError : (res,data) => {console.log(“ERRRR”,res,data)},
})

and I want to receive something like (both error and data):
{ error:[…], data:[animal:[…]] }

but it’s only a response error. Here is Apollo’s doc: Handling operation errors - Apollo GraphQL Docs
Did I miss something?

@qtruongbk in your code sample, you’re logging data in the onError callback. The useQuery onError callback doesn’t pass data in as a second param, so in this line of your code:

onError : (res,data) => {console.log(“ERRRR”,res,data)},

data is always going to be undefined. onError callbacks only receive one param, the error itself. If you console.log(data) after your useQuery call, you should see partially loaded data.

@hwillson , Thanks for reply, but I’m sure console.log after useQuery call, still undefined.

Thanks for the additional details. You’re encountering a 400 error which means the server can’t return any data, because it doesn’t understand your query. The “all” errorPolicy will only return partial data if it gets some data back from the server. In this case a broken query doesn’t count. If you sent in a valid query but there was an error trying to run the resolver function for one of the fields in a selection set for example, then you would get partial data back.

@hwillson Thanks,
But I don’t really understand

run the resolver function for one of the fields in a selection

Does it mean some thing can wrong in resolver function and graphq still return an error? graphql not sync?

I’d suggest reading/re-reading the Apollo Client error handling docs. When you send a query to Apollo Server, one of the first things it does is validate the query against your schema. In your case the query is invalid, since it includes a field that doesn’t exist in your schema. At this point Apollo Server can’t run the query, since it’s invalid, so it has to let you know there was a problem. It does this by returning an HTTP status code of 400 along with some error details. It can’t return any data, as it wasn’t able to run your query to get data. If your query was valid however, but there was an error trying to resolve parts of your query on the server side, Apollo Server would return an HTTP status of 200, along with error details, and whatever data it was able to resolve.

@hwillson , I understand, but I think the doc needs more detail for a newbie and send a big thank’s to you!

2 Likes

Ahhh I see what you mean @qtruongbk. We should update that to show by badField we mean a field that will produce an error when resolving, not an invalid query field. Thanks! cc @StephenBarlow

2 Likes