首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用Prisma 2和Nexus的订阅?

不使用Prisma 2和Nexus的订阅?
EN

Stack Overflow用户
提问于 2019-08-20 15:56:42
回答 2查看 3.2K关注 0票数 5

对Nexus的订阅是没有文档的,但是我搜索了Github,并尝试了这本书中的每一个例子。只是对我没用。

我克隆了Prisma2 GraphQL样板工程 &我的文件如下所示:

普里斯马/方案

代码语言:javascript
复制
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

代码语言:javascript
复制
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,我不知道它为什么不能工作,也不知道它应该如何工作。

我搜索了这个查询,这导致了所有使用Subscriptions的项目。

我还发现这个项目中的承诺与我的答案相关。为了简洁起见,在这里张贴相关代码:

代码语言:javascript
复制
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上的每个示例都类似于上面的内容&甚至我也在做同样的事情。

有什么建议吗?

编辑:订阅尚未实现:(

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-21 03:58:01

订阅尚未实现。

我打开了问题来追踪它。

一旦在Prisma 2中实现了这个答案,我就会编辑它。

票数 0
EN

Stack Overflow用户

发布于 2019-08-28 20:04:55

尽管订阅没有实现,但我似乎已经做到了这一点。我有一个基于prisma2样板和Ben的视频教程https://youtu.be/146AypcFvAU的概念的公开证明。应该可以使用redis和websockets来处理订阅,直到prisma2版本准备就绪为止。

订阅

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57577464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档