apollo-server-integrations / apollo-server-integration-aws-lambda

An integration to use AWS Lambda as a hosting service with Apollo Server
MIT License
46 stars 9 forks source link

Versioning in the Accept header is returning a error #89

Closed chinmayghotkar closed 1 year ago

chinmayghotkar commented 1 year ago

Hi,

We recently made the upgrade to Apollo V4 and started using this integration. When we make requests to our server, we are passing an accept header that looks like this:

'accept': 'application/json;v=2'

but we receive an error message in the response:

"An 'accept' header was provided for this request which does not accept application/json; charset=utf-8 or application/graphql-response+json; charset=utf-8"

we have a workaround in our validation middleware that overrides the the accept header with:

    // overwrite accept header - apollo server fails with application/json;v2
    event.headers['accept'] = 'application/json'

And this workaround seems to allow for us to make graphql requests.

The apollo server initialization:

const server = new ApolloServer({
  schema,
  csrfPrevention: true,
  cache: 'bounded',
  introspection: !!process.env.IS_LOCAL
})

Was wondering if we are missing some configuration that we need to handle as part of initializing the startServerAndCreateLambdaHandler()or when initializing the apollo server? We can use the workaround but we would prefer to implement in the correct way.

Edit:

We are also passing

'content-type': 'application/json', with the headers

BlenderDude commented 1 year ago

This seems like it might be an issue with the main @apollo/server package. This library doesn't look at or modify the Accept header, so I assume this is on @apollo/server. I recommend creating an issue there.

chinmayghotkar commented 1 year ago

Thanks for replying! I see, I will go ahead and create an issue there!

trevor-scheer commented 1 year ago

@chinmayghotkar 👋 Apollo Server maintainer here. AS4 is more particular about accept headers / content type negotiation. You should be able to bypass our content type negotiation altogether by setting the content-type header yourself in a plugin like so:

{
  async requestDidStart({ response }) {
    response.http?.headers.set(
      'content-type',
      'application/json;v=2',
    );
  },
},

Let me know if this helps!

chinmayghotkar commented 1 year ago

@trevor-scheer Hi Trevor, Thanks for the replying! Is the code snippet you sent for response headers or request headers? We are having issues with the request header with the

'accept': 'application/json;v=2'

We are getting a graphql error, so we arent sure if the request header is causing the issue or something else. With the workaround above, we are able to hit the query resolvers.

This is the error we are getting: {"errors": [{"extensions": {"code": "BAD_REQUEST", "stacktrace": ["BadRequestError: An 'accept' header was provided for this request which does not accept application/json; charset=utf-8 or application/graphql-response+json; charset=utf-8", " at new GraphQLErrorWithCode

trevor-scheer commented 1 year ago

@chinmayghotkar Good question. I know it seems counter-intuitive. In Apollo Server, we only inspect your accept request header if the content-type header has not been set on the response yet (so that we can determine how to set it).

Here's the relevant code snippet where you can see we bypass the negotiation altogether if the response header is already set.

chinmayghotkar commented 1 year ago

I see! Thanks for the explanation!