Question about authentication for subscriptions u...
# prisma-whats-new
k
Question about authentication for subscriptions using Prisma. When I test a subscription using the playground, I send an Authorization header and it comes through but when the same request is sent through apollo on android no header comes through. Has any one had any issues like this or have any insight into what is going on?
😖 1
This is my
Subscription.js
file
Copy code
const { getUserIdFromAuthorization } = require('../utils')

const Subscription = {
  pendingMessages: {
    subscribe: async (parent, args, ctx, info) => {
      console.log(info)
      console.log("AUTHORIZATION: " + ctx.connection.context.Authorization)
      const userId = getUserIdFromAuthorization(ctx.connection.context.Authorization)
      return ctx.db.subscription.pendingMessage({
        where:{
          mutation_in: ["CREATED", "UPDATED"],
          node: {
            user: {id: userId},
          },
        }
      }, info)
    }
  },

  newMessage: {
    subscribe: async(parent, args, ctx, info) => {
      const userId = getUserIdFromAuthorization(ctx.connection.context.Authorization)
      return ctx.db.subscription.message({
        where:{
          mutation_in: ["CREATED"],
          node:{
            conversation: {
              user: {
                id: userId
              }
            }
          }
        }
      }, info)
    }
  },

}

module.exports = { Subscription }
index.js
for resolvers
Copy code
const { Query } = require('./Query')
const { auth } = require('./Mutation/auth')
const { messages } = require('./Mutation/messages')
const { conversations } = require('./Mutation/conversations')
const { AuthPayload } = require('./AuthPayload')
const { Subscription } = require('./Subscription')

module.exports = {
  Query,
  Mutation: {
    ...auth,
    ...messages,
    ...conversations,
  },
  Subscription,
  AuthPayload,
}
d
subscription go through WebSocket by default, that means there is a single connection at the beginning where you needs to pass the token ... in case you don't have it yet, you have to ensure that connection is not open till you have it or you need to reconnect
simply said, you cannot change headers for websocket connection
a
Speaking of playground authentication…I’m having issues because my playground App tab won’t let me run a mutation…but I have definitely set
Copy code
disableAuth: true
So I can run the mutation directly on the server tab of the playground…but not on the app tab because
Copy code
{
  "data": null,
  "errors": [
    {
      "message": "Not authorized",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "createCountry"
      ]
    }
  ]
}
any ideas? 😞
d
disableAuth is for DB tab only 🙂 the app tab is completely under your control (it's your code)
k
@Daniel K. Makes sense thanks!