neo4j / graphql

A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations.
https://neo4j.com/docs/graphql-manual/current/
Apache License 2.0
504 stars 149 forks source link

Created Subscription Returns Invalid Data #5451

Closed DanielAtCosmicDNA closed 2 months ago

DanielAtCosmicDNA commented 2 months ago

Describe the bug A clear and concise description of what the bug is.

When following: https://github.com/CosmicDNA/neo4j-graphql-subscription, the resulting subscription returns null for the fields seen and sender when they should return:

  "sender": {
      "id": "6015e398-3e93-4f61-a035-47abd5b969ad"
    },
    "seen": []

Type definitions Here are the type definitions:

type User {
  id: ID! @id @unique
  name: String
  conversations: [Conversation!]!
    @relationship(type: "CONVERSATIONS_USERS", direction: IN)
  seenMessages: [Message!]!
    @relationship(type: "SEEN_MESSAGES_USERS", direction: IN)

  messages: [Message!]! @relationship(type: "MESSAGES_SENDER", direction: OUT)
}

type Conversation {
  id: ID! @id @unique
  name: String

  messages: [Message!]!
    @relationship(type: "CONVERSATION_MESSAGES", direction: IN)
  users: [User!]! @relationship(type: "CONVERSATIONS_USERS", direction: OUT)
}

type Message {
  id: ID! @id @unique
  body: String

  seen: [User!]! @relationship(type: "SEEN_MESSAGES_USERS", direction: OUT)
  conversation: Conversation
    @relationship(type: "CONVERSATION_MESSAGES", direction: OUT)

  sender: User @relationship(type: "MESSAGES_SENDER", direction: IN)
}

type Subscription {
  messageCreated(conversationId: ID!): Message
}

To Reproduce Steps to reproduce the behavior:

  1. Run a server with the following code
  2. Execute the following Mutation
  3. Execute the following Subscription
  4. Execute the following Mutation
  5. Execute the following Query
  6. Compare the subscription output from step 3 with the output of the query from step 5 and see the error.

Expected behavior The subscription output should show seen and sender variables correctly as follows:

// Response received at 12:57:54
{
  "data": {
    "messageRelationshipCreated": {
      "message": {
        "id": "e4e4d3bc-b2b9-4f3f-8b39-7fbaf089670c",
        "body": "Welcome to you!"
      },
      "createdRelationship": {
        "seen": [],
        "sender": {
          "id": "6015e398-3e93-4f61-a035-47abd5b969ad"
        },
        "conversation": {
          "node": {
            "id": "8f8faa32-a565-4b48-bf6b-cd69ab4ee82a"
          }
        }
      }
    }
  }
}

Screenshots

image

System (please complete the following information):

Additional context

neo4j-team-graphql commented 2 months ago

Many thanks for raising this bug report @DanielAtCosmicDNA. :bug: We will now attempt to reproduce the bug based on the steps you have provided.

Please ensure that you've provided the necessary information for a minimal reproduction, including but not limited to:

If you have a support agreement with Neo4j, please link this GitHub issue to a new or existing Zendesk ticket.

Thanks again! :pray:

darrellwarde commented 2 months ago

Hey @DanielAtCosmicDNA, let's talk about your expectations.

{
  "data": {
    "messageRelationshipCreated": {
      "message": {
        "id": "e4e4d3bc-b2b9-4f3f-8b39-7fbaf089670c",
        "body": "Welcome to you!"
      },
      "createdRelationship": {
        "seen": [],
        "sender": {
          "id": "6015e398-3e93-4f61-a035-47abd5b969ad"
        },
        "conversation": {
          "node": {
            "id": "8f8faa32-a565-4b48-bf6b-cd69ab4ee82a"
          }
        }
      }
    }
  }
}

You expect seen to be an empty array, but this field is concerning itself with whether a seen relationship was created as part of the mutation, which it was not. seen might be an empty array when querying the conversation, but in the mutation a seen relationship was not created so it is being returned as null as it should be in this case.

For sender and conversation - a subscription event is created for each created relationship. The event you've shown in your reproduction is the event for the conversation relationship was created. There should be another event which contains the sender relationship information! Can you please check this?

DanielAtCosmicDNA commented 2 months ago

@darrellwarde, I would rather subscribe with:

subscription OnMessageCreated($where: MessageRelationshipCreatedSubscriptionWhere) {
  messageRelationshipCreated(where: $where) {
    message {
      id
      body
      sender {
        id
      }
      seen {
        id
      }
    }

But I am not sure how to achieve this or workaround the limitation of filtering being only able to be applied at the root of the subscription operation...

DanielAtCosmicDNA commented 2 months ago

I wanted to avoid making a second query to get the message with sender and seen fields populated. I would rather have this promptly available straight from the subscription instead...

darrellwarde commented 2 months ago

I see. Apologies @DanielAtCosmicDNA, but we are working within the constraints of what we can do with the database, I'm afraid it's not on our roadmap to have the subscriptions payload fully match the fields that you can select in a query. I'm going to close this issue because it is working as it is intended to. Thanks for opening this!