dotansimha / dotansimha/graphql-code-generator

Operations codegen without documents

Open
#7,065 9 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

Right now to generate operations we must explicitly write `*.graphql` or `gql` tag query/mutation documents, which are pretty verbose of all those documents.
I would like to discuss if we can have a plugin to generate operations without writing the documents:

1. Because the operations are generated automatically, so they can contain only 1 query/mutation per operation. We need to manually write the document if we want combined query (like what we are doing right now):
```
// need to manually write combined operations
query combined($arg1: Arg1, $arg2: Arg2) {
b(arg1: $arg1) {
b1
b2
nested: {
nested_1
}
}
c(arg2: $arg2) {
c1
c2
}
}
```

2. If the return type of that operation is a scalar without field selection -> perfect
3. If the return type is object which needs field selection, we can generate the document dynamically like:
```js
useBQuery({
variables: {
arg1: data,
},
}, {
// selected fields
b1: true,
b2: true,
nested: {
nested_1: true,
},
})
// which in the generated code it would dynamically generate the document as:
const doc = gql`
query b($arg1: Arg1) {
b(arg1: $arg1) {
${computeSelection(selectedFields)}
}
}
`
// then execute the query as usual...
```
We can easily write the `computeSelection` function, also with typescript nested typing: https://stackoverflow.com/a/54949737

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.