-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improvements for loaders * WIP * New implementation * Use cache instead of preresolvedTypeDefs * Support schema definition * Fix comments * Add tests for shadowed variable * Use absolute: true
- Loading branch information
Showing
149 changed files
with
1,524 additions
and
1,650 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 |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import { GraphQLSchema, buildASTSchema, parse, BuildSchemaOptions } from 'graphql'; | ||
import { GraphQLSchema, buildASTSchema, parse, BuildSchemaOptions, ParseOptions } from 'graphql'; | ||
import { printSchemaWithDirectives } from '.'; | ||
|
||
export function fixSchemaAst(schema: GraphQLSchema, options?: BuildSchemaOptions) { | ||
if (!schema.astNode) { | ||
Object.defineProperty(schema, 'astNode', { | ||
get: () => { | ||
return buildASTSchema(parse(printSchemaWithDirectives(schema)), { | ||
commentDescriptions: true, | ||
...(options || {}), | ||
}).astNode; | ||
}, | ||
}); | ||
Object.defineProperty(schema, 'extensionASTNodes', { | ||
get: () => { | ||
return buildASTSchema(parse(printSchemaWithDirectives(schema)), { | ||
commentDescriptions: true, | ||
...(options || {}), | ||
}).extensionASTNodes; | ||
}, | ||
}); | ||
export function fixSchemaAst(schema: GraphQLSchema, options: BuildSchemaOptions) { | ||
if (!schema.astNode || !schema.extensionASTNodes) { | ||
const schemaWithValidAst = buildASTSchema( | ||
parse(printSchemaWithDirectives(schema), { | ||
noLocation: true, | ||
...(options || {}), | ||
}), | ||
{ | ||
commentDescriptions: true, | ||
...(options || {}), | ||
} | ||
); | ||
if (!schema.astNode) { | ||
schema.astNode = schemaWithValidAst.astNode; | ||
} | ||
if (!schema.extensionASTNodes) { | ||
schema.extensionASTNodes = schemaWithValidAst.extensionASTNodes; | ||
} | ||
} | ||
return schema; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Source as GraphQLSource, buildClientSchema, parse, ParseOptions } from 'graphql'; | ||
import { printSchemaWithDirectives, Source } from '.'; | ||
import { GraphQLSchemaValidationOptions } from 'graphql/type/schema'; | ||
|
||
function stripBOM(content: string): string { | ||
content = content.toString(); | ||
// Remove byte order marker. This catches EF BB BF (the UTF-8 BOM) | ||
// because the buffer-to-string conversion in `fs.readFileSync()` | ||
// translates it to FEFF, the UTF-16 BOM. | ||
if (content.charCodeAt(0) === 0xfeff) { | ||
content = content.slice(1); | ||
} | ||
|
||
return content; | ||
} | ||
|
||
function parseBOM(content: string): any { | ||
return JSON.parse(stripBOM(content)); | ||
} | ||
|
||
export function parseGraphQLJSON(location: string, jsonContent: string, options: ParseOptions & GraphQLSchemaValidationOptions): Source { | ||
let parsedJson = parseBOM(jsonContent); | ||
|
||
if (parsedJson['data']) { | ||
parsedJson = parsedJson['data']; | ||
} | ||
|
||
if (parsedJson.kind === 'Document') { | ||
const document = parsedJson; | ||
return { | ||
location, | ||
document, | ||
}; | ||
} else if (parsedJson.__schema) { | ||
const schema = buildClientSchema(parsedJson, options); | ||
return { | ||
location, | ||
document: parse(printSchemaWithDirectives(schema), options), | ||
schema, | ||
}; | ||
} | ||
throw new Error(`Not valid JSON content`); | ||
} |
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,22 @@ | ||
import { ParseOptions, DocumentNode, parse, Kind, Source as GraphQLSource } from 'graphql'; | ||
|
||
export function parseGraphQLSDL(location: string, rawSDL: string, options: ParseOptions) { | ||
let document: DocumentNode; | ||
try { | ||
document = parse(new GraphQLSource(rawSDL, location), options); | ||
} catch (e) { | ||
if (e.message.includes('EOF')) { | ||
document = { | ||
kind: Kind.DOCUMENT, | ||
definitions: [], | ||
}; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
return { | ||
location, | ||
document, | ||
rawSDL, | ||
}; | ||
} |
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,12 +1,12 @@ | ||
import { Loader, Source } from '@graphql-toolkit/common'; | ||
import { Source } from '@graphql-toolkit/common'; | ||
import { Kind } from 'graphql'; | ||
import { LoadTypedefsOptions, loadTypedefsUsingLoaders, UnnormalizedTypeDefPointer } from './load-typedefs'; | ||
import { LoadTypedefsOptions, loadTypedefs, UnnormalizedTypeDefPointer } from './load-typedefs'; | ||
|
||
export const OPERATION_KINDS = [Kind.OPERATION_DEFINITION, Kind.FRAGMENT_DEFINITION]; | ||
export const NON_OPERATION_KINDS = Object.keys(Kind) | ||
.reduce((prev, v) => [...prev, Kind[v]], []) | ||
.filter(v => !OPERATION_KINDS.includes(v)); | ||
|
||
export async function loadDocumentsUsingLoaders(loaders: Loader[], documentDef: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[], options: LoadTypedefsOptions = {}, cwd = process.cwd()): Promise<Source[]> { | ||
return await loadTypedefsUsingLoaders(loaders, documentDef, { ...options, skipGraphQLImport: true, noRequire: true }, NON_OPERATION_KINDS, cwd); | ||
export function loadDocuments(documentDef: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[], options: LoadTypedefsOptions): Promise<Source[]> { | ||
return loadTypedefs(documentDef, { noRequire: true, filterKinds: NON_OPERATION_KINDS, ...options }); | ||
} |
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
Oops, something went wrong.