订阅不适用于 Prisma 2 和 Nexus?

Subscriptions not working with Prisma 2 and Nexus?

Nexus 订阅未记录在案,但我搜索了 Github 并尝试了书中的每个示例。它只是不适合我。

我已经克隆了 Prisma2 GraphQL boilerplate project & 我的文件如下:

prisma/schema.棱镜

datasource db {
  provider = "sqlite"
  url      = "file:dev.db"
  default  = true
}

generator photon {
  provider = "photonjs"
}

generator nexus_prisma {
  provider = "nexus-prisma"
}

model Pokemon {
  id      String         @default(cuid()) @id @unique
  number  Int            @unique
  name    String
  attacks PokemonAttack?
}

model PokemonAttack {
  id      Int      @id
  special Attack[]
}

model Attack {
  id     Int    @id
  name   String
  damage String
}

src/index.js

const { GraphQLServer } = require('graphql-yoga')
const { join } = require('path')
const { makeSchema, objectType, idArg, stringArg, subscriptionField } = require('@prisma/nexus')
const Photon = require('@generated/photon')
const { nexusPrismaPlugin } = require('@generated/nexus-prisma')

const photon = new Photon()

const nexusPrisma = nexusPrismaPlugin({
  photon: ctx => ctx.photon,
})

const Attack = objectType({
  name: "Attack",
  definition(t) {
    t.model.id()
    t.model.name()
    t.model.damage()
  }
})

const PokemonAttack = objectType({
  name: "PokemonAttack",
  definition(t) {
    t.model.id()
    t.model.special()
  }
})

const Pokemon = objectType({
  name: "Pokemon",
  definition(t) {
    t.model.id()
    t.model.number()
    t.model.name()
    t.model.attacks()
  }
})

const Query = objectType({
  name: 'Query',
  definition(t) {
    t.crud.findManyPokemon({
      alias: 'pokemons'
    })
    t.list.field('pokemon', {
      type: 'Pokemon',
      args: {
        name: stringArg(),
      },
      resolve: (parent, { name }, ctx) => {
        return ctx.photon.pokemon.findMany({
          where: {
              name
          }
        })
      },
    })
  },
})

const Mutation = objectType({
  name: 'Mutation',
  definition(t) {
    t.crud.createOnePokemon({ alias: 'addPokemon' })
  },
})

const Subscription = subscriptionField('newPokemon', {
  type: 'Pokemon',
  subscribe: (parent, args, ctx) => {
    return ctx.photon.$subscribe.pokemon()
  },
  resolve: payload => payload
})

const schema = makeSchema({
  types: [Query, Mutation, Subscription, Pokemon, Attack, PokemonAttack, nexusPrisma],
  outputs: {
    schema: join(__dirname, '/schema.graphql')
  },
  typegenAutoConfig: {
    sources: [
      {
        source: '@generated/photon',
        alias: 'photon',
      },
    ],
  },
})

const server = new GraphQLServer({
  schema,
  context: request => {
    return {
      ...request,
      photon,
    }
  },
})

server.start(() => console.log(` Server ready at http://localhost:4000`))

相关部分是 Subscription,我不知道它为什么不起作用或它应该如何工作。

我在 Github 中搜索了 this query,这导致所有项目都使用 Subscriptions

我还发现 this commit in this project 与我的回答相关。为简洁起见,在此处发布相关代码:

import { subscriptionField } from 'nexus';
import { idArg } from 'nexus/dist/core';
import { Context } from './types';

 export const PollResultSubscription = subscriptionField('pollResult', {
  type: 'AnswerSubscriptionPayload',
  args: {
    pollId: idArg(),
  },
  subscribe(_: any, { pollId }: { pollId: string }, context: Context) {
    // Subscribe to changes on answers in the given poll
    return context.prisma.$subscribe.answer({
      node: { poll: { id: pollId } },
    });
  },
  resolve(payload: any) {
    return payload;
  },
});

这和我做的很相似。但是他们确实有 AnswerSubscriptionPayload 并且我没有得到任何包含 Subscription 的生成类型。

我该如何解决这个问题?我认为我做的一切都是对的,但它仍然没有用。 GitHub 上的每个示例都与上面类似,甚至我也在做同样的事情。

有什么建议吗?

编辑:订阅尚未实现:(

订阅尚未实现。

我已经打开 an issue 来追踪它。

我会在 Prisma 2 中实施后立即编辑此答案。

尽管没有实施订阅,但我似乎已经完成了这项工作。我有一个基于 prisma2 样板和 Ben Awad 的视频教程 https://youtu.be/146AypcFvAU 的工作 pubsub 概念证明。在 prisma2 版本准备就绪之前,应该能够启动并 运行 redis 和 websockets 来处理订阅。

https://github.com/ryanking1809/prisma2_subscriptions