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

Add __typename to base TS and flow types #1701

Merged
merged 2 commits into from
May 15, 2019
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
65 changes: 65 additions & 0 deletions packages/plugins/flow/flow/tests/flow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ describe('Flow Plugin', () => {

expect(result).toBeSimilarStringTo(`
export type B = {
__typename?: "B",
/** the id */
id?: ?$ElementType<Scalars, 'ID'>,
};`);
Expand All @@ -235,6 +236,7 @@ describe('Flow Plugin', () => {

expect(result).toBeSimilarStringTo(`
export type Node = {
__typename?: "Node",
/** the id */
id: $ElementType<Scalars, 'ID'>,
};`);
Expand Down Expand Up @@ -298,6 +300,7 @@ describe('Flow Plugin', () => {

expect(result).toBeSimilarStringTo(`
export type MyInterface = {|
__typename?: "MyInterface",
foo?: ?$ElementType<Scalars, 'String'>,
bar: $ElementType<Scalars, 'String'>,
|};
Expand All @@ -322,6 +325,7 @@ describe('Flow Plugin', () => {

expect(result).toBeSimilarStringTo(`
export type MyInterface = {
__typename?: "MyInterface",
+foo?: ?$ElementType<Scalars, 'String'>,
+bar: $ElementType<Scalars, 'String'>,
};
Expand Down Expand Up @@ -354,6 +358,7 @@ describe('Flow Plugin', () => {
`);
expect(result).toBeSimilarStringTo(`
export type mytype = {
__typename?: "MyType",
foo?: ?$ElementType<Scalars, 'String'>,
};
`);
Expand Down Expand Up @@ -397,6 +402,7 @@ describe('Flow Plugin', () => {

expect(result).toBeSimilarStringTo(`
export type Imytype = {
__typename?: "MyType",
foo?: ?$ElementType<Scalars, 'String'>,
};
`);
Expand Down Expand Up @@ -459,35 +465,42 @@ describe('Flow Plugin', () => {
expect(result).toBeSimilarStringTo(`export type myenum = $Values<typeof myenumvalues>;`);

expect(result).toBeSimilarStringTo(`export type mytype = {
__typename?: "MyType",
f?: ?$ElementType<Scalars, 'String'>,
bar?: ?myenum,
b_a_r?: ?$ElementType<Scalars, 'String'>,
myOtherField?: ?$ElementType<Scalars, 'String'>,
};`);

expect(result).toBeSimilarStringTo(`export type my_type = {
__typename?: "My_Type",
linkTest?: ?mytype,
};`);

expect(result).toBeSimilarStringTo(`export type myunion = my_type | mytype;`);

expect(result).toBeSimilarStringTo(`export type some_interface = {
__typename?: "Some_Interface",
id: $ElementType<Scalars, 'ID'>,
};`);

expect(result).toBeSimilarStringTo(`export type impl1 = some_interface & {
__typename?: "Impl1",
id: $ElementType<Scalars, 'ID'>,
};`);

expect(result).toBeSimilarStringTo(`export type impl_2 = some_interface & {
__typename?: "Impl_2",
id: $ElementType<Scalars, 'ID'>,
};`);

expect(result).toBeSimilarStringTo(`export type impl_3 = some_interface & {
__typename?: "impl_3",
id: $ElementType<Scalars, 'ID'>,
};`);

expect(result).toBeSimilarStringTo(`export type query = {
__typename?: "Query",
something?: ?myunion,
use_interface?: ?some_interface,
};`);
Expand All @@ -508,35 +521,42 @@ describe('Flow Plugin', () => {
expect(result).toBeSimilarStringTo(`export type MyEnum = $Values<typeof MyEnumValues>;`);

expect(result).toBeSimilarStringTo(`export type MyType = {
__typename?: "MyType",
f?: ?$ElementType<Scalars, 'String'>,
bar?: ?MyEnum,
b_a_r?: ?$ElementType<Scalars, 'String'>,
myOtherField?: ?$ElementType<Scalars, 'String'>,
};`);

expect(result).toBeSimilarStringTo(`export type My_Type = {
__typename?: "My_Type",
linkTest?: ?MyType,
};`);

expect(result).toBeSimilarStringTo(`export type MyUnion = My_Type | MyType;`);

expect(result).toBeSimilarStringTo(`export type Some_Interface = {
__typename?: "Some_Interface",
id: $ElementType<Scalars, 'ID'>,
};`);

expect(result).toBeSimilarStringTo(`export type Impl1 = Some_Interface & {
__typename?: "Impl1",
id: $ElementType<Scalars, 'ID'>,
};`);

expect(result).toBeSimilarStringTo(`export type Impl_2 = Some_Interface & {
__typename?: "Impl_2",
id: $ElementType<Scalars, 'ID'>,
};`);

expect(result).toBeSimilarStringTo(`export type Impl_3 = Some_Interface & {
__typename?: "impl_3",
id: $ElementType<Scalars, 'ID'>,
};`);

expect(result).toBeSimilarStringTo(`export type Query = {
__typename?: "Query",
something?: ?MyUnion,
use_interface?: ?Some_Interface,
};`);
Expand All @@ -556,35 +576,42 @@ describe('Flow Plugin', () => {
expect(result).toBeSimilarStringTo(`export type IMyEnum = $Values<typeof IMyEnumValues>;`);

expect(result).toBeSimilarStringTo(`export type IMyType = {
__typename?: "MyType",
f?: ?$ElementType<Scalars, 'String'>,
bar?: ?IMyEnum,
b_a_r?: ?$ElementType<Scalars, 'String'>,
myOtherField?: ?$ElementType<Scalars, 'String'>,
};`);

expect(result).toBeSimilarStringTo(`export type IMy_Type = {
__typename?: "My_Type",
linkTest?: ?IMyType,
};`);

expect(result).toBeSimilarStringTo(`export type IMyUnion = IMy_Type | IMyType;`);

expect(result).toBeSimilarStringTo(`export type ISome_Interface = {
__typename?: "Some_Interface",
id: $ElementType<Scalars, 'ID'>,
};`);

expect(result).toBeSimilarStringTo(`export type IImpl1 = ISome_Interface & {
__typename?: "Impl1",
id: $ElementType<Scalars, 'ID'>,
};`);

expect(result).toBeSimilarStringTo(`export type IImpl_2 = ISome_Interface & {
__typename?: "Impl_2",
id: $ElementType<Scalars, 'ID'>,
};`);

expect(result).toBeSimilarStringTo(`export type IImpl_3 = ISome_Interface & {
__typename?: "impl_3",
id: $ElementType<Scalars, 'ID'>,
};`);

expect(result).toBeSimilarStringTo(`export type IQuery = {
__typename?: "Query",
something?: ?IMyUnion,
use_interface?: ?ISome_Interface,
};`);
Expand Down Expand Up @@ -654,6 +681,7 @@ describe('Flow Plugin', () => {

expect(result).toBeSimilarStringTo(`
export type TMutation = {
__typename?: "Mutation",
foo?: ?$ElementType<Scalars, 'String'>,
};

Expand Down Expand Up @@ -819,6 +847,7 @@ describe('Flow Plugin', () => {

expect(result).toBeSimilarStringTo(`
export type MyType = {
__typename?: "MyType",
foo?: ?$ElementType<Scalars, 'String'>,
bar: $ElementType<Scalars, 'String'>,
};
Expand All @@ -840,11 +869,13 @@ describe('Flow Plugin', () => {

expect(result).toBeSimilarStringTo(`
export type MyInterface = {
__typename?: "MyInterface",
foo: $ElementType<Scalars, 'String'>,
};
`);
expect(result).toBeSimilarStringTo(`
export type MyType = MyInterface & {
__typename?: "MyType",
foo: $ElementType<Scalars, 'String'>,
};
`);
Expand All @@ -870,16 +901,19 @@ describe('Flow Plugin', () => {

expect(result).toBeSimilarStringTo(`
export type MyInterface = {
__typename?: "MyInterface",
foo: $ElementType<Scalars, 'String'>,
};
`);
expect(result).toBeSimilarStringTo(`
export type MyOtherInterface = {
__typename?: "MyOtherInterface",
bar: $ElementType<Scalars, 'String'>,
};
`);
expect(result).toBeSimilarStringTo(`
export type MyType = MyInterface & MyOtherInterface & {
__typename?: "MyType",
foo: $ElementType<Scalars, 'String'>,
bar: $ElementType<Scalars, 'String'>,
};
Expand All @@ -901,11 +935,13 @@ describe('Flow Plugin', () => {

expect(result).toBeSimilarStringTo(`
export type MyType = {
__typename?: "MyType",
foo: MyOtherType,
};
`);
expect(result).toBeSimilarStringTo(`
export type MyOtherType = {
__typename?: "MyOtherType",
bar: $ElementType<Scalars, 'String'>,
};
`);
Expand Down Expand Up @@ -947,6 +983,7 @@ describe('Flow Plugin', () => {

expect(result).toBeSimilarStringTo(`
export type MyInterface = {
__typename?: "MyInterface",
foo?: ?$ElementType<Scalars, 'String'>,
bar: $ElementType<Scalars, 'String'>,
};
Expand Down Expand Up @@ -1003,4 +1040,32 @@ describe('Flow Plugin', () => {
validateFlow(result);
});
});

it('should not contain __typename', async () => {
const schema = buildSchema(/* GraphQL */ `
type User {
id: Int!
name: String!
email: String!
}
type QueryRoot {
allUsers: [User]!
userById(id: Int!): User
# Generates a new answer for the guessing game
answer: [Int!]!
}
type SubscriptionRoot {
newUser: User
}
schema {
query: QueryRoot
subscription: SubscriptionRoot
}
`);

const content = await plugin(schema, [], { skipTypename: true }, { outputFile: '' });
expect(content).not.toContain('__typename');

validateFlow(content);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,7 @@ export interface ParsedDocumentsConfig extends ParsedConfig {
addTypename: boolean;
}

export interface RawDocumentsConfig extends RawConfig {
/**
* @name skipTypename
* @type boolean
* @default false
* @description Automatically adds `__typename` field to the generated types, even when they are not specified
* in the selection set.
*
* @example
* ```yml
* config:
* skipTypename: true
* ```
*/
skipTypename?: boolean;
}
export interface RawDocumentsConfig extends RawConfig {}

export class BaseDocumentsVisitor<TRawConfig extends RawDocumentsConfig = RawDocumentsConfig, TPluginConfig extends ParsedDocumentsConfig = ParsedDocumentsConfig> extends BaseVisitor<TRawConfig, TPluginConfig> {
protected _unnamedCounter = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
ScalarTypeDefinitionNode,
UnionTypeDefinitionNode,
StringValueNode,
isUnionType,
isInterfaceType,
} from 'graphql';
import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor';
import { parseMapper } from './mappers';
Expand Down Expand Up @@ -141,13 +143,18 @@ export class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTypesConfig
const originalNode = parent[key] as ObjectTypeDefinitionNode;
const interfaces = originalNode.interfaces && node.interfaces.length > 0 ? originalNode.interfaces.map(i => this.convertName(i)).join(' & ') + (node.fields.length ? ' & ' : '') : '';

const typeDefinition = new DeclarationBlock(this._declarationBlockConfig)
let declarationBlock = new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('type')
.withName(this.convertName(node))
.withContent(interfaces)
.withComment((node.description as any) as string)
.withBlock(node.fields.join('\n')).string;
.withComment((node.description as any) as string);

if (this._parsedConfig.addTypename) {
declarationBlock = declarationBlock.withTypeName(node.name);
}
const typeDefinition = declarationBlock.withBlock(node.fields.join('\n')).string;

const argumentsBlock = this.buildArgumentsBlock(originalNode);

Expand All @@ -157,12 +164,17 @@ export class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTypesConfig
InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode, key: number | string, parent: any): string {
const argumentsBlock = this.buildArgumentsBlock(parent[key] as InterfaceTypeDefinitionNode);

const interfaceDefinition = new DeclarationBlock(this._declarationBlockConfig)
let declarationBlock = new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('type')
.withName(this.convertName(node))
.withComment((node.description as any) as string)
.withBlock(node.fields.join('\n')).string;
.withComment((node.description as any) as string);

if (this._parsedConfig.addTypename) {
declarationBlock = declarationBlock.withTypeName(node.name);
}
const interfaceDefinition = declarationBlock.withBlock(node.fields.join('\n')).string;

return [interfaceDefinition, argumentsBlock].filter(f => f).join('\n\n');
}
Expand Down
17 changes: 17 additions & 0 deletions packages/plugins/other/visitor-plugin-common/src/base-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ParsedConfig {
scalars: ScalarsMap;
convert: ConvertFn;
typesPrefix: string;
addTypename: boolean;
}

export interface RawConfig {
Expand Down Expand Up @@ -80,6 +81,21 @@ export interface RawConfig {
* ```
*/
typesPrefix?: string;

/**
* @name skipTypename
* @type boolean
* @default false
* @description Automatically adds `__typename` field to the generated types, even when they are not specified
* in the selection set.
*
* @example
* ```yml
* config:
* skipTypename: true
* ```
*/
skipTypename?: boolean;
}

export class BaseVisitor<TRawConfig extends RawConfig = RawConfig, TPluginConfig extends ParsedConfig = ParsedConfig> {
Expand All @@ -91,6 +107,7 @@ export class BaseVisitor<TRawConfig extends RawConfig = RawConfig, TPluginConfig
scalars: { ...(defaultScalars || DEFAULT_SCALARS), ...(rawConfig.scalars || {}) },
convert: convertFactory(rawConfig),
typesPrefix: rawConfig.typesPrefix || '',
addTypename: !rawConfig.skipTypename,
...((additionalConfig || {}) as any),
};

Expand Down
Loading