Comments (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.
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.
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.
Thanks @FredyC, I am thinking to create my own withFilter() func. You save my time.
from prisma-binding.
Related Issues (20)
- how to submit many transactions
- Connect Prisma Bindings to mock server
- Calling a query from Yoga to Prisma does nothing. HOT 5
- Case Sensitive Query Adjustments. HOT 2
- Calling multiple prisma requests returns same error to all the request if any one of them gives error.
- forwardTo() without having to specify resolver keys
- Cannot set property 'fragmentReplacements' of undefined HOT 1
- forwardTo with type rename
- how to get the count of records
- Howto define subscription endpoint
- Sub-selecting custom fields HOT 2
- Subscription Implementation with Prisma Binding
- Improve Code Readability
- error when trying to call aggregate function HOT 1
- Aliases still not works correctly
- How to filter `Where null` HOT 1
- Prisma() not recognized as a constructor HOT 1
- Import error on IResolvers from graphql-tools
- The automated release is failing 🚨
- Version of this for graphql-code-generator ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prisma-binding.