Gatsby createResolvers for data types within other data types

Viewed 160

I've got this data set:

  const avengers = [
    {
      firstName: 'Tony',
      lastName: 'Stark',
      name: 'Iron Man',
      bestFriends: {
        name: 'Captain America',
      },
    },
    {
      firstName: 'Bruce',
      lastName: 'Banner',
      name: 'Hulk',
      bestFriends: {
        name: 'Black Widow',
      },
    },
    {
      firstName: 'Thor',
      lastName: 'Odinson',
      name: 'Thor',
      bestFriends: {
        name: 'Rocket',
      },
    },
]

and what I'm trying to accomplish in Gatsby is to extend the bestFriends node in my Gatsby Nodes and allow the bestFriend's data to be fetched via a single query. My result query would look like this:

query myQuery {
 Avenger (name: {eq: "Iron Man}) {
    name
    firstName
    lastName
    bestFriends {
      name
      friendData {
        firstName
        lastName
        name
        bestFriends 
      }
    }
  }
}

However I'm struggling to figure out how to set up my createResolvers so that I can set a custom resolver function for the friendData node. It almost seems as if the Gatsby createResolvers API isn't able to resolve nested data sets. I assumed that this should work but when I do query the data via GraphiQL the node bestFriends returns "null".

  createResolvers({
      Avenger: {
          bestFriends: {
              type: "Avenger",
              resolve: (source, args, context) => {
                  return context.nodeModel.runQuery({
                      query: {
                          filter: {
                              name: {eq: source.bestFriends.name}
                          }
                      }
                  })
              }
          }
      }
  })
};

Any idea how I can accomplish what I'm getting at? I imagine that this is how Facebook would set up data structures for posts and comments, where the commentor's name, profile url, profile picture, and the comment itself is visible when you look at a post.

0 Answers
Related