Skip to content

[api-codegen-preset] Types for anonymous operations are not generated / anonymous operations are ignored #1997

Open
@jakubnoga

Description

Overview/summary

In retrospective this seems like a reasonable behavior, but might be overlooked by other people as well and cost them some time and nerves. I think it's at least worth mentioning in the docs that anonymous operations are counted as documents but omitted / ignored in generation phase.
Having only anonymous queries in the project will not trigger any errors but will result in empty output *.generated.d.ts

Consider such an example straight from Shopify docs (https://shopify.dev/docs/api/admin-graphql/2024-10/queries/customers?language=Node.js):

const client = new shopify.clients.Graphql({session});
const data = await client.query({
  data: `query {
    customers(first: 5, query: "country:canada") {
      edges {
        node {
          id
        }
      }
    }
  }`,
});

apart from annotating with magic #graphql tag it also needs a name. So to get graphql-codegen going one will have to change above code to something like:

const client = new shopify.clients.Graphql({session});
const data = await client.query({
  data: `
  #graphql
  query customers {
    customers(first: 5, query: "country:canada") {
      edges {
        node {
          id
        }
      }
    }
  }`,
});

There's literally only a single mention of this fact in the graphql-codegen docs and it's in React / Vue chapter (I only found it because I knew what I was looking for): https://the-guild.dev/graphql/codegen/docs/guides/react-vue#writing-graphql-queries

Image

Respective "Vanilla TypeScript" chapter does not contain such a warning.

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions