Graphql-yoga: Add Custom PubSub Implementation

Created on 7 Mar 2018  路  4Comments  路  Source: dotansimha/graphql-yoga

I have been trying to port an existing project to graphql-yoga and I'm loving the simplicity of everything so far. However I'm having trouble adding a custom PubSub implementation for my subscriptions.

Is it possible to use something like graphql-redis-subscriptions instead of the default implementation which is from graphql-subscriptions?

kinquestion

Most helpful comment

That's a great point @mjdickinson. Currently PubSub is indeed just exposed as convenience but it might make to remove this convenience to keep things simpler.

@2wce pls ignore my previous comment, @mjdickinson is right, this should already be possible like the following:

import { GraphQLServer } from 'graphql-yoga'
import { RedisPubSub } from 'graphql-redis-subscriptions'

const pubsub = new RedisPubSub()
const SOMETHING_CHANGED_TOPIC = 'something_changed'

const typeDefs = `
  type Query {
    hello(name: String): String!
  }

  type Subscription {
    somethingChanged: String!
  }
`

export const resolvers = {
  Query: {
    hello: (_, { name }) => `Hello ${name || 'World'}`,
  },
  Subscription: {
    somethingChanged: {
      subscribe: () => pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC),
    },
  },
}

// trigger dummy event every 200ms
setInterval(
  () => pubsub.publish(SOMETHING_CHANGED_TOPIC, { somethingChanged: "I'm a test event" }),
  200
)

const server = new GraphQLServer({ typeDefs, resolvers })
server.start(() => console.log('Server is running on localhost:4000'))

Please let us know if this didn't work or we misunderstood your question 馃檪

All 4 comments

That's a great point @2wce. This is not yet possible but I agree that it should be supported.

Do you have some thoughts on how the API should look like?

Wouldn't you just use graphql-redis-subscriptions instead of PubSub exported by graphql-yoga? As far as I can tell, graphql-yoga is only bundling and exporting graphql-subscriptions as a convenience - it doesn't actually depend upon it.

Am I missing something?

That's a great point @mjdickinson. Currently PubSub is indeed just exposed as convenience but it might make to remove this convenience to keep things simpler.

@2wce pls ignore my previous comment, @mjdickinson is right, this should already be possible like the following:

import { GraphQLServer } from 'graphql-yoga'
import { RedisPubSub } from 'graphql-redis-subscriptions'

const pubsub = new RedisPubSub()
const SOMETHING_CHANGED_TOPIC = 'something_changed'

const typeDefs = `
  type Query {
    hello(name: String): String!
  }

  type Subscription {
    somethingChanged: String!
  }
`

export const resolvers = {
  Query: {
    hello: (_, { name }) => `Hello ${name || 'World'}`,
  },
  Subscription: {
    somethingChanged: {
      subscribe: () => pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC),
    },
  },
}

// trigger dummy event every 200ms
setInterval(
  () => pubsub.publish(SOMETHING_CHANGED_TOPIC, { somethingChanged: "I'm a test event" }),
  200
)

const server = new GraphQLServer({ typeDefs, resolvers })
server.start(() => console.log('Server is running on localhost:4000'))

Please let us know if this didn't work or we misunderstood your question 馃檪

I am getting the following error when i use redis with graphql-yoga

{
"error": {
"name": "MaxRetriesPerRequestError",
"message": "Reached the max retries per request limit (which is 20). Refer to \"maxRetriesPerRequest\" option for details."
}
}

this occurs when its graphql server is managed by PM2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ramonmulia picture ramonmulia  路  3Comments

SebastianEdwards picture SebastianEdwards  路  4Comments

frederikhors picture frederikhors  路  4Comments

AhmadEl-Banna picture AhmadEl-Banna  路  5Comments

woss picture woss  路  5Comments