Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix declarationKind=interface, type syntax errors #3505

Merged
merged 4 commits into from
Feb 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/graphql-codegen-cli/tests/codegen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,12 @@ describe('Codegen Executor', () => {

expect(output.length).toBe(1);
expect(output[0].content).toBeSimilarStringTo(`export type Scalars = {
ID: string,
String: string,
Boolean: boolean,
Int: number,
Float: number,
UniqueID: any,
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
UniqueID: any;
};`);
});
});
Expand Down
6 changes: 5 additions & 1 deletion packages/plugins/flow/flow/src/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NonNullTypeNode, ListTypeNode, ObjectTypeDefinitionNode, FieldDefinitionNode, EnumTypeDefinitionNode, NamedTypeNode, GraphQLSchema, InputValueDefinitionNode, Kind, GraphQLEnumType } from 'graphql';
import { BaseTypesVisitor, DeclarationBlock, wrapWithSingleQuotes, indent, ParsedTypesConfig, transformComment, getConfigValue } from '@graphql-codegen/visitor-plugin-common';
import { BaseTypesVisitor, DeclarationBlock, wrapWithSingleQuotes, indent, ParsedTypesConfig, transformComment, getConfigValue, DeclarationKind } from '@graphql-codegen/visitor-plugin-common';
import autoBind from 'auto-bind';
import { FlowPluginConfig } from './config';
import { FlowOperationVariablesToObject } from './flow-variables-to-object';
Expand Down Expand Up @@ -145,4 +145,8 @@ export class FlowVisitor extends BaseTypesVisitor<FlowPluginConfig, FlowPluginPa

return [enumValues, enumType].join('\n\n');
}

