How to use the "node" entry point?


#1

According to the doc, I can start my GraphQL query by querying for a specific node.

In this example I tried to query one of my organization’s name:

query {
  node(id: "MDEyOk9yZ2FuaXphdGlvbjEyNjU5MDAx") {
    name
  }
}

And it resulted in this:

{
  "data": null,
  "errors": [
    {
      "message": "Field 'name' doesn't exist on type 'Node'",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ]
    }
  ]
}

So my question is how should I assemble a proper query like this? Shouldn’t the queried node be casted to its real type (into an Organization in this example that implements the Node)?


#2

It seems that using fragments I can get the result I want.

I.e. to list the members of an organization:

query {
  orgNode: node(id: "MDEyOk9yZ2FuaXphdGlvbjEyNjU5MDAx") {
    ...fields
  }
}

fragment fields on Organization {
  id,
  name,
  members(first: 30) {
    edges {
      node {
        login
      }
    }
  }
}

Result:

{
  "data": {
    "orgNode": {
      "id": "MDEyOk9yZ2FuaXphdGlvbjEyNjU5MDAx",
      "name": "Cheppers",
      "members": {
        "edges": [
          {
            "node": {
              "login": "tompagabor"
            }
          },
          {
            "node": {
              "login": "Yitsushi"
            }
          },
          {
            "node": {
              "login": "snufkin"
            }
          },
          {
            "node": {
              "login": "csg"
            }
          },
          {
            "node": {
              "login": "MauPalantir"
            }
          },
          {
            "node": {
              "login": "smatyas"
            }
          },
          {
            "node": {
              "login": "Sweetchuck"
            }
          },
          {
            "node": {
              "login": "danikalmar"
            }
          },
          {
            "node": {
              "login": "miromichalicka"
            }
          },
          {
            "node": {
              "login": "baliame"
            }
          },
          {
            "node": {
              "login": "nemeth"
            }
          },
          {
            "node": {
              "login": "ghaabor"
            }
          },
          {
            "node": {
              "login": "ProZsolt"
            }
          },
          {
            "node": {
              "login": "Peneheals"
            }
          },
          {
            "node": {
              "login": "janost"
            }
          },
          {
            "node": {
              "login": "CzDani"
            }
          },
          {
            "node": {
              "login": "kozmate"
            }
          },
          {
            "node": {
              "login": "kovacslaci"
            }
          }
        ]
      }
    }
  }
}

#3

Hi @smatyas!

It seems that using fragments I can get the result I want.

That’s correct. You could also use an inline fragment if you so desired:

{
  node(id: "MDEyOk9yZ2FuaXphdGlvbjEyNjU5MDAx") {
    ... on Organization {
      id
      name
      members(first: 30) {
        edges {
          member:node {
            login
          }
        }
      }
    }
  }
}