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

feature: Support GraphQL Subscriptions on web side #8374

Merged
merged 9 commits into from
May 23, 2023

Conversation

dthyresson
Copy link
Contributor

@dthyresson dthyresson commented May 19, 2023

This PR includes changes needed to support use of GraphQL Subscriptions on the web side of a RedwoodJS project.

Note a project must be running via yarn rw serve and have the Redwood Fastify GraphQL Server properly configured (and be using Redwood canary for v6). Won't work in serverless or yarn rw dev

For example:

  await fastify.register(redwoodFastifyGraphQLServer, {
    loggerConfig: {
      logger: logger,
      options: { query: true, data: true, level: 'trace' },
    },
    graphiQLEndpoint: '/.redwood/functions/graphql', // 👈 this mimics the location of the classic handler function
    sdls,
    services,
    directives,
    subscriptions,
    allowedOperations: [
      OperationTypeNode.SUBSCRIPTION,
      OperationTypeNode.QUERY,
      OperationTypeNode.MUTATION,
    ],
    allowIntrospection: true,
    allowGraphiQL: true,
  })
  • Add code gen options to look for subscriptions inside the api/src/subscriptions/** directory so they gen included in the generated GraphQL schema
  • Adds useSubscription GraphQL hook to GraphQLHooksProvider so can import useSubscription from @redwoodjs/web
import { useSubscription } from '@redwoodjs/web'

const COUNTDOWN_SUBSCRIPTION = gql`
  subscription OnTheFinalCountdown {
    countdown(from: 1000, interval: 10)
  }
`

function FinalCountdown() {
  const { data, loading } = useSubscription(COUNTDOWN_SUBSCRIPTION, {
    variables: {},
  })
  console.log(data)
  return <h4>Duh dah dah dah dah!: {!loading && data.countdown}</h4>
}

This allows Subscription to be used and live query to run on server, but there is one remaining issue in that the @live directive is not part of the GraphQL schema that is generated.

While it would be easy to simply add this to the redwood root schema, I think it would be better to add the sdl types to create the directive use a yarn rw exp setup realtime cli command.

In short, the current code gen and code file loader only extracts the gel pluck and string interpolation isn't supported, so the schema won't be loaded to generate. Thus a command will be able to output the live directive better (I think).

The PR lets you use subs and live queries from pages and cells, but linting will show a warning for anything that uses @live at the moment.

@dthyresson dthyresson self-assigned this May 19, 2023
@dthyresson dthyresson added the release:feature This PR introduces a new feature label May 19, 2023
@dthyresson dthyresson changed the title featureL DRAFT - Support GraphQL Subscriptions on web side feature: DRAFT - Support GraphQL Subscriptions on web side May 19, 2023
@replay-io
Copy link

replay-io bot commented May 19, 2023

19 replays were recorded for 76fcf6f.

image 0 Failed
image 19 Passed

View test run on Replay ↗︎

@dthyresson dthyresson marked this pull request as ready for review May 22, 2023 17:00
@dthyresson dthyresson requested a review from jtoar May 22, 2023 17:00
Copy link
Contributor

@jtoar jtoar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dthyresson looks good!

@dthyresson dthyresson enabled auto-merge (squash) May 22, 2023 23:46
@dthyresson dthyresson merged commit 0de84e4 into redwoodjs:main May 23, 2023
25 checks passed
@redwoodjs-bot redwoodjs-bot bot added this to the next-release milestone May 23, 2023
@dthyresson dthyresson changed the title feature: DRAFT - Support GraphQL Subscriptions on web side feature: Support GraphQL Subscriptions on web side May 23, 2023
jtoar added a commit that referenced this pull request May 24, 2023
* Codegen subscriptions

* Add useSubscription graphql hook to web package

* Adds test for useSubscription hook

* Whitespace cleanup

* Fixes useSubscriptionHook typo

* Include subscriptions in codegen

* chore: run lint fix

---------

Co-authored-by: Dominic Saadi <dominiceliassaadi@gmail.com>
@jtoar jtoar modified the milestones: next-release, temp, v6.0.0 Jun 2, 2023
jtoar added a commit that referenced this pull request Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature This PR introduces a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants