Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add or modify a response header in ApolloServer? #3264

Closed
SAGARACH65 opened this issue Sep 8, 2019 · 6 comments
Closed

How to add or modify a response header in ApolloServer? #3264

SAGARACH65 opened this issue Sep 8, 2019 · 6 comments

Comments

@SAGARACH65
Copy link

SAGARACH65 commented Sep 8, 2019

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.

@trevor-scheer
Copy link
Member

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

@SAGARACH65
Copy link
Author

Thanks, @trevor-scheer. It pretty much solves my problem.

@bionicles
Copy link

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

@trevor-scheer
Copy link
Member

trevor-scheer commented Nov 30, 2019

An update for anyone who happens upon this issue, the plugin docs have been up for a bit now. Thanks to @abernix for all of his hard work on these 👏

@listaction
Copy link

@trevor-scheer : thank you for linking the doc! I am able to use plugins , however if the upstream (whatever apollo connects to get a response) , has a http header, I am unable to see that header value in the response object (inside the requestContext).

So for example, if the backend returned a xyz : value header that should be returned to the client , I would like to pass this to the client:
willSendResponse(requestContext) { console.log(requestContext.response.http.headers.get('xyz');//this is null }

@pradeep2987
Copy link

Thanks @trevor-scheer

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants