Apollo-server: Subscriptions in 2.0

Created on 11 Jun 2018  路  4Comments  路  Source: apollographql/apollo-server

Hi all,
I'm following the guide for subscriptions and it work well...I'm just wondering if the guide is related for 1.0 or it's good for 2.0 as well? This is what I had to do in my server.ts:
(that open the old playground as well)

export const pubsub = new PubSub();

const schema: GraphQLSchema = mergeSchemas({
    schemas,
    resolvers
});

// GraphQL
const server = new ApolloServer({
    schema,
    context: async ({ req }: any) => {
    ....
    },
    tracing: true
});

server.listen().then(({ url }) => {
    console.log(`馃殌 Server ready at ${url}`);
});

/*****************************************************/
/*********      SUBSCRIPTIONS        **********/
/*****************************************************/
const PORT = 5000;
const app = express();

app.use('*', cors({ origin: 'http://localhost:5000' }));

app.use('/graphql', bodyParser.json(), graphqlExpress({
  schema
}));

app.use('/graphiql', graphiqlExpress({
  endpointURL: '/graphql',
  subscriptionsEndpoint: `ws://localhost:5000/subscriptions`
}));

// We wrap the express server so that we can attach the WebSocket for subscriptions
const ws = createServer(app);

ws.listen(PORT, () => {
  console.log(`GraphQL Server is now running on http://localhost:${PORT}`);

  // Set up the WebSocket for handling GraphQL subscriptions
  new SubscriptionServer({
    execute,
    subscribe,
    schema
  }, {
    server: ws,
    path: '/subscriptions',
  });
});

Most helpful comment

Perhaps this is fullstack example helpful for people looking for how to set up and use subscriptions with Apollo Server 2 and Apollo Client.

All 4 comments

Related to this question - Is there an end-to-end example posted somwhere for using ApolloServer 2 that includes queries, mutations and subscriptions - over https and wss?

@johnreynoldsventure for the query and mutations you can have a look on my blog: www.dzurico.com for subscriptions I'm stack on the documentation really unclear

Perhaps this is fullstack example helpful for people looking for how to set up and use subscriptions with Apollo Server 2 and Apollo Client.

@daniele-zurico @rwieruch thanks for those examples. Curious if you've seen or have any debugging ideas for an issue where the connection appears to be open (in playground), but the subscription does not get pushed to the client (a similar connection to the source graphql service receives the payload).

Was this page helpful?
0 / 5 - 0 ratings