Skip to content

Commit

Permalink
Allow codegen to skip schema building when it's not possible (#2120)
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha committed Jul 7, 2019
1 parent 3cb77ab commit eae8448
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/graphql-codegen-cli/src/codegen.ts
Expand Up @@ -5,10 +5,11 @@ import { normalizeOutputParam, normalizeInstanceOrArray, normalizeConfig } from
import { prettify } from './utils/prettier';
import { Renderer } from './utils/listr-renderer';
import { loadSchema, loadDocuments } from './load';
import { GraphQLError, DocumentNode, buildASTSchema } from 'graphql';
import { GraphQLError, DocumentNode } from 'graphql';
import { getPluginByName } from './plugins';
import { getPresetByName } from './presets';
import { debugLog } from './utils/debugging';
import { tryToBuildSchema } from './utils/try-to-build-schema';

export const defaultLoader = (mod: string) => import(mod);

Expand Down Expand Up @@ -214,14 +215,15 @@ export async function executeCodegen(config: Types.Config): Promise<Types.FileOu
};

let outputs: Types.GenerateOptions[] = [];
const builtSchema = tryToBuildSchema(outputSchema);

if (hasPreset) {
outputs = await preset.buildGeneratesSection({
baseOutputDir: filename,
presetConfig: outputConfig.presetConfig || {},
plugins: normalizedPluginsArray,
schema: outputSchema,
schemaAst: buildASTSchema(outputSchema),
schemaAst: builtSchema,
documents: outputDocuments,
config: mergedConfig,
pluginMap,
Expand All @@ -232,7 +234,7 @@ export async function executeCodegen(config: Types.Config): Promise<Types.FileOu
filename,
plugins: normalizedPluginsArray,
schema: outputSchema,
schemaAst: buildASTSchema(outputSchema),
schemaAst: builtSchema,
documents: outputDocuments,
config: mergedConfig,
pluginMap,
Expand Down
12 changes: 12 additions & 0 deletions packages/graphql-codegen-cli/src/utils/try-to-build-schema.ts
@@ -0,0 +1,12 @@
import { DocumentNode, GraphQLSchema, buildASTSchema } from 'graphql';
import { debugLog } from './debugging';

export function tryToBuildSchema(schema: DocumentNode): GraphQLSchema {
try {
return buildASTSchema(schema);
} catch (e) {
debugLog(`Unable to build AST schema from DocumentNode, will try again later...`, e);

return null;
}
}

0 comments on commit eae8448

Please sign in to comment.