question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Operations codegen without documents

See original GitHub issue

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
  }
}
  1. If the return type of that operation is a scalar without field selection -> perfect
  2. If the return type is object which needs field selection, we can generate the document dynamically like:
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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
namnmcommented, Dec 22, 2021

So I am not sure I follow your suggestion

@ardatan Yes sorry you are correct, here is my updated:

  • typescript-operations generate type defs for operations, but we need to fully generate type def of all fields in the return type recursively (look like this is already done)
// for eg the return type of this query is Post from schema
type Post = {
  b1: string
  b2: string
  b3: number
  b4: boolean
  nested: Category
  // ...
}
// here are its references
type Category = {
  nested_1: ID
  nested_2: string
  nested_3: Post[] // can recursively link back to Post
}
// ...
  • Client generation (urql, apollo…) would need some tweak to accept dynamic field selection:
usePostQuery({
  variables: {
     arg1: data,
  },
}, {
  // dynamic field selection
  b1: true,
  b2: true,
  nested: {
    nested_1: true,
  },
})

// I have looked at some other places and see that the dynamic field selection can be more concise like this:
usePostQuery({
  variables: {
     arg1: data,
  },
}, p => p.b1.b2.nested(n => n.nested_1))
  • The typing for dynamic selection will be computed from above generated return type with all fields:
function usePostQuery<T extends Post>(o: PostQueryOption, f: FieldSelection<T>)
  : ComputedSelection<T>
0reactions
zirkelccommented, Dec 3, 2021

Okay, my fault. 😕 I was getting the following error: Plugin "operations-document" requires extension to be '.graphql' !

But this was due to the case that I used the extension .ts on the generated documents. I thought the error was related to the extension of the schema.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ignoring documents if not present · Issue #4989 - GitHub
As of now, I am commenting out the documents statement in the codegen yaml file for every new service that I create and...
Read more >
documents field - GraphQL Code Generator
The documents field should point to your GraphQL documents: query , mutation , subscription , and fragment . documents is only required if...
Read more >
@graphql-codegen/operations-document - npm
GraphQL Code Generator plugin for generating GraphQL operation documents. Latest version: 1.0.0-alpha-17cc8e1de.0, last published: a year ago.
Read more >
@graphql-codegen/operation-documents - npm package | Snyk
All security vulnerabilities belong to production dependencies of direct and indirect packages. License: MIT. Security Policy: No.
Read more >
Swagger Codegen Documentation
The source code for the Swagger Codegen can be found in GitHub. ... If you're not using the latest master to generate API...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found