graphile / postgraphile-apollo-server

41 stars 5 forks source link

Support for Subscriptions? #4

Open JeffJankowski opened 5 years ago

JeffJankowski commented 5 years ago

Are postgraphile subscriptions supported using this method of hosting an apollo/graphile server?

I tried enabling simple subscriptions, as one would do with an express app, but I'm not seeing the Subscription or ListenPayload types in the schema:

const pluginHook = makePluginHook([PgPubsub]);
const { schema, plugin } = await makeSchemaAndPlugin(
  pgPool,
  'public',
  {
    pluginHook,
    subscriptions: true,
    simpleSubscriptions: true
  }
);
benjie commented 5 years ago

We've not added support for subscriptions yet (note that pluginHook only applies to the PostGraphile server and cannot be used with other servers); however you may be able to leverage the underlying subscription schema plugins (appendPlugins works in all the use-cases as it enhances the schema rather than the server): https://github.com/graphile/graphile-engine/blob/b6fbfb849ee50e29b6c53114cb159dc4f12ddd19/packages/pg-pubsub/src/index.ts#L5-L6

I think you'll need to pass in an extra couple things for it to work; to start with you could try calling this underlying hook method directly on your options:

https://github.com/graphile/graphile-engine/blob/b6fbfb849ee50e29b6c53114cb159dc4f12ddd19/packages/pg-pubsub/src/index.ts#L54-L225

so it might end up something like

const pluginHook = makePluginHook([PgPubsub]);
const baseOptions = {
    pluginHook,
    subscriptions: true,
    simpleSubscriptions: true
  };

const hookedOptions = pluginHook('postgraphile:options', baseOptions, { pgPool });

const { schema, plugin } = await makeSchemaAndPlugin(
  pgPool,
  'public',
  hookedOptions
);

No idea if this will work or not; let me know how you get on!

miguelocarvajal commented 4 years ago

Hey @benjie

I tried this and do see the Subscription in the schema.

I am having a problem with the JWT token though, it doesn't seem like it's being passed through.

Had to initialize Apollo Server like this:

const app = express();

const server = new ApolloServer({
    schema: ...,
    ...
  });

server.applyMiddleware({ app });

const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);

httpServer.listen(port);

https://www.apollographql.com/docs/apollo-server/data/subscriptions/#subscriptions-with-additional-middleware

Any ideas?

benjie commented 4 years ago

Is there a particular reason you need the Apollo Server rather than the PostGraphile server? This plugin doesn't support subscriptions currently, and I don't know how to extend it to support subscriptions (I've not looked into it).