Ad

I Try To Implement A Connection Using Relay And All The Node's IDs Are The Same

- 1 answer

I write a really simple schema using graphql, but some how all the IDs in the edges are the same.

{
    "data": {
        "imageList": {
            "id": "SW1hZ2VMaXN0Og==",
            "images": {
                "edges": [
                  {
                      "node": {
                          "id": "SW1hZ2U6",
                          "url": "1.jpg"
                      }
                  },
                  {
                      "node": {
                          "id": "SW1hZ2U6",
                          "url": "2.jpg"
                      }
                  },
                  {
                      "node": {
                          "id": "SW1hZ2U6",
                          "url": "3.jpg"
                      }
                  }
                ]
            }
        }
    }
}

I posted the specific detail on github here's the link.

Ad

Answer

So, globalIdField expects your object to have a field named 'id'. It then takes the string you pass to globalIdField and adds a ':' and your object's id to create its globally unique id.

If you object doesn't have a field called exactly 'id', then it wont append it, and all your globalIdField will just be the string you pass in and ':'. So they wont be unique, they will all be the same.

There is a second parameter you can pass to globalIdField which is a function that gets your object and returns an id for globalIdField to use. So lets say your object's id field is actually called '_id' (thanks Mongo!). You would call globalIdField like so:

id: globalIdField('Image', image => image._id)

There you go. Unique IDs for Relay to enjoy.

Here is the link to the relevant source-code in graphql-relay-js: https://github.com/graphql/graphql-relay-js/blob/master/src/node/node.js#L110

Ad
source: stackoverflow.com
Ad