Skip to content

Commit

Permalink
Pass payload into the context function for subscriptions (#1513)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
clayne11 authored and evans committed Aug 16, 2018
1 parent efc5302 commit 0f17f7c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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], {
Expand Down

0 comments on commit 0f17f7c

Please sign in to comment.