protected getPunctuation(declarationKind: DeclarationKind): string {
return declarationKind === 'type' ? ',' : ';';
}
}
28 changes: 14 additions & 14 deletions packages/plugins/flow/flow/tests/__snapshots__/flow.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type TMutation = {|

export type TMutationFooArgs = {|
id?: ?$ElementType<Scalars, 'String'>,
input?: ?TInput
input?: ?TInput,
|};
"
`;
Expand All @@ -47,7 +47,7 @@ export type MyTypeFooArgs = {|
a: $ElementType<Scalars, 'String'>,
b?: ?$ElementType<Scalars, 'String'>,
c?: ?Array<?$ElementType<Scalars, 'String'>>,
d: Array<$ElementType<Scalars, 'Int'>>
d: Array<$ElementType<Scalars, 'Int'>>,
|};
"
`;
Expand All @@ -71,7 +71,7 @@ export type MyType = {|
export type MyTypeFooArgs = {|
a: $ElementType<Scalars, 'String'>,
b: $ElementType<Scalars, 'String'>,
c?: ?$ElementType<Scalars, 'String'>
c?: ?$ElementType<Scalars, 'String'>,
|};
"
`;
Expand Down Expand Up @@ -101,7 +101,7 @@ export type MyTypeFooArgs = {|
b: MyInput,
c?: ?Array<?MyInput>,
d: Array<?MyInput>,
e: Array<MyInput>
e: Array<MyInput>,
|};
"
`;
Expand Down Expand Up @@ -173,7 +173,7 @@ export type Mutation = {|

export type MutationUpdateUserArgs = {|
userId: $ElementType<Scalars, 'ID'>,
update?: ?UpdateUser
update?: ?UpdateUser,
|};

export type UpdateUser = {|
Expand Down Expand Up @@ -440,7 +440,7 @@ export type Imytypefooargs = {|
a: $ElementType<Scalars, 'String'>,
b?: ?$ElementType<Scalars, 'String'>,
c?: ?Array<?$ElementType<Scalars, 'String'>>,
d: Array<$ElementType<Scalars, 'Int'>>
d: Array<$ElementType<Scalars, 'Int'>>,
|};
"
`;
Expand All @@ -465,7 +465,7 @@ export type mytypefooargs = {|
a: $ElementType<Scalars, 'String'>,
b?: ?$ElementType<Scalars, 'String'>,
c?: ?Array<?$ElementType<Scalars, 'String'>>,
d: Array<$ElementType<Scalars, 'Int'>>
d: Array<$ElementType<Scalars, 'Int'>>,
|};
"
`;
Expand Down Expand Up @@ -730,7 +730,7 @@ export type TMutation = {|

export type TMutationFooArgs = {|
id?: ?$ElementType<Scalars, 'String'>,
input?: ?TInput
input?: ?TInput,
|};
"
`;
Expand All @@ -755,7 +755,7 @@ export type MyTypeFooArgs = {|
a: $ElementType<Scalars, 'String'>,
b?: ?$ElementType<Scalars, 'String'>,
c?: ?Array<?$ElementType<Scalars, 'String'>>,
d: Array<$ElementType<Scalars, 'Int'>>
d: Array<$ElementType<Scalars, 'Int'>>,
|};
"
`;
Expand All @@ -779,7 +779,7 @@ export type MyType = {|
export type MyTypeFooArgs = {|
a: $ElementType<Scalars, 'String'>,
b: $ElementType<Scalars, 'String'>,
c?: ?$ElementType<Scalars, 'String'>
c?: ?$ElementType<Scalars, 'String'>,
|};
"
`;
Expand Down Expand Up @@ -809,7 +809,7 @@ export type MyTypeFooArgs = {|
b: MyInput,
c?: ?Array<?MyInput>,
d: Array<?MyInput>,
e: Array<MyInput>
e: Array<MyInput>,
|};
"
`;
Expand Down Expand Up @@ -881,7 +881,7 @@ export type Mutation = {|

export type MutationUpdateUserArgs = {|
userId: $ElementType<Scalars, 'ID'>,
update?: ?UpdateUser
update?: ?UpdateUser,
|};

export type UpdateUser = {|
Expand Down Expand Up @@ -1148,7 +1148,7 @@ export type Imytypefooargs = {|
a: $ElementType<Scalars, 'String'>,
b?: ?$ElementType<Scalars, 'String'>,
c?: ?Array<?$ElementType<Scalars, 'String'>>,
d: Array<$ElementType<Scalars, 'Int'>>
d: Array<$ElementType<Scalars, 'Int'>>,
|};
"
`;
Expand All @@ -1173,7 +1173,7 @@ export type mytypefooargs = {|
a: $ElementType<Scalars, 'String'>,
b?: ?$ElementType<Scalars, 'String'>,
c?: ?Array<?$ElementType<Scalars, 'String'>>,
d: Array<$ElementType<Scalars, 'Int'>>
d: Array<$ElementType<Scalars, 'Int'>>,
|};
"
`;
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/flow/flow/tests/flow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ describe('Flow Plugin', () => {
a: $ElementType<Scalars, 'String'>,
b?: ?$ElementType<Scalars, 'String'>,
c?: ?Array<?$ElementType<Scalars, 'String'>>,
d: Array<$ElementType<Scalars, 'Int'>>
d: Array<$ElementType<Scalars, 'Int'>>,
|};
`);

Expand Down
6 changes: 5 additions & 1 deletion packages/plugins/flow/operations/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FlowWithPickSelectionSetProcessor } from './flow-selection-set-processo
import { GraphQLSchema, isListType, GraphQLObjectType, GraphQLNonNull, GraphQLList, isEnumType } from 'graphql';
import { FlowDocumentsPluginConfig } from './config';
import { FlowOperationVariablesToObject } from '@graphql-codegen/flow';
import { PreResolveTypesProcessor, ParsedDocumentsConfig, BaseDocumentsVisitor, LoadedFragment, SelectionSetProcessorConfig, SelectionSetToObject, getConfigValue } from '@graphql-codegen/visitor-plugin-common';
import { PreResolveTypesProcessor, ParsedDocumentsConfig, BaseDocumentsVisitor, LoadedFragment, SelectionSetProcessorConfig, SelectionSetToObject, getConfigValue, DeclarationKind } from '@graphql-codegen/visitor-plugin-common';
import { isNonNullType } from 'graphql';
import autoBind from 'auto-bind';

Expand Down Expand Up @@ -63,4 +63,8 @@ export class FlowDocumentsVisitor extends BaseDocumentsVisitor<FlowDocumentsPlug
this.setSelectionSetHandler(new SelectionSetToObject(processor, this.scalars, this.schema, this.convertName.bind(this), allFragments, this.config));
this.setVariablesTransformer(new FlowOperationVariablesToObject(this.scalars, this.convertName.bind(this), this.config.namespacedImportName, enumsNames, this.config.enumPrefix));
}

protected getPunctuation(declarationKind: DeclarationKind): string {
return declarationKind === 'type' ? ',' : ';';
}
}
4 changes: 2 additions & 2 deletions packages/plugins/flow/operations/tests/flow-documents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ describe('Flow Operations Plugin', () => {
);
expect(result).toBeSimilarStringTo(
`export type MeQueryVariables = {
repoFullName: $ElementType<Scalars, 'String'>
repoFullName: $ElementType<Scalars, 'String'>,
};`
);
expect(result).toBeSimilarStringTo(`
Expand Down Expand Up @@ -997,7 +997,7 @@ describe('Flow Operations Plugin', () => {
mandatoryInput: InputType,
testArray?: ?Array<?$ElementType<Scalars, 'String'>>,
requireString: Array<?$ElementType<Scalars, 'String'>>,
innerRequired: Array<$ElementType<Scalars, 'String'>>
innerRequired: Array<$ElementType<Scalars, 'String'>>,
};`
);
validateFlow(result);
Expand Down
6 changes: 5 additions & 1 deletion packages/plugins/flow/resolvers/src/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ListTypeNode, NamedTypeNode, NonNullTypeNode, GraphQLSchema, ScalarTypeDefinitionNode, InputValueDefinitionNode } from 'graphql';
import autoBind from 'auto-bind';
import { RawResolversConfig, indent, ParsedResolversConfig, BaseResolversVisitor, DeclarationBlock } from '@graphql-codegen/visitor-plugin-common';
import { RawResolversConfig, indent, ParsedResolversConfig, BaseResolversVisitor, DeclarationBlock, DeclarationKind } from '@graphql-codegen/visitor-plugin-common';
import { FlowOperationVariablesToObject } from '@graphql-codegen/flow';
import { FLOW_REQUIRE_FIELDS_TYPE } from './flow-util-types';

Expand Down Expand Up @@ -104,4 +104,8 @@ export class FlowResolversVisitor extends BaseResolversVisitor<RawResolversConfi
)
.withBlock([indent(`...GraphQLScalarTypeConfig<${baseName}, any>`), indent(`name: '${node.name}'`)].join(', \n')).string;
}

protected getPunctuation(declarationKind: DeclarationKind): string {
return declarationKind === 'type' ? ',' : ';';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export type ResolversParentTypes = {

export type MyDirectiveDirectiveArgs = { arg: $ElementType<Scalars, 'Int'>,
arg2: $ElementType<Scalars, 'String'>,
arg3: $ElementType<Scalars, 'Boolean'> };
arg3: $ElementType<Scalars, 'Boolean'>, };

export type MyDirectiveDirectiveResolver<Result, Parent, ContextType = any, Args = MyDirectiveDirectiveArgs> = DirectiveResolverFn<Result, Parent, ContextType, Args>;

Expand Down
10 changes: 5 additions & 5 deletions packages/plugins/flow/resolvers/tests/mapping.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ describe('ResolversTypes', () => {

expect(result.content).toBeSimilarStringTo(`
export type MyDirectiveDirectiveArgs = { arg: $ElementType<Scalars, 'Int'>,
arg2: $ElementType<Scalars, 'String'>, arg3: $ElementType<Scalars, 'Boolean'> };
arg2: $ElementType<Scalars, 'String'>, arg3: $ElementType<Scalars, 'Boolean'>, };
`);

expect(result.content).toBeSimilarStringTo(`
Expand Down Expand Up @@ -285,7 +285,7 @@ describe('ResolversTypes', () => {
expect(result.prepend).toContain(`import { type MyCustomOtherType } from './my-file';`);
expect(result.content).toBeSimilarStringTo(`
export type MyDirectiveDirectiveArgs = { arg: $ElementType<Scalars, 'Int'>,
arg2: $ElementType<Scalars, 'String'>, arg3: $ElementType<Scalars, 'Boolean'> };
arg2: $ElementType<Scalars, 'String'>, arg3: $ElementType<Scalars, 'Boolean'>, };
`);

expect(result.content).toBeSimilarStringTo(`
Expand Down Expand Up @@ -361,7 +361,7 @@ describe('ResolversTypes', () => {

expect(result.content).toBeSimilarStringTo(`
export type MyDirectiveDirectiveArgs = { arg: $ElementType<Scalars, 'Int'>,
arg2: $ElementType<Scalars, 'String'>, arg3: $ElementType<Scalars, 'Boolean'> };
arg2: $ElementType<Scalars, 'String'>, arg3: $ElementType<Scalars, 'Boolean'>, };
`);

expect(result.content).toBeSimilarStringTo(`
Expand Down Expand Up @@ -435,7 +435,7 @@ describe('ResolversTypes', () => {

expect(result.content).toBeSimilarStringTo(`
export type MyDirectiveDirectiveArgs = { arg: $ElementType<Scalars, 'Int'>,
arg2: $ElementType<Scalars, 'String'>, arg3: $ElementType<Scalars, 'Boolean'> };
arg2: $ElementType<Scalars, 'String'>, arg3: $ElementType<Scalars, 'Boolean'>, };
`);

expect(result.content).toBeSimilarStringTo(`
Expand Down Expand Up @@ -511,7 +511,7 @@ describe('ResolversTypes', () => {

expect(result.content).toBeSimilarStringTo(`
export type MyDirectiveDirectiveArgs = { arg: $ElementType<Scalars, 'Int'>,
arg2: $ElementType<Scalars, 'String'>, arg3: $ElementType<Scalars, 'Boolean'> };
arg2: $ElementType<Scalars, 'String'>, arg3: $ElementType<Scalars, 'Boolean'>, };
`);

expect(result.content).toBeSimilarStringTo(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ export class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTypesConfig
const scalarValue = this.config.scalars[scalarName].type;
const scalarType = this._schema.getType(scalarName);
const comment = scalarType && scalarType.astNode && scalarType.description ? transformComment(scalarType.description, 1) : '';
const { type } = this._parsedConfig.declarationKind;
const { scalar } = this._parsedConfig.declarationKind;

return comment + indent(`${scalarName}: ${scalarValue}${type === 'class' ? ';' : ','}`);
return comment + indent(`${scalarName}: ${scalarValue}${this.getPunctuation(scalar)}`);
});

return new DeclarationBlock(this._declarationBlockConfig)
Expand Down Expand Up @@ -187,8 +187,9 @@ export class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTypesConfig

InputValueDefinition(node: InputValueDefinitionNode): string {
const comment = transformComment((node.description as any) as string, 1);
const { input } = this._parsedConfig.declarationKind;

return comment + indent(`${node.name}: ${node.type},`);
return comment + indent(`${node.name}: ${node.type}${this.getPunctuation(input)}`);
}

Name(node: NameNode): string {
Expand All @@ -200,7 +201,7 @@ export class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTypesConfig
const comment = transformComment((node.description as any) as string, 1);
const { type } = this._parsedConfig.declarationKind;

return comment + indent(`${node.name}: ${typeString}${type === 'class' ? ';' : ','}`);
return comment + indent(`${node.name}: ${typeString}${this.getPunctuation(type)}`);
}

UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string {
Expand All @@ -227,7 +228,7 @@ export class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTypesConfig
getObjectTypeDeclarationBlock(node: ObjectTypeDefinitionNode, originalNode: ObjectTypeDefinitionNode): DeclarationBlock {
const optionalTypename = this.config.nonOptionalTypename ? '__typename' : '__typename?';
const { type } = this._parsedConfig.declarationKind;
const allFields = [...(this.config.addTypename ? [indent(`${this.config['immutableTypes'] ? 'readonly' : ''} ${optionalTypename}: '${node.name}'${type === 'class' ? ';' : ','}`)] : []), ...node.fields] as string[];
const allFields = [...(this.config.addTypename ? [indent(`${this.config['immutableTypes'] ? 'readonly' : ''} ${optionalTypename}: '${node.name}'${this.getPunctuation(type)}`)] : []), ...node.fields] as string[];
const interfacesNames = originalNode.interfaces ? originalNode.interfaces.map(i => this.convertName(i)) : [];

const declarationBlock = new DeclarationBlock(this._declarationBlockConfig)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ScalarsMap, ParsedScalarsMap, NamingConvention, ConvertFn, ConvertOptions, LoadedFragment, NormalizedScalarsMap } from './types';
import { ScalarsMap, ParsedScalarsMap, NamingConvention, ConvertFn, ConvertOptions, LoadedFragment, NormalizedScalarsMap, DeclarationKind } from './types';
import { DeclarationBlockConfig } from './utils';
import autoBind from 'auto-bind';
import { convertFactory } from './naming';
Expand Down Expand Up @@ -148,4 +148,8 @@ export class BaseVisitor<TRawConfig extends RawConfig = RawConfig, TPluginConfig

return (useTypesPrefix ? this.config.typesPrefix : '') + this.config.convert(node, options);
}

protected getPunctuation(declarationKind: DeclarationKind): string {
return '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class OperationVariablesToObject {
return null;
}

return variablesNode.map(variable => indent(this.transformVariable(variable))).join(',\n');
return variablesNode.map(variable => indent(this.transformVariable(variable))).join(`${this.getPunctuation()}\n`) + this.getPunctuation();
}

protected getScalar(name: string): string {
Expand Down Expand Up @@ -101,4 +101,8 @@ export class OperationVariablesToObject {

return fieldType;
}

protected getPunctuation(): string {
return ',';
}
}
Loading