Skip to content

Create a GraphQL operation (query/mutation) object-oriented builder based on the GraphQL schemaΒ #10578

@MurzNN

Description

@MurzNN

Is your feature request related to a problem? Please describe.

For now, the GraphQL Code Generator tool covers only the results of the GraphQL operations, providing types for them, as I understand.

But composing the GraphQL operations (query/mutation) still happens as a plain string, instead of a structured or typed object, which leads to a bad developer experience.

So, would be great to extend the GraphQL Code Generator project by providing a convenient object-oriented builder of GraphQL operations (query/mutation/etc), that operates with the operation fields, attributes and other items as objects, not just as a string.

Describe the solution you'd like

The idea is to create a tool that allows composing a GraphQL operation, providing not a plain text string with the GraphQL operation text, but an object that can be converted to a string.

So, instead of a code like this:

const postsQueryDocument = /* GraphQL */ `
  query Posts {
    posts {
      id
      title
      author {
        id
        firstName
        lastName
      }
    }
  }
`

have the same structure, but as an object, like this:

const postsQueryDocument: GraphqlOperation = {
  query: {
    Posts: {
      posts: {
        id: true,
        title: true,
        author: {
          id: true,
          firstName: true,
          lastName: true,
        },
      },
    },
  },
};

that can be converted to a string by a utility function.

This approach can provide native Typescript type checking with highlighting all the errors in the operation structure, field and attribute names, etc.

Additionally, users will be able to get a base operation and modify it, without using a regexp magic, something like this:

const postsQueryDocumentWithBody = structuredClone(postsQueryDocument);
// Adds a "body" field to the already existing posts query.
postsQueryDocumentWithBody.query.Posts.posts.body = true;
// Deletes the "author" field from the query.
delete postsQueryDocumentWithBody.query.Posts.posts.author;

Provided examples are for JavaScript, but we can have a similar developer experience in all other languages.

What do you think about this idea?

Describe alternatives you've considered

There is a separate project, implementing this idea, already exists here https://github.com/john-yuan/graphql-toolkit/tree/main/packages/generate-graphql-query

It is able to generate Typescript types in a similar way that the codegen project does, but the format is slightly different. Therefore, users can't reuse the generated Typescript types from codegen and have to manage two separate Typescript files (and the mess with a lot of similar type names in the files) to use both libraries at once.

So, the alternative can be adapting the generate-graphql-query project to work directly with the codegen-generated types.

Any additional important details?

If this is already possible now with the current version of the tool or a plugin, could you please provide an example?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions