Skip to content

Commit

Permalink
Merge branch 'master' into custom-operation-result-suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha committed Jul 8, 2019
2 parents 84147c3 + 6cf9846 commit 87bdfb8
Show file tree
Hide file tree
Showing 39 changed files with 581 additions and 332 deletions.
20 changes: 0 additions & 20 deletions dev-test/test/codegen.yml

This file was deleted.

23 changes: 0 additions & 23 deletions dev-test/test/schema.graphql

This file was deleted.

10 changes: 0 additions & 10 deletions dev-test/test/src/ComponentA/document.generated.tsx

This file was deleted.

3 changes: 0 additions & 3 deletions dev-test/test/src/ComponentA/document.graphql

This file was deleted.

25 changes: 0 additions & 25 deletions dev-test/test/src/ComponentB/document.generated.tsx

This file was deleted.

14 changes: 0 additions & 14 deletions dev-test/test/src/ComponentB/document.graphql

This file was deleted.

48 changes: 0 additions & 48 deletions dev-test/test/src/document.generated.tsx

This file was deleted.

12 changes: 0 additions & 12 deletions dev-test/test/src/document.graphql

This file was deleted.

31 changes: 0 additions & 31 deletions dev-test/test/src/gql/__generated__/types.ts

This file was deleted.

