Difference between using edges>node & nodes


#1

Could smbd explain, please?


#2

Hi @yanisurbis, I’ve asked the team for their input on this. I’ll be sure to write back to you once I find out more, though I don’t have an ETA for when that will be.


#3

Hey @yanisurbis :wave: This answer is two-part: definitions and differences. I’d like to set a basis for what we’re talking about first so that we’re on the same page. :slight_smile:

Definitions

A node is a generic term for an object. You can think of it as a “thing” that you can get at through a relationship. An edge represents the connections between nodes. Edges would represent many connections between nodes.

Example

For example, let’s take a repository and its issues. From a repository to issues, each node is the issue itself, defined by an “issue connection.” Here, the edge then would be the information about the relationship between repository and issue.

Differences

You asked what the difference is between using edges > node and nodes.

After speaking with one of my colleagues about this, the only difference between edges { node { ... } } and nodes { ... } is that one provides support for cursor and the other doesn’t. Here’s a sample query:

{
  viewer {
    repositories(first: 10) {
      edges {
        cursor
        node {
          name
        }
      }
      nodes {
        name
      }
    }
  }
}

cursor is useful for pagination. However, you may not want to use the edges { node } syntax to just get at some data. Also, you could add more fields to the edge other than cursor. For example:

{
  viewer {
    repositories(first: 10) {
      edges {
        node {
          languages(first:10) {
            edges {
              size
              node {
                name
              }
            }
          }
        }
      }
    }
  }
}

I can see how this information would be helpful in our https://developer.github.com/v4 documentation, so I’ve reached out to our Documentation team about this so they can take a look.

I hope that helps. Please let me know if you have any other questions about this!


#4

Thanks for asking the question. I was just wondering the same thing.

Even with the clarification about cursors, it strikes me that if your sole aim is pagination, you still have two ways, either:

  1. Get the cursor for each edge

    {
    viewer {
    name
    login
    repositories(first: 3) {
    edges {
    cursor
    node {
    name
    }
    }
    }
    }
    }

OR

  1. Get the pageInfo on the RepositoryConnection which gives you the startCursor and endCursor

    {
    viewer {
    name
    login
    repositories(first: 3) {
    pageInfo {
    endCursor
    hasNextPage
    hasPreviousPage
    startCursor
    }
    nodes {
    name
    }
    }
    }
    }

Depending on one’s use case, the latter might even be more helpful as it includes the hasNextPage and hasPreviousPage bool’s.

I totally agree that a little note about this in the getting started doc’s would be helpful :slight_smile: