javascript - 订阅不适用于 Prisma 2 和 Nexus?

标签 javascript graphql prisma prisma-graphql nexus-prisma

Nexus 订阅没有记录,但我搜索了 Github 并尝试了书中的每个示例。它对我不起作用。

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

棱镜/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`))

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

我在 Github 上搜索了 this query这导致所有项目都使用 订阅

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

有什么建议吗?

编辑:订阅尚未实现 :(

最佳答案

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

https://github.com/ryanking1809/prisma2_subscriptions

关于javascript - 订阅不适用于 Prisma 2 和 Nexus?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57577464/

相关文章:

javascript - 如何从 javascript/jquery 中的多个复选框中获取附加值?

javascript - 如何在 MVC 中使用带有 Ajax Actionlink 的自定义确认对话框

javascript - Azure 上使用 javascript 运行网页的计划任务

graphql - 数据未合并,Apollo 3 分页与字段策略

Prisma,无法在 '_count' 查询中使用 'FindMany' 来获取数据总数

BlogSpot 中的 JavaScript/Jquery 在移动浏览器中不工作

.net-core - HotChocolate带有Authorize属性,如何获取当前登录的用户?

amazon-web-services - AWS Amplify - AppSync 和多个 DynamoDB 表

node.js - 我怎样才能 Jest 用 es6(没有 typescript )模拟 Prisma 客户端?

Prisma2 在prisma 模式中设置长度和列类型