Skip to content

Commit

Permalink
fix: unmerge subtype which where accidentally merged
Browse files Browse the repository at this point in the history
  • Loading branch information
ChibiBlasphem committed Sep 14, 2020
1 parent 6238c94 commit 922d821
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 20 deletions.
9 changes: 7 additions & 2 deletions fixtures/generator/UnionTypesQuery.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { UnionTypesQuery_currentUser_User, UnionTypesQuery_currentUser_AuthUser } from "../UnionTypesQuery.ts";
import { UnionTypesQuery_currentUser_User_address, UnionTypesQuery_currentUser_User, UnionTypesQuery_currentUser_AuthUser } from "../UnionTypesQuery.ts";
interface GqlObject<T extends string = string> {
__typename: T;
}
export const CurrentUserUserAddress = {
isAddress: (gqlObject: void | object | GqlObject | null | undefined): gqlObject is UnionTypesQuery_currentUser_User_address => {
return !!gqlObject && "__typename" in gqlObject && gqlObject.__typename === "Address" && "id" in gqlObject && "streetNumber" in gqlObject && "streetName" in gqlObject;
}
};
export const CurrentUser = {
isUser: (gqlObject: void | object | GqlObject | null | undefined): gqlObject is UnionTypesQuery_currentUser_User => {
return !!gqlObject && "__typename" in gqlObject && gqlObject.__typename === "User" && "id" in gqlObject && "displayName" in gqlObject && "photoURL" in gqlObject;
return !!gqlObject && "__typename" in gqlObject && gqlObject.__typename === "User" && "id" in gqlObject && "displayName" in gqlObject && "photoURL" in gqlObject && "address" in gqlObject;
},
isAuthUser: (gqlObject: void | object | GqlObject | null | undefined): gqlObject is UnionTypesQuery_currentUser_AuthUser => {
return !!gqlObject && "__typename" in gqlObject && gqlObject.__typename === "AuthUser" && "id" in gqlObject && "defaultDisplayName" in gqlObject && "defaultPhotoURL" in gqlObject;
Expand Down
8 changes: 8 additions & 0 deletions fixtures/parser/UnionTypesQuery.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
export interface UnionTypesQuery_currentUser_User_address {
__typename: "Address"
id: string;
streetNumber: number;
streetName: string;
}

export interface UnionTypesQuery_currentUser_User {
__typename: "User";
id: string;
displayName: string;
photoURL: string | null;
address: UnionTypesQuery_currentUser_User_address;
}

export interface UnionTypesQuery_currentUser_AuthUser {
Expand Down
18 changes: 18 additions & 0 deletions src/__tests__/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ const unionedTypenames = [
]

const unionTypes = [
{
name: 'UnionTypesQuery_currentUser_User_address',
predicates: [
{
reference: 'UnionTypesQuery_currentUser_User_address',
typename: 'Address',
properties: [
{ name: 'id', type: t.tsStringKeyword() },
{ name: 'streetNumber', type: t.tsNumberKeyword() },
{ name: 'streetName', type: t.tsStringKeyword() },
],
},
],
},
{
name: 'UnionTypesQuery_currentUser',
predicates: [
Expand All @@ -117,6 +131,10 @@ const unionTypes = [
{ name: 'id', type: t.tsStringKeyword() },
{ name: 'displayName', type: t.tsStringKeyword() },
{ name: 'photoURL', type: t.tsStringKeyword() },
{
name: 'address',
type: t.tsTypeReference(t.identifier('UnionTypesQuery_currentUser_User_address')),
},
],
},
{
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ describe('extractGraphQLTypes', () => {
})
it('Should return mutiple predicates for a union types', () => {
expect(extractGraphQLTypes(unionTypesFixture)).toMatchObject([
{
name: 'UnionTypesQuery_currentUser_User_address',
predicates: [
{
reference: 'UnionTypesQuery_currentUser_User_address',
typename: 'Address',
properties: [
{ name: 'id', type: t.tsStringKeyword() },
{ name: 'streetNumber', type: t.tsNumberKeyword() },
{ name: 'streetName', type: t.tsStringKeyword() },
],
},
],
},
{
name: 'UnionTypesQuery_currentUser',
predicates: [
Expand All @@ -137,6 +151,7 @@ describe('extractGraphQLTypes', () => {
{ name: 'id', type: t.tsStringKeyword() },
{ name: 'displayName', type: t.tsStringKeyword() },
{ name: 'photoURL', type: t.tsUnionType([t.tsStringKeyword(), t.tsNullKeyword()]) },
{ name: 'address', type: ref('UnionTypesQuery_currentUser_User_address') },
],
},
{
Expand Down
12 changes: 0 additions & 12 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,6 @@ const buildGuardObject = (predicates: GraphQLTypePredicateInfo[]): t.ObjectExpre
}

return t.objectExpression(props)

// return t.objectExpression([
// t.objectProperty(t.identifier(`is${idName}`), buildArrowFunction(graphqlTypeInfo)),
// ...(typeof typename === 'string'
// ? []
// : typename.map(typename => {
// return t.objectProperty(
// t.identifier(`is${typename}`),
// buildArrowFunction({ ...graphqlTypeInfo, typename })
// )
// })),
// ])
}

const buildNamedExport = (graphqlTypeInfo: GraphQLTypeInfo): t.ExportNamedDeclaration => {
Expand Down
10 changes: 4 additions & 6 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ export const extractGraphQLTypes = (code: string): GraphQLTypeInfo[] => {

const extractIdName = (reference: string) => {
const segments = reference.split('_')
const lastSegment = segments[segments.length - 1]
const isLastSegmentCapitalized = lastSegment.charAt(0).toUpperCase() === lastSegment.charAt(0)

const limitIndex = segments
.slice(1)
.findIndex(segment => segment.charAt(0).toUpperCase() === segment.charAt(0))

if (limitIndex === -1) {
if (segments.length === 1 || !isLastSegmentCapitalized) {
return reference
}

return segments.slice(0, limitIndex + 1).join('_')
return segments.slice(0, segments.length - 1).join('_')
}

const extractNamedExports = (statements: types.Statement[]): types.ExportNamedDeclaration[] => {
Expand Down

0 comments on commit 922d821

Please sign in to comment.