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.

Not able to access response object from context in apollo-server-fastify

See original GitHub issue

Previously I was using apollo-server-express and I wanted to migrate to fastify so I change it to apollo-server-fastify. But it started giving lots of issues.

One of such issue is - Not able to access res object from context function. Below code was working perfectly but res object itself was not available with apollo-server-fastify

context: async ({ req, res, connection }) => {
// res <--------- undefined
    if (connection) {
      return {
        models,
      }
    }
    if (req) {
      const me = await getMe(req, res)
      return {
        models,
        me,
        secret: process.env.SECRET,
      }
    }
  },

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
blacksmoke26commented, Aug 18, 2019

Yeah, the same issue but you can pass reply instance manually.

File plugins/fastify-apollo-server/index.js

const fp = require('fastify-plugin');
const {ApolloServer} = require('apollo-server-fastify');

// **** Plugin: plugins/fastify-apollo-server/index.js
module.exports = fp(async ( fastify, opts, next ) => {
  const server = new ApolloServer({
    /* ... */
    context: async request => {
      const {reply} = request;
      return {
        request,
        reply, // res: reply,
        app: fastify,
      };
    },
  });
  
  fastify.addHook('preHandler', async ( request, reply ) => {
    if ( String(request.req.url).startsWith('/graphql') ) {
      request.reply = reply;
    }
  });
  
  fastify.register(server.createHandler());
  
  next();
}, {
  name: 'fastify-apollo-server',
});

File: init-server.js

const app = require('fastify')();

// Plugins
app.register(require('plugins/fastify-apollo-server'));

(async function () {
  await app.listen(3000);
})();
2reactions
HW13commented, Apr 24, 2020

#3895 is awaiting review. In the meantime I’ve published a standalone version of apollo-server-fastify with the fix applied, here: https://github.com/autotelic/apollo-server-fastify.

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
Authentication and authorization - Apollo GraphQL Docs
Before we can correctly control access to data, we have to authenticate a user. There are many patterns for providing authentication credentials, including...
Read more >
Migrating to Apollo Server 4 - Apollo GraphQL Docs
The willSendResponse plugin hook receives an operation's requestContext , which has a response field containing a GraphQLResponse object. Note that the ...
Read more >
Apollo Server 4: a lightweight and easier-to-use Apollo Server
Apollo Server 4 is a lightweight, open-source, spec-compliant GraphQL server that's compatible with any GraphQL client.
Read more >
Context and contextValue - Apollo GraphQL Docs
By default, if your context function throws an error, Apollo Server returns that error in a JSON response with a 500 HTTP status...
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