Anyone managed to create his own subscription reso...
# prisma-whats-new
t
Anyone managed to create his own subscription resolver in prisma? I have no idea how to access the pubsub service in my resolver like i can do this in graphql-yoga
Ok found the solution! Just at it to your server index.ts to the context
Copy code
typescript
import { GraphQLServer, PubSub } from 'graphql-yoga';
import { Prisma } from './generated/prisma';
import resolvers from './resolvers';

const server = new GraphQLServer({
  typeDefs: './src/schema.graphql',
  resolvers,
  context: req => ({
    ...req,
    db: new Prisma({
      endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma DB service (value is set in .env)
      secret: process.env.PRISMA_SECRET, // taken from database/prisma.yml (value is set in .env)
      debug: true // log all GraphQL queries & mutations
    }),
    pubsub: new PubSub()
  })
});

server.start(() => console.log(`Server is running on <http://localhost:4000`>));
don’t forget to extend your Context Interface πŸ˜‰ after that u have access in your resolver to the pubsub under ctx.pubsub Works so cool everything :))
hehe but next problem appears of course πŸ˜„ how do i get my user πŸ™‚ i am researching and keep you in touch
d
I think you are looking for this? https://www.prismagraphql.com/docs/tutorials/graphql-server-development/permissions-thohp1zaih In this example, take a look at utils.js
It uses context to get request authorization header, token from it and user ID from it.
t
it works for queries and mutations because there the request object is propagated to the ctx
but for subscriptions the is no request because it is an open connection -> for that the connection property is propagated but with almost no information
d
aha! got it, If I run into a solution I will post it here! πŸ‘
t
πŸ˜‰
working on it πŸ˜‰
will be something for the docs later too
but it looks like that we need to do for that something in yoga because there the connection is build up.
Copy code
javascript
this.subscriptionServer = SubscriptionServer.create(
          {
            schema: this.executableSchema,
            execute,
            subscribe,
            onOperation: async (message, connection, webSocket) => {
              let context
              try {
                context =
                  typeof this.context === 'function'
                    ? await this.context({ connection })
                    : this.context
              } catch (e) {
                console.error(e)
                throw e
              }
              return { ...connection, context }
            },
          },
          {
            server: combinedServer,
            path: this.options.subscriptions,
          },
        )
here you can see the onOperation -> this is how the connection is injected
graphql-subscriptions have a onConnect function too and there you can read out the auth token
i will try to make a PR with a proposal and than lets see
d
Awesome, thanks πŸ‘
t
ok found a solution and it works but must be discussed in the PR
i am creating one now