paljs / prisma-tools

Prisma tools to help you generate CRUD system for GraphQL servers
https://paljs.com
MIT License
683 stars 54 forks source link

Subscriptions #142

Open DregondRahl opened 3 years ago

DregondRahl commented 3 years ago

Would it be possible to add subscriptions ? Would be linked to the mutations (createOne, updateOne)

import { subscriptionField } from '@nexus/schema'

export const PostLatestSubscription = subscriptionField('latestPost', {
  type: 'Post',
  subscribe(_root, _args, ctx) {
    return ctx.pubsub.asyncIterator('latestPost')
  },
  resolve(payload) {
    return payload
  },
})
import { mutationField, arg } from '@nexus/schema'

export const PostCreateOneMutation = mutationField('createOnePost', {
  type: 'Post',
  nullable: false,
  args: {
    data: arg({
      type: 'PostCreateInput',
      nullable: false,
    }),
  },
  resolve: async (_parent, { data }, { prisma, select, pubsub }) => {
    const newPost = await prisma.post.create({
      data,
      ...select,
    })

    pubsub.publish('latestPost', newPost)
    return newPost
  },
})
AhmedElywa commented 3 years ago

It's a good idea to make a plugins system to our CLI generator so you can add custom generations.

DregondRahl commented 3 years ago

one way to do this could be to create a _templates folder when a project is created using paljs, then before they run pal g they can modify the templates and then run it. It would be similar to how hygen works. It would be easy then to even generate it with validations or anything else customizable.

AhmedElywa commented 3 years ago

Yes, you are right, but it's like a big change