Charlie
Charlie

Reputation: 104

Can I return a mapped object from a GraphQL query?

I'm wondering if I can create an object using GraphQL, instead of manipulating the data that GraphQL returns. I'm using GraphQL in Gatsby.

My query looks something like this:

nodes {
  name
  url
}

The data looks like this:

{
  "data": {
    "nodes": [
      {
        "name": "ABC"
        "url": "ABC.com"
      }
    ]
  }
}

Is it possible to use GraphQL to return something that looks like this?

{
   "data": {
      "ABC": "ABC.com"
   }
}

Upvotes: 0

Views: 162

Answers (1)

Andy
Andy

Reputation: 31

I am missing the context of your use case, but I question why you would need this functionality.

The typical use case would be to hydrate the data object with the property name and value as key value pairs, which will allow you to access the data in your JSX template as data.propertyname.

If you change the structure to what you are suggesting above, you would need to know the "name" of the object when trying to access it in the context of the data object.

Upvotes: 2

Related Questions