【问题标题】:Subscriptions not working with Prisma 2 and Nexus?订阅不适用于 Prisma 2 和 Nexus?
【发布时间】:2019-12-25 21:31:14
【问题描述】:

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

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

prisma/schema.prisma

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 上的每个示例都与上面类似,甚至我也在做同样的事情。

有什么建议吗?

编辑:订阅尚未实现:(

【问题讨论】:

  • 我想我会在添加订阅时添加答案,但我会将编辑添加为答案:)

标签: javascript graphql prisma prisma-graphql nexus-prisma


【解决方案1】:

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

https://github.com/ryanking1809/prisma2_subscriptions

【讨论】:

  • 哦,是的,我在 GitHub 上看到了您的问题。我已经订阅了那个问题。我认为订阅现在只适用于有点骇人听闻的 Pub/Sub 模型。我与 Slack 的团队进行了交谈,他们说它甚至还没有确定,所以我们必须等待。在那之前没有实时的:)
  • 是的,我很高兴用 PubSub 来填补这个空间,直到他们有其他事情发生。
【解决方案2】:

订阅尚未实现。

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

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

【讨论】:

    猜你喜欢
    • 2021-05-18
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2016-03-06
    • 2020-08-11
    • 2020-10-27
    • 2018-03-31
    相关资源
    最近更新 更多