fix(graphql-codegen): use undici fetch and add browserCompatible option #662
+1,026
−550
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes two issues in the fetch process for GraphQL schema introspection (build-time) and adds a new
browserCompatibleconfig option for generated runtime code.Build-time fixes (fetch-schema.ts)
Problem 1: Wrong fetch implementation - The code was using Node.js's global
fetch()instead of undici'sfetch(), which meant thedispatcheroption (used for custom DNS resolution) was being ignored.Problem 2: Incorrect DNS lookup callback - The custom DNS lookup function wasn't handling the
all: trueoption correctly. Whenopts.allis true, the callback expects an array of address objects[{address, family}], not individual parameters(err, address, family).New feature:
browserCompatibleoption for generated codeAdded a new config option to control the fetch implementation in generated runtime code:
true(default): Generated code uses native W3C fetch API (works in browsers and Node.js)false: Generated code uses undici fetch with dispatcher support for localhost DNS resolution (Node.js only - enables proper*.localhostsubdomain resolution on macOS)Updates since last revision
Refactored code generation to use template files instead of string-based generation:
templates/directory containing all static runtime code as real TypeScript filesclient.browser.ts,client.node.ts,orm-client.ts,select-types.ts,query-builder.tsdist/for production useBenefits: Templates are properly typed, easier to maintain, and have IDE support.
Review & Testing Checklist for Human
pnpm build, check thatdist/core/codegen/templates/contains all 5 template filescnc codegenagainst a GraphQL endpoint using a localhost subdomain (e.g.,http://api.localhost:3000/graphql) on macOSbrowserCompatible: falseand verify the generatedclient.tsincludes undici imports and localhost agent logicbrowserCompatible: false, ensure the generated code compiles and runs correctly in a Node.js environment with undici installedRecommended test plan:
pnpm buildand verifydist/core/codegen/templates/exists with all template filescnc codegenagainsthttp://api.localhost:3000/graphqlon macOS to test build-time fixbrowserCompatible: trueandbrowserCompatible: false, inspect the output filesNotes
browserCompatibleoption or template file reading logicbrowserCompatible: false)browserCompatible: false, users must haveundiciinstalled as a runtime dependency