graphql / graphql/graphql-spec

New directive which enables easier access to deeply nested variables

Open
#654 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
14.6k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

I often use GraphQL to make accessing nested data act more similar to the way it is done with object (ie `obj.prop.subprop.targetValue`). Unfortunately, GraphQL has no way to say "give me targetValue or bust," so what I end up doing is requesting a big multilayered object, then doing lots and lots of checks to make sure the value is there.

Something like this:
```
const resp = await queryGQL(`
User(id: "123") {
Story(id: "123") {
Event(type: "beginning") {
timestamp
}
}
}
`);

if (!resp
|| !resp.data
|| !resp.data.User
|| !resp.data.User.Story
|| !resp.data.User.Story.Event
) throw apiErr(`Could not find Event`);
```

It would be really nice to be able do say something like:

```
const resp = await queryGQL(`
User(id: "123") {
Story(id: "123") {
// brackets symbolize where "data" begins
<>
}
}
`);

if (!resp || !resp.data) throw apiErr(`Could not find event`);

const event = resp.data;
console.log("It all started in " + event.timestamp);
```

As long as a query only has one set of request brackets `<<>>` and none of the layers above it request properties, it would be valid. And it would really help with digging in quickly to a multilayered data structure.

As an alternative, I'm thinking of trying to build a query builder and then adding all sorts of features and watching it never take off then become deprecated... which is hardly a solution compared to it being added as a core feature of the query syntax.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.