Closed useafterfree closed 1 year ago
Hi @useafterfree
You can use this library with Pothos plugins like this:
import { builder } from "../../builder";
import { findFirstOrderQueryObject } from "../../__generated__/Order";
export const findFirstOrderTest = builder.queryFields((t) => {
const field = findFirstOrderQueryObject(t);
return {
findFirstOrderTest: t.prismaField({
...field,
args: {
testParam: t.arg({ type: "String", required: false }),
},
resolve: async (...args) => {
const [include, root, { testParam }, { response, prisma }, info] = args;
if (testParam === "123") throw new Error("Invalid");
return field.resolve(...args);
},
smartSubscription: true,
subscribe: (subscriptions) => subscriptions.register(`DATABASE-UPDATED-ORDER`)
}),
};
});
And then in prisma Middleware you can trigger pubsub updates like this:
prisma.$use(async (params, next) => {
const { action, args, dataPath, model } = params
const result = await next(params)
if (
params.action === 'create' ||
params.action === 'update' ||
params.action === 'delete' ||
params.action === 'deleteMany' ||
params.action === 'updateMany' ||
params.action === 'createMany'
) {
log(module)(`🚀 ${params.action} ${params.model}`)
pubsub.publish('DATABASE-UPDATED-'+model, {})
}
// See results here
return result
})
Inside builder you need to configure
...
import SmartSubscriptionsPlugin, { subscribeOptionsFromIterator } from '@pothos/plugin-smart-subscriptions';
export const builder = new SchemaBuilder<{
Context: Context,
PrismaTypes: PrismaTypes,
Scalars: Scalars<Prisma.Decimal, Prisma.InputJsonValue | null, Prisma.InputJsonValue>,
smartSubscriptions: {
debounceDelay: number | null;
subscribe: (
name: string,
context: Context,
cb: (err: unknown, data?: unknown) => void,
) => Promise<void> | void;
unsubscribe: (name: string, context: Context) => Promise<void> | void;
},
}>({
plugins: [PrismaPlugin, SmartSubscriptionsPlugin],
prisma: {
client: prisma,
},
smartSubscriptions: {
...subscribeOptionsFromIterator((name, { pubsub }) => {
return pubsub.asyncIterator(name);
}),
},
});
And pothos auto creates a subscription for findFirstOrderTest
query
@useafterfree
In the new 0.5.1 version you can apply plugins to all crud resolvers
Please, see example
I hope it helps
omg, thanks so much!
I applied your example and got: Cannot read properties of undefined (reading 'Symbol(Pothos.contextCache)')
which I am not sure how to address.
Can you please share a minimal, reproducible example? in a Github repo?
Of course. I made a version here.
You can run by using: yarn run example
or yarn run dev:debug
which both run example.ts
@useafterfree
Strange that I couldn't make Pothos work with the new Apollo Subscriptions. I tested it with another Schema (ok), and when I switch to Schema with Pothos, it gives an error ❌ (I've opened a issue)...
Months ago I had tried to configure with an old version of apollo and it had worked fine 💹 https://github.com/Cauen/apollo-subscriptions-pothos/tree/old-version-apollo This example generates automatic subscriptions for all queries with autocrud.
I tested pothos example with Yoga, and working fine... 💹
Let's wait to see if the author of Pothos can help with something in this
@useafterfree Updated example now working
Thank you, let me take a look!
After adding the context to useServer
it worked perfectly, thank you so much!
Would it be possible to generate subscriptions in the the generators?
I am using: https://pothos-graphql.dev/docs/plugins/smart-subscriptions#creating-a-smart-subscription
I am looking for suggestions, also. Not sure if I need to tweak the generated output by hand or not?