Is it possible to get rid of the 'data' ,'nodes', ... fields?
Asked Answered
B

1

5

I have the following GraphQL query:

{
  allForums {
    nodes {
      name,
      topics: topicsByForumId(orderBy: [TITLE_ASC]) {
        nodes {
          title
        }
      }
    }
  }
}

This returns something as following:

{
  "data": {
    "allForums": {
      "nodes": [
        {
          "name": "1",
          "topics": {
            "nodes": [
              {
                "title": "a"
              },
              {
                "title": "b"
              }
            ]
          }
        }
      ]
    }
  }
}

I would like to get the result below:

[
    {
        "name": "1",
        "topics": [
            {
                "title": "a"
            },
            {
                "title", "b"
            }
        ]
    }
]

Is it possible to get rid of the data, nodes, ... fields? Is that something that can be done within GraphQL, or should I do that in my service implementation?

I am using PostGraphile v4.2.0 as a GraphQL implementation, on top of PostgreSQL v11.

Bolick answered 27/12, 2018 at 10:19 Comment(0)
S
6

As indicated in the docs, you can expose a simpler interface for connections, or eliminate the default Relay-based connection interface altogether:

If you prefer a simpler list interface over GraphQL connections then you can enable that either along-side our connections (both) or exclusively (only) using our --simple-collections [omit|both|only] option.

Sanskrit answered 27/12, 2018 at 14:2 Comment(4)
👍 Further you can remove this “List” suffix by either writing your own inflector or using pg-simplify-inflectorLevo
@Levo / all: The hint to to the docs Postgraphile above is Ok but radical and rather under-documented. The docs just says >>--simple-collections [omit|both|only] "omit" (default) - relay connections only, "only" - simple collections only (no Relay connections), "both" - both<<. So, configuring "both": What's the query to include or get rid of the "nodes" (and edges)?Dimple
@Levo And BTW: What's the reason to include "edges" and "nodes" in the response - which won't contain any "cursor" info - given the query does not include pageInfo (like "pageInfo { hasNextPage... }")? (My main question remains: How to get rid of nodes/edges in the response?)Dimple
@Dimple You can read about the justification of connections here: graphile.org/postgraphile/connections/#connections; we've recently expanded PostGraphile core to support aggregate operations on connections also which makes them even more powerful. By default, simple collections (which are just lists of the target record, so don't have edges/nodes/pageInfo/aggregates/etc) use the same names as the connection fields, but with List appended to the field name; e.g. allForumsList.Levo

© 2022 - 2024 — McMap. All rights reserved.