Context function error handling for batched queries

Hi,

I am currently using batched queries in my project and in the docs in your website to shows an example on how to add then auth process in the context function so that its validated once for all batched queries.

This is fine, but in my case I need to only validate and throw an error for specific queries while the rest should still continue.

I am not sure how to handle this.

Are there any examples or where should I add the errors I want to throw for the specific queries?

For example:

In req.body I get

[
	{
		"operationName": "Query1",
		"query": "query Query1 { query1 }"	
	},
	{
		"operationName": "Query2",
		"query": "query Query1 { query2 }"	
	}
]	

I only want query2 to throw an error if the authentication validation fails.

BatchHttpLink is always going to put all of the queries in to the same request and will provide the first operation’s context to the rest of the operations.

You most likely would need to tweak that code to support what you’re trying to do. You can also provide multiple links to customize things the way you want.

If you have more questions about this, you can join our Apollo Discord Server and talk more about your use case.

1 Like

Thanks for your reply!
My issue isn’t on the client-side but on the @apollo/server side.

In the test implementation that I had, upon authentication failiure throw a GraphQLError.
But this would throw the error for all queries in a batch request.

What I actually wanted or thought was possible, was to return the error for only selected queries that I decide.

I thought I could use something like req.body.errors and push the index or something like that.
I tried but it didn’t work.

The only way I could achieve this was use a middleware, which is executed per query even in batch queries.

So I moved my authentication process to a middleware and I identify which query to skip based on the info.fieldName.

If there is a better solution I appreciate the suggestions.