Graphcool / graphcool-framework

Apache License 2.0
1.77k stars 131 forks source link

Server Side Subscription is not firing #526

Open ed-zm opened 6 years ago

ed-zm commented 6 years ago

Hi guys, I am trying to fire a server side subscription to create a notification when a comment is added.

This is my graphql subscription:

subscription serverSideCommentsSubscription {
  Comment(filter: {
    mutation_in: [CREATED]
  }){
    node {
      id
      event {
        id
        host {
          id
        }
        attendees {
          id
        }
      }
      user {
        id
        name
        picture
      }
    }
  }
}

this is my graphcool.yml:

functions:
  commentsSubscription:
    type: subscription
    query: './src/subscriptions/commentsSubscription/index.graphql'
    handler:
      code:
        src: './src/subscriptions/commentsSubscription/index.ts'

permissions:
  - operation: "*"

this is my type:

type Comment @model {
  id: ID! @isUnique
  comment: String!
  private: Boolean!
  user: User! @relation(name: "UserComments")
  event: Event! @relation(name: "EventComments")
  replies: [Reply!]! @relation(name: "CommentReplies")
  createdAt: DateTime!
  updatedAt: DateTime!
}

this is my resolver:

export default async(e: FunctionEvent<EventData>) => {
  console.log('in')
  try {
    const { node: { id, user, event }} = e.data.Comment
    const graphcool = fromEvent(e)
    const api = graphcool.api('simple/v1')
    const title = 'has commented on'
    const allReceivers = event.attendees.concat(event.host)
    const receivers = allReceivers.filter(receiver => receiver.id !== user.id)
    console.log(receivers)
    await Promise.all(
      receivers.map(receiver => {
        createNotification(api, title, 'COMMENT', event.id, user.id, receiver.id)
      }))
  } catch(e) {
    console.log(e)
    return { error: `An unexpected error occured` }
  }
}

the current behavior is the function doesn't fire when I create a new comment, even if I create the new comment directly in the data explorer of graphcool playground.

However if I run this in the graphcool playground I get the data back, so the query is well written.

The expected behavior is the serverside subscription should fire the resolver and I should be able to get the data on server and perform the operations I need to do(create a notification).

Is this a current bug in the server side subscriptions? am I missing something in the graphcool docs?

PD: I have another subscription running and working with the same structure but of course with different purpose. Also, this subscription worked 2 or 3 times in the past and I got the logs in the playground, but didn't work anymore. Thanks in advance