dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.84k stars 1.33k forks source link

Operations codegen without documents #7065

Open namnm opened 2 years ago

namnm commented 2 years ago

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:

    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

ardatan commented 2 years ago

Are you looking for something like this? https://github.com/dotansimha/graphql-code-generator/pull/6861

namnm commented 2 years ago

@ardatan Yes, some thing like that but a bit different:

zirkelc commented 2 years ago

I'd like to add one more suggestion to support JSON schema files (currently only GraphQL SDL files are supported).

ardatan commented 2 years ago

@namnm typescript-operations doesn't generate operations but TypeScript types for operations. So I am not sure I follow your suggestion @zirkelc I am not sure if I understand how it can take JSON Schemas?

zirkelc commented 2 years ago

@namnm typescript-operations doesn't generate operations but TypeScript types for operations. So I am not sure I follow your suggestion @zirkelc I am not sure if I understand how it can take JSON Schemas?

I meant introspection schema in JSON format, like schema.json. I can already use them as schema files for the other plugins, so I thought it might be possible to use them also for this plugin?

ardatan commented 2 years ago

GraphQL Codegen accepts URL, introspection JSON, SDL and so on. The schema passed to the plugin is loaded by codegen so it is supported like any other plugin.

zirkelc commented 2 years ago

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.

namnm commented 2 years ago

So I am not sure I follow your suggestion

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

// 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:
```ts
function usePostQuery<T extends Post>(o: PostQueryOption, f: FieldSelection<T>)
  : ComputedSelection<T>
lucsoft commented 11 months ago

Any updates on this? Would love to create my queries dynamically