-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(internal): Improve loader by adding a merged loader and incorpora…
…ting `SchemaOrigin` parsing (#165)
- Loading branch information
Showing
8 changed files
with
185 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@gql.tada/cli-utils": patch | ||
"@gql.tada/internal": patch | ||
--- | ||
|
||
Update internal loader to merge them into one and incorporate `SchemaOrigin` parsing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,41 @@ | ||
export type { SchemaLoader } from './types'; | ||
export { loadFromSDL } from './sdl'; | ||
export { loadFromURL } from './url'; | ||
export type * from './types'; | ||
|
||
import path from 'node:path'; | ||
import type { SchemaLoader, SchemaOrigin } from './types'; | ||
import { loadFromSDL } from './sdl'; | ||
import { loadFromURL } from './url'; | ||
|
||
export { loadFromSDL, loadFromURL }; | ||
|
||
const getURLConfig = (origin: SchemaOrigin | null) => { | ||
try { | ||
return ( | ||
origin && { | ||
url: new URL(typeof origin === 'object' ? origin.url : origin), | ||
headers: typeof origin === 'object' ? origin.headers : undefined, | ||
} | ||
); | ||
} catch (_error) { | ||
throw new Error(`Configuration contains an invalid "schema" option`); | ||
} | ||
}; | ||
|
||
export interface LoadConfig { | ||
origin: SchemaOrigin; | ||
rootPath?: string; | ||
fetchInterval?: number; | ||
assumeValid?: boolean; | ||
} | ||
|
||
export function load(config: LoadConfig): SchemaLoader { | ||
const urlOrigin = getURLConfig(origin); | ||
if (urlOrigin) { | ||
return loadFromURL({ ...urlOrigin, interval: config.fetchInterval }); | ||
} else if (typeof origin === 'string') { | ||
const file = config.rootPath ? path.resolve(config.rootPath, origin) : origin; | ||
const assumeValid = config.assumeValid != null ? config.assumeValid : true; | ||
return loadFromSDL({ file, assumeValid }); | ||
} else { | ||
throw new Error(`Configuration contains an invalid "schema" option`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,25 @@ | ||
import type { IntrospectionQuery, GraphQLSchema } from 'graphql'; | ||
|
||
export interface SchemaLoaderResult { | ||
introspection: IntrospectionQuery; | ||
schema: GraphQLSchema; | ||
} | ||
|
||
export type OnSchemaUpdate = (result: SchemaLoaderResult) => void; | ||
|
||
export interface SchemaLoader { | ||
load(reload?: boolean): Promise<SchemaLoaderResult | null>; | ||
notifyOnUpdate(onUpdate: OnSchemaUpdate): () => void; | ||
|
||
/** @internal */ | ||
loadIntrospection(): Promise<IntrospectionQuery | null>; | ||
/** @internal */ | ||
loadSchema(): Promise<GraphQLSchema | null>; | ||
notifyOnUpdate(onUpdate: () => void): () => void; | ||
} | ||
|
||
export type SchemaOrigin = | ||
| string | ||
| { | ||
url: string; | ||
headers?: HeadersInit; | ||
}; |
Oops, something went wrong.