question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add or modify a response header in ApolloServer?

See original GitHub issue

I am using graphql ApolloServer and using following for Apolloserver

server.applyMiddleware({ app, path: '/graphql' });

And I need to pass the error returned from the resolvers in the response header.

I read through the docs but looks like we cannot add another middleware after the above-mentioned middleware.

I also tried adding a formatResponse while initializing the server but the object here is not the actual http response where i can change the error header.

const server = new ApolloServer({
  schema,
  validationRules: [depthLimit(7)],
  playground: process.env.NODE_ENV !== 'production',
  debug: process.env.NODE_ENV !== 'production',
  formatError: err => {
    // Don't give the specific errors to the client.
    if (err.message.startsWith('Database Error: ') || err.message.startsWith('connect')) {
      return new Error('Internal server error');
    }

    // Otherwise return the original error. The error can also
    // be manipulated in other ways, so long as it's returned.
    return err;
  },
  formatResponse: (res:any,options:any) => {

   // can't set headers here as it is not the http response object.

    return res;
  }
});

I need this because I have an Axios interceptor set up in the front end and it checks the header of the response to function and currently Apollo server returns 200 even when an error occurs.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
trevor-scheercommented, Sep 9, 2019

Hey @SAGARACH65, great question! Our plugin API supports this, it just isn’t documented yet.

The interfaces are a great place to look for now if you’d like to know what various things a plugin is capable of.

A small example might look something like:

const server = new ApolloServer({
  // ...,
  plugins: [
    {
      requestDidStart() {
        return {
          didEncounterErrors(requestContext) {
            requestContext.response.http.headers.set('Has-Errors', '1');
          }
        };
      }
    }
  ]
});

Hope this is helpful, if you have any further questions please let me know - and keep an eye out for some fresh docs in the future 👀 #2008

2reactions
bioniclescommented, Nov 23, 2019

Aha! Just what I was looking for. I want to set security headers in Vanilla Apollo Server

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add a response header in ApolloServer?
new ApolloServer({ context: ({res}) => { res.header('key', 'value') } }).
Read more >
API Reference: ApolloServer - Apollo GraphQL Docs
This article documents the ApolloServer class from the @apollo/server package. You can use the ApolloServer class to create an instance of Apollo Server...
Read more >
Accessing Authorization headers in Apollo graphql client
We can use HttpLink to send custom headers to in our requests (which we will do to authorize via 'Authorization' header), but there...
Read more >
API Reference: @apollo/gateway - Apollo Server - Netlify
The example below demonstrates adding an x-user-id HTTP header to every request the gateway sends to an implementing service:.
Read more >
apollo-server-plugin-http-headers - npm package - Snyk
In the past month we didn't find any pull request activity or change in issues status has been detected for the GitHub repository....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found