Skip to content

feat(core): generate TypeScript types from the GraphQL queries - #475

Open
marcalexiei wants to merge 1 commit into
stats-organization:masterfrom
marcalexiei:generate-grapth-ql-types
Open

feat(core): generate TypeScript types from the GraphQL queries#475
marcalexiei wants to merge 1 commit into
stats-organization:masterfrom
marcalexiei:generate-grapth-ql-types

Conversation

@marcalexiei

Copy link
Copy Markdown

The queries moved to src/graphql/queries/*.graphql and their types are generated from
GitHub's schema instead of hand-written.

  • scripts/generate-graphql-types.js validates each query against @octokit/graphql-schema and writes one file per query plus a shared common.ts,
    so a wrong field fails generation and a wrong variable fails tsc.
  • pnpm check-graphql-types runs in CI, so the generated types can't drift.
  • httpGraphQLRequest takes a generated document: the four *QueryResponse interfaces are gone and GRAPHQL_REPOS_FIELD is a real fragment.
    Untyped request stays exported for the backend, with a TODO for considering its removal.
  • Fixed nullability the hand-written types got wrong
    • (a gist's owner and files[].name, user, repositories.nodes);
    • Lang.color is nullable again
    • parseOwnerAffiliations returns the schema's RepositoryAffiliation.
  • graphql stays on 16: @octokit/graphql-schema needs ^16.

Warning

The four apps/backend contract snapshots were refreshed:
the recorded request text changed, the rendered SVGs did not.

@vercel

vercelBot commented Aug 12, 2026

Copy link
Copy Markdown

@marcalexiei is attempting to deploy a commit to the martin-mfg's projects Team on Vercel.

A member of the Team first needs to authorize it.

@marcalexiei
marcalexieiforce-pushed the generate-grapth-ql-types branch 2 times, most recently from 9b1d028 to 5ccd96cCompareAugust 12, 2026 18:21
@martin-mfg

Copy link
Copy Markdown
Member
  • According to https://the-guild.dev/graphql/codegen/docs/guides/vanilla-typescript, GraphQL Codegen would support code generation without a custom script. I understand that importing GitHub's schema, checking for left-behind generated files, etc. require a custom script. But I wonder if it would be possible to reduce/omit the custom generation logic? The less code we need to maintain, the better.
  • This PR also contains the TypeScript migration for WakaTime. I guess this is not intended?

marcalexiei

This comment was marked as outdated.

@marcalexiei
marcalexieiforce-pushed the generate-grapth-ql-types branch from 5ccd96c to 7aadeacCompareAugust 12, 2026 18:46
@marcalexiei

marcalexiei commented Aug 12, 2026

Copy link
Copy Markdown
Author

The guide recommends @graphql-codegen/cli + client-preset (documentMode: 'string') with a
fetch helper.

Three things moved this PR off it:

  • The CLI can't be installed here:
    its Babel chain pulls semver@6.3.1, which trustPolicy: no-downgrade rejects.
    The script drives @graphql-codegen/core directly.
  • client-preset emits one file; we generate one per query plus a shared common.ts.
    Better fit here: each card already has its own fetcher / api / render file.
  • TypedDocumentString drags @graphql-typed-document-node/core into the published types
    (it implements DocumentTypeDecoration), and is re-emitted per file once output is split
    per query. The local graphqlDocument.ts avoids both.

This costs a 253-line script and one post-process (dropping the Incremental type typescript-operations always emits).
Guide-style would cost 9 codegen devDeps instead of 5, a single file for all queries, and the phantom type in the published types.

P.S.: I reverted the WakaTime card migration.

@martin-mfgmartin-mfg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have only looked at generate-graphql-types.js so far:

const subsetSchemaAst = buildSchema(
[...variableTypeNames]
.map((name) => schemaAst.getType(name))
.filter((type) => !!type && !type.name.startsWith("__"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a comment explaining this line? I tried removing it, but the output didn't change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted all *.graphql files except for gist.graphql and ran pnpm --filter ./packages/core/ run generate-graphql-types. This threw an error. Presumably because there is nothing to be generated for common.ts then. Could you verify and fix this please?

if (checkOnly) {
drifted.push(path.relative(PACKAGE_ROOT, path.join(OUT_DIR, file)));
} else {
await fs.rm(path.join(OUT_DIR, file));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please log a message when deleting files here.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@marcalexiei@martin-mfg