apollo-server-testing on server error catch block not executed

Created on 24 Feb 2021  路  8Comments  路  Source: apollographql/apollo-server

when an error raised on the server. apollo-server-testing not throw it.

Most helpful comment

By "the client" are you describing the Apollo Client behavior? That is only the default behavior with errorPolicy: none.

If you'd like a helper that throws if any errors are returned, it's easy to write. I'll reiterate that apollo-server-testing is merely an ostensibly helpful wrapper around server.executeOperation and you can pretty easily write your own wrapper like

async function executeOrThrow(server: ApolloServer, request: GraphQLRequest) {
  const response = server.executeOperation(request);
  if (response.errors?.length) {
    throw new GraphQLResponseHasErrors(request);
  }
  return response;
}

All 8 comments

Hi @EhsanSarshar. In order for this to be an actionable bug report, we're going to need a much more clear explanation of what you tried, what you expected, and what you saw instead. This is most productive as a fully self-contained reproduction, like a git repo I can clone or a codesandbox.io demo. There's not enough information in your post for me to understand your concern, so I'm going to close this issue for now. I'm happy to reopen it if you provide a reproduction!

@glasser I think it's by design.
when the server throws an error by api call, the catch block must be triggered.

a sample

 try {
     const result = await mutata({...something in here})

    if (result. errors) {
    // this block executed when errors occurred.
    // instead of catch block
    }
} catch (e) {
     console.log(e.message)
}

As mentioned, I need a full reproduction to be helpful, not just a small code snippet.

client.mutation is returning an object that indicates Error, but it is not throwing

I think this is (as you suggested) working as designed. You're getting back the same kind of object that would be sent to the client, which may contain errors on a subfield.

It would probably be better if this were actually documented! I'd be happy to review a PR for docs/source/testing/testing.md.

@glasser if the client receive any error it receive it inside catch block not result.
if it's by design it should have a flexible way to check for errors. like function chaining.
you can have a look on super-test library.

By "the client" are you describing the Apollo Client behavior? That is only the default behavior with errorPolicy: none.

If you'd like a helper that throws if any errors are returned, it's easy to write. I'll reiterate that apollo-server-testing is merely an ostensibly helpful wrapper around server.executeOperation and you can pretty easily write your own wrapper like

async function executeOrThrow(server: ApolloServer, request: GraphQLRequest) {
  const response = server.executeOperation(request);
  if (response.errors?.length) {
    throw new GraphQLResponseHasErrors(request);
  }
  return response;
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mathroc picture mathroc  路  3Comments

danilobuerger picture danilobuerger  路  3Comments

manuelfink picture manuelfink  路  3Comments

veeramarni picture veeramarni  路  3Comments

nevyn-lookback picture nevyn-lookback  路  3Comments