Search code examples
graphqlrelayjsrelaygraphql-js

Relay edges treated as same


Some of my Relay edges are being treated as the same despite being different data.

For instance, I want to show a list of recommended movies. These movies are recommended based off the fact that there is an actor the user likes.

I expect my data to be like this:

{
  "data": {
    "movies": {
      "edges": [
        {
          "node": {
            "name": "Cool Movie 1",
            "myActor": {
              "name": "John Smith"
            }
          },
          "node": {
            "name": "Cool Movie 1", <- Same as movie above, different actor
            "myActor": {
              "name": "Mary Smith"
            }
          }
        }
      ]
    }
  }
}

Yet it returns the same actor for both node.

In my resolve() I return a Relay connection. The edges are generated like this:

let edges = movieIdAndActorIdPairs.map(({movieId, actorId}) => ({
      cursor: //,
      node: async () => {
          const dataRequirements = [getMovie(movieId), getActor(actorId)]
          let [movie, actor] = await Promise.all(dataRequirements)
          movie.myActor = actor
          return movie
      }
}))

When I log my edges it shows the correct myActor but when I log the myActor field on my Movie type, they both become John Smith.


Solution

  • Thank you to everyone that helped me out.

    I've tried the suggested methods, but couldn't get the desired data set.

    So I took this approach where an actorMovie contains separate fields for actor and movie:

    {
      "data": {
        "actorMovies": {
          "edges": [
            {
              "node": {
                "movie": {
                  name: "Cool Movie 1",
                },
                "myActor": {
                  "name": "John Smith"
                }
              },
            }
          ]
        }
      }
    }
    

    This removes the worry of overlapping IDs causing unintentional groupings.