Giter VIP home page Giter VIP logo

Comments (4)

danielkcz avatar danielkcz commented on May 24, 2024 4

I have made my own kind of withFilter that is more like filter + map. It was a rather big struggle at the beginning, but at least now I understand how AsyncIterator works and it works really well.

export function subscriber(getIterator, handler) {
  return async (_: any, args: any, ctx: Context, info: GraphQLResolveInfo) => {
    const iterator = await getIterator(_, args, ctx, info)
    const nextChange = async () => {
      const { value, done } = await iterator.next()
      const result = await handler(value, args, ctx, info)
      return result === null ? nextChange() : { value: result, done }
    }
    return {
      next() {
        return nextChange()
      },
      return() {
        return Promise.resolve({ value: undefined, done: true })
      },
      throw(error) {
        return Promise.reject(error)
      },
      [$$asyncIterator]() {
        return this
      },
    }
  }
}

And then I can write resolver that either returns null to ignore that change or transform it to required result.

const selectionSet = `{
  mutation
  node {
    id
  }
}`

export const onFightStart = {
  subscribe: subscriber(
    (_, __, ctx: Context) => ctx.db.subscription.fight({}, selectionSet),
    async ({ mutation, node }, _, ctx: Context) => {
      if (mutation !== 'CREATED' || !node) {
        return null
      }
      const { id } = node
      return { onFightStart: id }
    },
  )
}

I need to figure out Typescript here to have it more dynamic thou. Looks like there are some issues with prisma-binding, but I need to investigate further.

from prisma-binding.

timsuchanek avatar timsuchanek commented on May 24, 2024 1

Hi @tbrannam thanks for asking that question and sorry for getting back so late!
Your implementation looks correct.

That you have to call the result of withFilter lies in the fact, that it is intended to be used directly as a resolver. So the function, that withFilter is returning, has a resolver signature, as you can see here https://github.com/apollographql/graphql-subscriptions/blob/master/src/with-filter.ts

To make your implementation work correctly, you have to pass in the resolver args:

  subscribe: async (parent, args, ctx, info) => {
    const subscription = await ctx.db.subscription.link(
      { },
      info,
    )

    const filteredSubscription =  withFilter( () => subscription , (payload, variables) => {
      return true
    })

    return filteredSubscription(parent, args, ctx, info)
  } 

withFilter indeed should await the result of the function passed in as the first arg to make it easier to use.
Maybe you want to open an issue in https://github.com/apollographql/graphql-subscriptions and ask about that? I'm sure they're happy to get a PR for that :)

I'm closing this, as it's not a particular issue with prisma-binding, but the graphql-subscriptions implementation.

from prisma-binding.

schickling avatar schickling commented on May 24, 2024

Thanks a lot for sharing this code @FredyC. We should probably think about how we could make this easier – possibly through a utility function.

from prisma-binding.

ashish-patidar-rau avatar ashish-patidar-rau commented on May 24, 2024

Thanks @FredyC, I am thinking to create my own withFilter() func. You save my time.

from prisma-binding.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.