Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined cursor if no connection is found (v2.0.0-alpha.3) #327

Closed
kristjanmik opened this issue Jul 16, 2021 · 7 comments · Fixed by #334
Closed

Undefined cursor if no connection is found (v2.0.0-alpha.3) #327

kristjanmik opened this issue Jul 16, 2021 · 7 comments · Fixed by #334
Labels
bug Something isn't working

Comments

@kristjanmik
Copy link

kristjanmik commented Jul 16, 2021

Describe the bug
After the #282 merge, I'm getting the error: Cannot read property 'cursor' of undefined.
This only happens when no nodes are connected with a relationship, but the error is not raised if relationships and nodes are found.

Type definitions

type Query {
  activity(id: ID!): Activity
}

interface ActivityHasMediaProperties {
  timestamp: DateTime
}

type Activity {
  media: [Media]!
    @relationship(
      type: "HAS_MEDIA"
      properties: "ActivityHasMediaProperties"
      direction: OUT
    )
}

To Reproduce

Querying for activity1 works. This is expected as at least one relationship exists.
Screenshot 2021-07-16 at 12 18 26

query Activity {
  activity(id: "activity1") {
    id
    mediaConnection{
      edges{
        timestamp
      }
    }
  }
}

Results

{
  "data": {
    "activity": {
      "id": "activity1",
      "mediaConnection": {
        "edges": [
          {
            "timestamp": "2021-04-14T00:00:00.000Z"
          }
        ]
      }
    }
  }
}

Querying for activity3 results in the above error. This is not expected, should return an empty array.
Screenshot 2021-07-16 at 12 18 37

query Activity {
  activity(id: "activity3") {
    id
    mediaConnection{
      edges{
        timestamp
      }
    }
  }
}

Results

{
  "errors": [
    {
      "message": "Cannot read property 'cursor' of undefined",
      "locations": [
        {
          "line": 151,
          "column": 3
        }
      ],
      "path": [
        "activity"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Cannot read property 'cursor' of undefined",
            "    at Model.<anonymous> (/...masked.../node_modules/@neo4j/graphql-ogm/src/classes/Model.ts:105:19)",
            "    at Generator.next (<anonymous>)",
            "    at fulfilled (/...masked.../node_modules/@neo4j/graphql-ogm/dist/classes/Model.js:23:58)",
            "    at processTicksAndRejections (internal/process/task_queues.js:93:5)"
          ]
        }
      }
    }
  ],
  "data": {
    "activity": null
  }
}

Expected behavior
Querying for activity 3 should return the following without errors.

{
  "data": {
    "activity": {
      "id": "activity3",
      "mediaConnection": {
        "edges": [ ]
      }
    }
  }
}
@kristjanmik kristjanmik added bug Something isn't working inbox labels Jul 16, 2021
@darrellwarde
Copy link
Contributor

Brilliant, I'm looking at a few bugs around edges, cursors, totalCount and pageInfo now, so this should help in reproduction. Thanks!

@dmoree
Copy link
Contributor

dmoree commented Jul 17, 2021

I am seeing this same error. Below is a reproduction based off the neo-push example.

Steps to reproduce:

  1. git clone https://github.com/dmoree/neo4j-graphql-issue-327.git Deleted
  2. cd neo4j-graphql-issue-327 && yarn install && yarn seed && yarn start Deleted
  3. Run following query in GraphiQL at http://localhost:4000/graphql (or other as defined in .env)
query PostsWithoutComments {
  posts {
    commentsConnection {
      edges {
        node {
          id
        }
      }
    }
  }
}
  1. error

Graph: (basically reproducing neo-push except not creating any comments on any posts)
Screen Shot 2021-07-16 at 7 35 04 PM

@litewarp
Copy link
Contributor

I'm 95% sure the issue is in the pagination helper when it calculates the returned pageInfo object.

I believe you just need to change it so that the start and end cursors are null when the count is zero.

@dmoree
Copy link
Contributor

dmoree commented Jul 18, 2021

@litewarp I'm not too familiar with it, but according to the Relay specification on Connection types:

A “Connection Type” must contain a field called pageInfo. This field must return a non-null PageInfo object, as defined in the “PageInfo” section below.

https://relay.dev/graphql/connections.htm#sel-FAFFHDCAACE0Bnld

And then in the PageInfo section it states:

PageInfo must contain fields hasPreviousPage and hasNextPage, both of which return non-null booleans. It must also contain fields startCursor and endCursor, both of which return non-null opaque strings.

https://relay.dev/graphql/connections.htm#sel-EALFDDAAACJvB_iO

My question would be that in the situation with zero edges would you return a PageInfo object with a startCursor and endCursor with empty strings. Otherwise, what would you return in this case?

@litewarp
Copy link
Contributor

litewarp commented Jul 18, 2021

Great question. So the graphql-relay package (which I've been following for reference) returns null for the end and start cursors when the edges array is empty.

Thus, when the total number of edges is zero, it will return a pageInfo object like the following:

pageInfo: {
  hasNextPage: false,
  hasPreviousPage: false,
  startCursor: null,
  endCursor: null
}

Also see PR #331

@dmoree
Copy link
Contributor

dmoree commented Jul 18, 2021

That makes sense to me. I couldn't see how to return a non-null string for startCursor when you have zero edges. Thanks for the PR.

@darrellwarde
Copy link
Contributor

The fix for this will be released in 2.0.0-alpha.4 shortly, please let us know if it works for you! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
4 participants