From 0f17f7c50e77089d564edbe972afd38266e7e8a7 Mon Sep 17 00:00:00 2001 From: Curtis Layne Date: Thu, 16 Aug 2018 14:26:39 -0400 Subject: [PATCH] Pass payload into the context function for subscriptions (#1513) * Pass payload into the context function for subscriptions The payload can be used for things like auth. You can pass a token on the payload of the message and use it to fetch the user. * Update CHANGELOG.md --- CHANGELOG.md | 1 + packages/apollo-server-core/src/ApolloServer.ts | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b76a881b8f..e1cf5d59ad0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All of the packages in the `apollo-server` repo are released with the same versi ### vNEXT +- pass payload into context function for subscriptions [#1513](https://github.com/apollographql/apollo-server/pull/1513) - Add option to mock the entire schema(i.e. sets preserveResolvers) [PR #1546](https://github.com/apollographql/apollo-server/pull/1546) ### v2.0.2 diff --git a/packages/apollo-server-core/src/ApolloServer.ts b/packages/apollo-server-core/src/ApolloServer.ts index cc9327831f2..9e7290e878c 100644 --- a/packages/apollo-server-core/src/ApolloServer.ts +++ b/packages/apollo-server-core/src/ApolloServer.ts @@ -291,7 +291,10 @@ export class ApolloServerBase { ? onConnect : (connectionParams: Object) => ({ ...connectionParams }), onDisconnect: onDisconnect, - onOperation: async (_: string, connection: ExecutionParams) => { + onOperation: async ( + message: { payload: any }, + connection: ExecutionParams, + ) => { connection.formatResponse = (value: ExecutionResult) => ({ ...value, errors: @@ -306,7 +309,7 @@ export class ApolloServerBase { try { context = typeof this.context === 'function' - ? await this.context({ connection }) + ? await this.context({ connection, payload: message.payload }) : context; } catch (e) { throw formatApolloErrors([e], {