Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relay-nextjs with Subscriptions - Live Queries #54

Closed
AngelLoredoAlvarez opened this issue Mar 9, 2022 · 3 comments
Closed

relay-nextjs with Subscriptions - Live Queries #54

AngelLoredoAlvarez opened this issue Mar 9, 2022 · 3 comments

Comments

@AngelLoredoAlvarez
Copy link

I was wondering if it is possible to make use of Subscriptions - Live Queries with graphql-ws, they offer an example using Relay Modern, but I am not pretty sure that their implementation will work properly with Next and SSR.

@rrdelaney
Copy link
Member

Although I haven't tested this functionality myself, it definitely seems possible from reading the docs! relay-nextjs doesn't have any say on how you structure your network, so it should work as normal.

@petrbela
Copy link

Just add it to your client environment like so:

// getClientEnvironment.ts
import { createClient } from 'graphql-ws'
import { getRelaySerializedState } from 'relay-nextjs'
import { withHydrateDatetime } from 'relay-nextjs/date'
import {
  Environment,
  Network,
  Observable,
  RecordSource,
  Store,
} from 'relay-runtime'

export function createClientNetwork() {
  const ws = process.env.NEXT_PUBLIC_API_URL?.replace('http', 'ws')
  const wsClient = createClient({ url: ws + '/graphql' })

  return Network.create(
    async (params, variables) => {
      const response = await fetch(
        `${process.env.NEXT_PUBLIC_API_URL}/graphql`,
        {
          method: 'POST',
          credentials: 'include',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            query: params.text,
            variables,
          }),
        }
      )

      const json = await response.text()
      return JSON.parse(json, withHydrateDatetime)
    },
    (operation, variables) =>
      Observable.create((sink) =>
        wsClient.subscribe(
          {
            operationName: operation.name,
            query: operation.text!,
            variables,
          },
          sink as any
        )
      )
  )
}

let clientEnv: Environment | undefined

export function getClientEnvironment() {
  if (typeof window === 'undefined') return null

  if (clientEnv == null) {
    clientEnv = new Environment({
      network: createClientNetwork(),
      store: new Store(new RecordSource(getRelaySerializedState()?.records)),
      isServer: false,
    })
  }

  return clientEnv
}

@rrdelaney
Copy link
Member

@petrbela Thanks for the example! Knowing that relay-nextjs doesn’t get in the way of using this feature, I’m going to close this issue. Please re-open if it doesn’t work for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants