You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 43d0f65
Browse filesBrowse the repository at this point in the historyBrowse files
Widen SchemaWithLoaderOptions.loader to accept a custom loader function (#11025)
* Widen SchemaWithLoaderOptions.loader to accept a custom loader function
Allows passing an imported loader function (or a class instance exposing
a `loader` property/method) directly as the `loader` value in a
per-entry `schema` custom-loader config, instead of only a path string
that `@graphql-tools/load` resolves with `require()`.
`@graphql-tools/load`'s `useCustomLoader`/`collectCustomLoader` already
dispatch on a function value at runtime
(`typeof loaderPointer === 'function'`); only the type was missing
this case. Backward compatible — `loader: string` keeps working.
* Add changeset for custom schema loader function type
* Document passing a loader function directly
* Key custom loader functions by identity in the schema cache
The CLI caches loaded schemas under JSON.stringify(schemaPointerMap),
but JSON.stringify drops functions, so distinct custom loader functions
for the same pointer key collide and the second target silently reuses
the first target's schema. Serialize function values by object identity
(via the existing getJsObjectId, widened to object) so different loaders
load separately, while one function reused across targets still loads once.
* Include @graphql-codegen/cli in changeset for the schema-cache fix
* Add tests for function loaders (direct, distinct-per-pointer, reused-once)
* Prettier
---------
Co-authored-by: Eddy Nguyen <ch@eddeee888.me>
Widen `Types.SchemaWithLoaderOptions['loader']` to accept a custom loader function (`CustomSchemaLoaderFn`) in addition to a path string.
7
+
8
+
A per-entry `schema` custom loader (`{ '<pointer>': { loader, ...options } }`) is resolved by `@graphql-tools/load`'s `useCustomLoader`, which already accepts either a path string (resolved via `require()`) or a function value at runtime (`typeof loaderPointer === 'function'`). Only the TypeScript type restricted `loader` to `string`, forcing consumers who pass an imported loader function (or a class instance exposing a bound `loader` property) to cast the `schema` array to bypass the type error, even though it worked correctly at runtime.
9
+
10
+
This is additive and backward compatible — `loader: string` continues to type-check exactly as before.
11
+
12
+
Also fixes a schema-cache collision for function loaders in `@graphql-codegen/cli`: the cache key is `JSON.stringify(schemaPointerMap)`, and `JSON.stringify` drops functions, so two `generates` targets using different loader functions for the same pointer (`{ 'schema.graphql': { loader: fnA } }` vs `{ loader: fnB }`) both keyed as `{"schema.graphql":{}}` and the second silently reused the first's schema. Function values are now keyed by object identity (via the existing `getJsObjectId`), so distinct loaders load separately while one function reused across targets still loads once.
0 commit comments