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

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

Closed
techyrajeev opened this issue Aug 14, 2019 · 9 comments · Fixed by #3895
Closed

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

techyrajeev opened this issue Aug 14, 2019 · 9 comments · Fixed by #3895

Comments

@techyrajeev
Copy link

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,
      }
    }
  },
@blacksmoke26
Copy link

blacksmoke26 commented Aug 14, 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);
})();

@techyrajeev
Copy link
Author

@blacksmoke26 seems like kind of hack, but thats simplest solution as well.

@abernix
Copy link
Member

abernix commented Aug 26, 2019

Seems at least somewhat related to #2476.

@devin-fee-ah
Copy link

@abernix unfortunately, they're unrelated. If you look at the context param then it's documented that fastify receives nothing in the context... (which is inaccurate, it actually receives the req as context.

The linked issue is about express and what its context consists of.

@HW13
Copy link
Contributor

HW13 commented Mar 16, 2020

I've been having the same issue and was able to track down the cause. Should have a bug-fix PR up shortly, I'll be sure to link this issue. Meanwhile, if you'd like to check out the fix here it is -> autotelic#1

@MikeAliG
Copy link

Any update on this one?

@HW13
Copy link
Contributor

HW13 commented 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.

@benjsicam
Copy link

Waiting on #3895 to be merged. Planning to use it to set (JWT) authentication cookies.

@skozer
Copy link

skozer commented Jun 25, 2020

Would be great to see this merged! 🙂

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

Successfully merging a pull request may close this issue.

8 participants