Nexus的订阅没有记录,但是我搜索了Github并尝试了本书中的每个示例。只是对我不起作用。
我已经克隆了Prisma2 GraphQL boilerplate project,我的文件如下:
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
}
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上的每个示例都与上面相似,甚至我都在做同样的事情。
有什么建议吗?
编辑:尚未实现订阅:(
答案 0 :(得分:1)
尽管未实施订阅,但我似乎可以正常工作。我有一个有效的pubsub概念证明,它基于prisma2样板和Ben Awad的视频教程https://youtu.be/146AypcFvAU。应该可以使用redis和websockets来启动它并运行它,以处理订阅,直到达到Arizonaa2版本为止。
答案 1 :(得分:0)