Skip to content

Codegen improvements: interfaces/unions, subscriptions, typed variables, builder fixes - #1

Merged
ifmelate merged 11 commits into
masterfrom
feature/codegen-improvements
Oct 10, 2026
Merged

ifmelate merged 11 commits into
masterfrom
feature/codegen-improvements

Conversation

@ifmelate

Copy link
Copy Markdown
Owner

Summary

Generated code for the project's own fixture schema did not compile before this change (86 analyzer errors, json_serializable failed on the SearchResult union). It now compiles with no errors or warnings in both nullability modes, and a new test runs it end to end.

This branch is based on feature/improve-filepath, which was never merged, so it also brings in those commits (1.0.2-rc.1 path handling, AST parser, strict nullability, deterministic output).

Generator

  • Interfaces and unions become abstract classes whose fromJson dispatches on __typename, with Unknown* fallbacks. __typename is added to generated documents.
  • Subscriptions return Streams via GraphQLClient.subscribe. Before, SubscriptionOptions were passed to query, which did not compile.
  • Every operation in a file gets generated, not just the first.
  • Fragments are resolved from other .graphql files.
  • Aliases are read from the alias key.
  • Operations with several root fields return an <Op>Result class instead of silently dropping fields.
  • New in each operation file: <Op>Variables classes with toJson(), plus public <op>Document and parse<Op>Data, usable with watchQuery and any fetch policy.
  • Dart reserved words are escaped (class → class_, enum value default → default_), while the GraphQL name is still sent on the wire.
  • extend type ... schema extensions are merged.

Builder / schema loading

  • The schema is loaded and types.dart generated once per build instead of once per file.
  • Local schemas are read through BuildStep, so editing the schema triggers a rebuild.
  • Removed the hardcoded schema.graphql path check, which skipped files like get_schema.graphql. Schema files are now detected by their content.
  • Output goes through log instead of print. build_runner reported every print as a warning.
  • Remote schemas:
    • new schema_headers option with ${ENV} expansion;
    • 30s timeout;
    • introspection fallback converted to SDL.

Behaviour changes (see CHANGELOG)

  • *Data methods throw OperationException instead of returning null on errors.
  • Custom scalar classes keep the raw JSON value in raw; value is still a string.
  • DateTimeConverter.toJson / LocalDateConverter.toJson return dynamic.
  • The Awesome placeholder is removed.

Known limitation (not addressed)

Generated classes still mirror schema types, not selection sets. Parsing an object throws if the operation did not select one of its non-null fields. This is documented in the README. Fixing it needs selection-set types, which is a major version change.

Test plan

  • dart format --set-exit-if-changed .
  • dart analyze --fatal-infos (was failing on master's base)
  • dart test: 93 tests, including the new compile test that runs json_serializable plus dart analyze on generated code and executes it against a fake link
  • CI on Dart 3.2.0 (not verified locally)

🤖 Generated with Claude Code

ifmelate and others added 11 commits September 17, 2025 00:36
… file://; robust index exports;

- Replace manual string paths with package:path (join, basename, normalize) for Windows/macOS/Linux compatibility
- Parse file:// URIs via p.fromUri and normalize local paths in SchemaDownloader
- Unify schema loading by routing all sources through SchemaDownloader.downloadSchema
- Fix index.dart export generation using p.basename and POSIX '/' in emitted exports
- Keep generated imports relative and platform-agnostic

Files:
- lib/src/builder.dart
- lib/src/schema_downloader.dart

No breaking changes; improves Windows IDE/build_runner compatibility.
- Structured error codes and exceptions with JSON/plain formatting
- Global config context with reset() for test and watch-mode safety
- File naming utilities (snake_case, kebab-case, camelCase, PascalCase)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ixes

- Replace regex parser with AST-based GraphQL parser (package:gql)
- Wire strictNullability config to control list nullability behavior
- Wire diagnosticsFormat into exception formatting with jsonEncode
- Validate fileNaming config values; reject unknown modes
- Fix stale doc comment on _writeIfChanged
- Make _typesFileGenerated instance field (not static) for watch mode
- Use p.join for index file path (cross-platform consistency)
- Fix index file name extraction for all naming modes
- Escape special characters in parser string serialization
- Remove unused variables in schema_analyzer.dart

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Enable strictNullability in setUp for tests that verify strict behavior
- Add tearDown with CodegenConfigContext.reset() for test isolation
- Add config_options_test, determinism_test, diagnostics_test

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Fix strict_nullability default from true to false (matches code)
- Document all configuration options accurately

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Generate interfaces and unions as abstract classes dispatching on
  __typename with Unknown* fallbacks; add __typename to documents
- Subscriptions return Streams via GraphQLClient.subscribe
- Generate every operation of a document, resolve fragments from other
  files, handle aliases and multiple root fields (<Op>Result)
- Emit <Op>Variables classes, document/operation-name constants and
  parse<Op>Data functions
- Escape Dart reserved words in field names and enum values
- Merge schema extensions; support schema_headers and introspection
  fallback with timeout for remote schemas
- Load schema and generate types once per build; read local schemas
  through BuildStep; use log instead of print
- Propagate OperationException instead of returning null
- Remove Awesome placeholder

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Compile generated code with json_serializable + dart analyze and run it
  against a fake link (tagged `compile`)
- Builder tests via build_test
- Unit tests for document utils, naming, introspection-to-SDL, config
- Update expectations for interfaces, unions, subscriptions, mutations

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On Dart 3.2 pub resolves build_test 2.2.2, which rethrows builder
exceptions instead of only logging them like 3.x does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ifmelate
ifmelate merged commit 7a0637d into master Oct 10, 2026
2 checks passed
ifmelate added a commit that referenced this pull request Oct 10, 2026
Codegen improvements: interfaces/unions, subscriptions, typed variables, builder fixes
ifmelate added a commit that referenced this pull request Oct 10, 2026
Codegen improvements: interfaces/unions, subscriptions, typed variables, builder fixes
Sign up for free to 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.

1 participant