-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
101 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
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,98 +1,61 @@ | ||
import { UnifiedGraphManagerOptions } from '@graphql-mesh/fusion-runtime'; | ||
import { defaultImportFn, isUrl, readFileOrUrl } from '@graphql-mesh/utils'; | ||
import { defaultPrintFn } from '@graphql-tools/executor-common'; | ||
import type { MaybePromise } from '@graphql-tools/utils'; | ||
import { | ||
getDocumentNodeFromSchema, | ||
isDocumentNode, | ||
isValidPath, | ||
mapMaybePromise, | ||
printSchemaWithDirectives, | ||
} from '@graphql-tools/utils'; | ||
import type { DocumentNode, GraphQLSchema } from 'graphql'; | ||
import { buildASTSchema, isSchema, parse, print } from 'graphql'; | ||
import { isSchema } from 'graphql'; | ||
import type { GatewayConfigContext } from './types'; | ||
|
||
export type UnifiedGraphSchema = GraphQLSchema | DocumentNode | string; | ||
export type UnifiedGraphSchema = Awaited< | ||
ReturnType<UnifiedGraphManagerOptions<unknown>['getUnifiedGraph']> | ||
>; | ||
|
||
export type UnifiedGraphConfig = | ||
| UnifiedGraphSchema | ||
| Promise<UnifiedGraphSchema> | ||
| (() => UnifiedGraphSchema | Promise<UnifiedGraphSchema>); | ||
| (( | ||
configContext: GatewayConfigContext, | ||
) => UnifiedGraphSchema | Promise<UnifiedGraphSchema>); | ||
|
||
export function handleUnifiedGraphConfig( | ||
config: UnifiedGraphConfig, | ||
configContext: GatewayConfigContext, | ||
): MaybePromise<GraphQLSchema> { | ||
): MaybePromise<UnifiedGraphSchema> { | ||
return mapMaybePromise( | ||
typeof config === 'function' ? config() : config, | ||
typeof config === 'function' ? config(configContext) : config, | ||
(schema) => handleUnifiedGraphSchema(schema, configContext), | ||
); | ||
} | ||
|
||
export const unifiedGraphASTMap = new WeakMap<GraphQLSchema, DocumentNode>(); | ||
export const unifiedGraphSDLMap = new WeakMap<GraphQLSchema, string>(); | ||
|
||
export function getUnifiedGraphSDL(schema: GraphQLSchema) { | ||
let sdl = unifiedGraphSDLMap.get(schema); | ||
if (!sdl) { | ||
const ast = getUnifiedGraphAST(schema); | ||
sdl = print(ast); | ||
unifiedGraphSDLMap.set(schema, sdl); | ||
} | ||
return sdl; | ||
} | ||
|
||
export function getUnifiedGraphAST(schema: GraphQLSchema) { | ||
let ast = unifiedGraphASTMap.get(schema); | ||
if (!ast) { | ||
ast = getDocumentNodeFromSchema(schema); | ||
unifiedGraphASTMap.set(schema, ast); | ||
export function getUnifiedGraphSDL(unifiedGraphSchema: UnifiedGraphSchema) { | ||
if (isSchema(unifiedGraphSchema)) { | ||
return printSchemaWithDirectives(unifiedGraphSchema); | ||
} else if (isDocumentNode(unifiedGraphSchema)) { | ||
return defaultPrintFn(unifiedGraphSchema); | ||
} | ||
return ast; | ||
return unifiedGraphSchema; | ||
} | ||
|
||
export function handleUnifiedGraphSchema( | ||
unifiedGraphSchema: UnifiedGraphSchema, | ||
configContext: GatewayConfigContext, | ||
): Promise<GraphQLSchema> | GraphQLSchema { | ||
if (isSchema(unifiedGraphSchema)) { | ||
return unifiedGraphSchema; | ||
} | ||
if (isDocumentNode(unifiedGraphSchema)) { | ||
const schema = buildASTSchema(unifiedGraphSchema, { | ||
assumeValid: true, | ||
assumeValidSDL: true, | ||
): MaybePromise<UnifiedGraphSchema> { | ||
if ( | ||
typeof unifiedGraphSchema === 'string' && | ||
(isValidPath(unifiedGraphSchema) || isUrl(unifiedGraphSchema)) | ||
) { | ||
return readFileOrUrl<string>(unifiedGraphSchema, { | ||
fetch: configContext.fetch, | ||
cwd: configContext.cwd, | ||
logger: configContext.logger, | ||
allowUnknownExtensions: true, | ||
importFn: defaultImportFn, | ||
}); | ||
unifiedGraphASTMap.set(schema, unifiedGraphSchema); | ||
return schema; | ||
} | ||
if (typeof unifiedGraphSchema === 'string') { | ||
if (isValidPath(unifiedGraphSchema) || isUrl(unifiedGraphSchema)) { | ||
return readFileOrUrl<string>(unifiedGraphSchema, { | ||
fetch: configContext.fetch, | ||
cwd: configContext.cwd, | ||
logger: configContext.logger, | ||
allowUnknownExtensions: true, | ||
importFn: defaultImportFn, | ||
}).then((sdl) => handleUnifiedGraphSchema(sdl, configContext)); | ||
} | ||
try { | ||
const ast = parse(unifiedGraphSchema, { | ||
noLocation: true, | ||
}); | ||
const schema = buildASTSchema(ast, { | ||
assumeValid: true, | ||
assumeValidSDL: true, | ||
}); | ||
unifiedGraphSDLMap.set(schema, unifiedGraphSchema); | ||
unifiedGraphASTMap.set(schema, ast); | ||
return schema; | ||
} catch (e) { | ||
configContext.logger.error( | ||
`Failed to build UnifiedGraphSchema from "${unifiedGraphSchema}"`, | ||
); | ||
throw e; | ||
} | ||
} | ||
throw new Error( | ||
`Invalid UnifiedGraphSchema "${unifiedGraphSchema}". It can be an SDL string, an instance of GraphQLSchema or DocumentNode, or a function that returns/resolves any of these.`, | ||
); | ||
return unifiedGraphSchema; | ||
} |