Skip to content

Commit

Permalink
remove broken isTypeOf call (always undefined in graphql-tools v6) (#…
Browse files Browse the repository at this point in the history
…5039)

* #4216: remove isTypeOf call (always undefined in graphql-tools v6)

* created changeset per bot direction

* #4216: check interfaces.

maybe not needed but committing just in case it comes up later

* added missing test

* added missing changesets

Co-authored-by: Ben Mosher <[email protected]>
  • Loading branch information
dotansimha and benmosher authored Nov 4, 2020
1 parent 0f35e77 commit 612e5e5
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/chilled-trees-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/visitor-plugin-common': patch
---

remove broken isTypeOf call for expanding fragments with flattenGeneratedTypes = true
6 changes: 6 additions & 0 deletions .changeset/nasty-dragons-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/typescript-operations': patch
'@graphql-codegen/flow-operations': patch
---

Remove broken isTypeOf call (always undefined in graphql-tools v6)
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
this._appendToTypeMap(types, typeOnSchema.name, fields);
this._appendToTypeMap(types, typeOnSchema.name, spreadsUsage[typeOnSchema.name]);
this._collectInlineFragments(typeOnSchema, inlines, types);
} else if (isInterfaceType(typeOnSchema) && parentType.isTypeOf(typeOnSchema, null, null)) {
} else if (isInterfaceType(typeOnSchema) && parentType.getInterfaces().includes(typeOnSchema)) {
this._appendToTypeMap(types, parentType.name, fields);
this._appendToTypeMap(types, parentType.name, spreadsUsage[parentType.name]);
this._collectInlineFragments(typeOnSchema, inlines, types);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,59 @@ export type UserQueryQuery = (
"
`;
exports[`TypeScript Operations Plugin Union & Interfaces #4216 - handle fragments against unions and interfaces with flattenGeneratedTypes 1`] = `
"export type Maybe<T> = T | null;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
};
export type Query = {
__typename?: 'Query';
search?: Maybe<Array<Searchable>>;
};
export type Concept = {
id?: Maybe<Scalars['String']>;
};
export type Dimension = Concept & {
__typename?: 'Dimension';
id?: Maybe<Scalars['String']>;
};
export type DimValue = {
__typename?: 'DimValue';
dimension?: Maybe<Dimension>;
value: Scalars['String'];
};
export type Searchable = Dimension | DimValue;
export type SearchPopularQueryVariables = Exact<{ [key: string]: never; }>;
export type SearchPopularQuery = (
{ __typename?: 'Query' }
& { search?: Maybe<Array<(
{ __typename?: 'Dimension' }
& Pick<Dimension, 'id'>
) | (
{ __typename?: 'DimValue' }
& Pick<DimValue, 'value'>
& { dimension?: Maybe<(
{ __typename?: 'Dimension' }
& Pick<Dimension, 'id'>
)> }
)>> }
);
"
`;
exports[`TypeScript Operations Plugin Union & Interfaces Should handle union selection sets with both FragmentSpreads and InlineFragments 1`] = `
"export type UserQueryQueryVariables = Exact<{ [key: string]: never; }>;
Expand Down
156 changes: 156 additions & 0 deletions packages/plugins/typescript/operations/tests/ts-documents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3593,6 +3593,119 @@ describe('TypeScript Operations Plugin', () => {
`);
});

it('#4216 - handle fragments against unions and interfaces with flattenGeneratedTypes', async () => {
const testSchema = buildSchema(/* GraphQL */ `
schema {
query: Query
}
type Query {
search: [Searchable!]
}
interface Concept {
id: String
}
type Dimension implements Concept {
id: String
}
type DimValue {
dimension: Dimension
value: String!
}
union Searchable = Dimension | DimValue
`);

const query = parse(/* GraphQL */ `
query SearchPopular {
search {
...SearchableFragment
}
}
fragment SearchableFragment on Searchable {
...SearchConceptFragment
...SearchDimValueFragment
}
fragment SearchConceptFragment on Concept {
id
}
fragment SearchDimValueFragment on DimValue {
dimension {
...SearchConceptFragment
}
value
}
`);

const config = {
flattenGeneratedTypes: true,
};

const { content } = await plugin(testSchema, [{ location: '', document: query }], config, {
outputFile: 'graphql.ts',
});

const output = await validate(content, config, testSchema);
expect(mergeOutputs([output])).toMatchSnapshot();

expect(output).toBeSimilarStringTo(`
export type Maybe<T> = T | null;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
};
export type Query = {
__typename?: 'Query';
search?: Maybe<Array<Searchable>>;
};
export type Concept = {
id?: Maybe<Scalars['String']>;
};
export type Dimension = Concept & {
__typename?: 'Dimension';
id?: Maybe<Scalars['String']>;
};
export type DimValue = {
__typename?: 'DimValue';
dimension?: Maybe<Dimension>;
value: Scalars['String'];
};
export type Searchable = Dimension | DimValue;
export type SearchPopularQueryVariables = Exact<{ [key: string]: never; }>;
export type SearchPopularQuery = (
{ __typename?: 'Query' }
& { search?: Maybe<Array<(
{ __typename?: 'Dimension' }
& Pick<Dimension, 'id'>
) | (
{ __typename?: 'DimValue' }
& Pick<DimValue, 'value'>
& { dimension?: Maybe<(
{ __typename?: 'Dimension' }
& Pick<Dimension, 'id'>
)> }
)>> }
);`);
});

it('Should add operation name when addOperationExport is true', async () => {
const testSchema = buildSchema(/* GraphQL */ `
type User {
Expand Down Expand Up @@ -3788,6 +3901,49 @@ describe('TypeScript Operations Plugin', () => {
});

describe('Issues', () => {
it('#4389 - validate issues with interfaces', async () => {
const testSchema = buildSchema(/* GraphQL */ `
interface A {
a: String!
}
interface B implements A {
a: String!
b: String
}
type C implements B {
a: String!
b: String
c: String!
}
type Query {
foo: C
}
`);

const query = parse(/* GraphQL */ `
query {
foo {
... on A {
a
}
}
}
`);

const { content } = await plugin(
testSchema,
[{ location: '', document: query }],
{},
{
outputFile: 'graphql.ts',
}
);
expect(content).toContain(`{ foo?: Maybe<{ __typename?: 'C' }> }`);
});

it('#5001 - incorrect output with typeSuffix', async () => {
const testSchema = buildSchema(/* GraphQL */ `
type Query {
Expand Down

0 comments on commit 612e5e5

Please sign in to comment.