2 changes: 1 addition & 1 deletion docs/getting-started/programmatic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ fs.writeFile(path.join(__dirname, outputFile), output, () => {
});
```

> We are using this API in the live demo in GraphQL Code Generator website, [here is the code](https://github.com/dotansimha/graphql-code-generator/blob/master/website/live-demo/src/App.js#L79).
> We are using this API in the live demo in GraphQL Code Generator website, [here is the code](https://github.com/dotansimha/graphql-code-generator/blob/master/website/live-demo/src/generate.ts).
3 changes: 2 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"version": "1.3.1",
"npmClient": "yarn",
"useWorkspaces": true
"useWorkspaces": true,
"concurrency": 2
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"jest": "24.8.0",
"jest-junit": "6.4.0",
"lerna": "3.15.0",
"lint-staged": "9.0.2",
"lint-staged": "9.1.0",
"microbundle": "0.11.0",
"react-apollo": "2.5.8",
"react-apollo-hooks": "0.4.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"babylon": "7.0.0-beta.47",
"chalk": "2.4.2",
"change-case": "3.1.0",
"chokidar": "3.0.1",
"chokidar": "3.0.2",
"commander": "2.20.0",
"common-tags": "1.8.0",
"detect-indent": "6.0.0",
Expand Down
8 changes: 5 additions & 3 deletions packages/graphql-codegen-cli/src/codegen.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 3 additions & 3 deletions packages/plugins/flow/flow/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class FlowVisitor extends BaseTypesVisitor<FlowPluginConfig, FlowPluginPa
EnumTypeDefinition(node: EnumTypeDefinitionNode): string {
const typeName = (node.name as any) as string;

if (this.config.enumValues[typeName] && typeof this.config.enumValues[typeName] === 'string') {
if (this.config.enumValues[typeName] && this.config.enumValues[typeName].sourceFile) {
return null;
}

Expand All @@ -99,8 +99,8 @@ export class FlowVisitor extends BaseTypesVisitor<FlowPluginConfig, FlowPluginPa
const optionName = this.convertName(enumOption, { transformUnderscore: true, useTypesPrefix: false });
let enumValue: string = (enumOption.name as any) as string;

if (this.config.enumValues[typeName] && typeof this.config.enumValues[typeName] === 'object' && this.config.enumValues[typeName][enumValue]) {
enumValue = this.config.enumValues[typeName][enumValue];
if (this.config.enumValues[typeName] && this.config.enumValues[typeName].mappedValues && this.config.enumValues[typeName].mappedValues[enumValue]) {
enumValue = this.config.enumValues[typeName].mappedValues[enumValue];
}

return comment + indent(`${optionName}: ${wrapWithSingleQuotes(enumValue)}`);
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/flow/resolvers/src/flow-util-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FLOW_REQUIRE_FIELDS_TYPE = `export type $RequireFields<Origin, Keys> = $Diff<Args, Keys> & $ObjMapi<Keys, <Key>(k: Key) => $NonMaybeType<$ElementType<Origin, Key>>>;`;
2 changes: 1 addition & 1 deletion packages/plugins/flow/resolvers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ${resolverTypeWrapper}
}

return {
prepend: [gqlImports, ...mappersImports],
prepend: [gqlImports, ...mappersImports, ...visitor.globalDeclarations],
content: [header, resolversTypeMapping, resolversParentTypeMapping, ...visitorResult.definitions.filter(d => typeof d === 'string'), getRootResolver(), getAllDirectiveResolvers()].join('\n'),
};
};
8 changes: 7 additions & 1 deletion packages/plugins/flow/resolvers/src/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { FlowResolversPluginConfig } from './index';
import { ListTypeNode, NamedTypeNode, NonNullTypeNode, GraphQLSchema, ScalarTypeDefinitionNode } from 'graphql';
import { ListTypeNode, NamedTypeNode, NonNullTypeNode, GraphQLSchema, ScalarTypeDefinitionNode, InputValueDefinitionNode } from 'graphql';
import * as autoBind from 'auto-bind';
import { indent, ParsedResolversConfig, BaseResolversVisitor, DeclarationBlock } from '@graphql-codegen/visitor-plugin-common';
import { FlowOperationVariablesToObject } from '@graphql-codegen/flow';
import { FLOW_REQUIRE_FIELDS_TYPE } from './flow-util-types';

export interface ParsedFlorResolversConfig extends ParsedResolversConfig {}

Expand All @@ -17,6 +18,11 @@ export class FlowResolversVisitor extends BaseResolversVisitor<FlowResolversPlug
return `$ElementType<Scalars, '${name}'>`;
}

protected applyRequireFields(argsType: string, fields: InputValueDefinitionNode[]): string {
this._globalDeclarations.add(FLOW_REQUIRE_FIELDS_TYPE);
return `$RequireFields<${argsType}, { ${fields.map(f => `${f.name.value}: *`).join(', ')} }>`;
}

protected buildMapperImport(source: string, types: { identifier: string; asDefault?: boolean }[]): string {
if (types[0] && types[0].asDefault) {
return `import type ${types[0].identifier} from '${source}';`;
Expand Down
18 changes: 16 additions & 2 deletions packages/plugins/flow/resolvers/tests/flow-resolvers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,22 @@ describe('Flow Resolvers Plugin', () => {
expect(result).toMatchSnapshot();
});

it('Default values of args and compatibility with typescript plugin', async () => {
const testSchema = buildSchema(/* GraphQL */ `
type Query {
something(arg: String = "default_value"): String
}
`);

const config: any = { noSchemaStitching: true };
const result = (await plugin(testSchema, [], config, { outputFile: '' })) as Types.ComplexPluginOutput;

expect(result.prepend).toContain(`export type $RequireFields<Origin, Keys> = $Diff<Args, Keys> & $ObjMapi<Keys, <Key>(k: Key) => $NonMaybeType<$ElementType<Origin, Key>>>;`);
expect(result.content).toContain(`something?: Resolver<?$ElementType<ResolversTypes, 'String'>, ParentType, ContextType, $RequireFields<QuerySomethingArgs, { arg: * }>>,`);
});

it('Should generate ResolversParentTypes', () => {
const result = plugin(schema, [], {}, { outputFile: '' });
const result = plugin(schema, [], {}, { outputFile: '' }) as Types.ComplexPluginOutput;

expect(result.content).toBeSimilarStringTo(`
/** Mapping between all available schema types and the resolvers parents */
Expand Down Expand Up @@ -72,7 +86,7 @@ describe('Flow Resolvers Plugin', () => {
{
rootValueType: 'MyRoot',
asyncResolverTypes: true,
},
} as any,
{ outputFile: 'graphql.ts' }
)) as Types.ComplexPluginOutput;

Expand Down

0 comments on commit 87bdfb8

Please sign in to comment.