简体   繁体   中英

Subscriptions not working with Prisma 2 and Nexus?

Subscriptions with Nexus are undocumented but I searched Github and tried every example in the book. It's just not working for me.

I have cloned Prisma2 GraphQL boilerplate project & my files are as follows:

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`))

The related part is the Subscription which I don't know why it's not working or how it's supposed to work.

I searched Github for this query which results in all projects using Subscriptions .

I also found out this commit in this project to be relevant to my answer. Posting the related code here for brevity:

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;
  },
});

Which is similar to what I do. But they do have AnswerSubscriptionPayload & I don't get any generated type that contains Subscription in it.

How do I solve this? I think I am doing everything right but it's still not working. Every example on GitHub is similar to above & even I am doing the same thing.

Any suggestions?

Edit: Subscriptions aren't implemented yet :(

I seem to have got this working despite subscriptions not being implemented. I have a working pubsub proof of concept based off the prisma2 boilerplate and Ben Awad's video tutorial https://youtu.be/146AypcFvAU . Should be able to get this up and running with redis and websockets to handle subscriptions until the prisma2 version is ready.

https://github.com/ryanking1809/prisma2_subscriptions

Subscriptions aren't implemented yet.

I've opened up an issue to track it.

I'll edit this answer as soon as it's implemented in Prisma 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM