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

support for subscription type #1096

Open
jasonkuhrt opened this issue Sep 16, 2024 · 4 comments
Open

support for subscription type #1096

jasonkuhrt opened this issue Sep 16, 2024 · 4 comments

Comments

@jasonkuhrt
Copy link
Member

Perceived Problem

Ideas / Proposed Solution(s)

@scythewyvern
Copy link

What do you think about this API?

const graffle = Graffle
  .create()
  .transport({
    url: '',
  })
  .use(Subscription())

const ProjectSubscription = gql(`
  subscription ProjectSubscription {
    project {
      name
      slug
    }
  }
`)

const unsubscribe = graffle
  // or .subsription(ProjectSubscription, {})
  .gql(ProjectSubscription, {})
  .subscribe((data) => {
    /**
     * incoming data
     * {
     *   project: {
     *     name: string
     *     slug: string
     *   }
     * }
     */
    console.log(data)
  })
  

@jasonkuhrt
Copy link
Member Author

jasonkuhrt commented Dec 24, 2024

const graffle = Graffle.create()

const subscription = await graffle
	.gql`
	  subscription {
	    project {
	      name
	      slug
	    }
	  }
	`
    .subscribe()

console.log(subscription.response)

let i=0
await for (event of subscription) {
  i++
  console.log(event)
    /**
     * {
     *   project: {
     *     name: string
     *     slug: string
     *   }
     * }
     */
  if (i===4) break
}


await subscription.cancel()

Document Builder:

const subscription = await graffle.subscription.project({
  name,
  slug,
})

Key points:

  • .subscribe alternative to .send to signal that the document is expected to result in a subscription.
  • returns an object that has an async iterator (observable) interface as well as socket methods (e.g. cancel) and response data. I am not sure about the GraphQL envelope, e.g. errors.

Reasons we might want .use(Subscription()) (subscription support as an extension):

  • Subscriptions significantly increase bundle size
  • We want to be able to use alternative subscription implementations

wdyt?

@scythewyvern
Copy link

i think i like it!

also, if you use graphql-ws for implementation, you will need to be able to pass arguments to configure the client or even be able to pass a client created manually (not sure about this)

@jasonkuhrt
Copy link
Member Author

fwiw We can also make it work cross transport, e.g. http or memory.

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

No branches or pull requests

2 participants