Skip to content

Commit

Permalink
Fixed #15 and #16
Browse files Browse the repository at this point in the history
  • Loading branch information
babyfish-ct committed Jun 22, 2022
1 parent 89417c4 commit 7d981cd
Show file tree
Hide file tree
Showing 29 changed files with 180 additions and 164 deletions.
17 changes: 11 additions & 6 deletions codegen/dist/EnumInputMetadataWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ class EnumInputMetadataWriter extends Writer_1.Writer {
this.importStatement("import { EnumInputMetadataBuilder } from 'graphql-ts-client-api';");
}
writeCode() {
const processedTypeNames = new Set();
const enumInputMetaTypeMap = new Map();
const typeMap = this.schema.getTypeMap();
for (const typeName in typeMap) {
const type = typeMap[typeName];
if (type instanceof graphql_1.GraphQLEnumType) {
this.collectEnumMetaTypes(type, enumInputMetaTypeMap);
this.collectEnumMetaTypes(type, processedTypeNames, enumInputMetaTypeMap);
}
else if (type instanceof graphql_1.GraphQLInputObjectType) {
this.collectEnumMetaTypes(type, enumInputMetaTypeMap);
this.collectEnumMetaTypes(type, processedTypeNames, enumInputMetaTypeMap);
}
}
this.text("export const ENUM_INPUT_METADATA = ");
Expand Down Expand Up @@ -51,31 +52,35 @@ class EnumInputMetadataWriter extends Writer_1.Writer {
});
this.text(";");
}
collectEnumMetaTypes(type, outMap) {
collectEnumMetaTypes(type, processedTypeNames, outMap) {
if (type instanceof graphql_1.GraphQLScalarType) {
return false;
}
if (type instanceof graphql_1.GraphQLList) {
return this.collectEnumMetaTypes(type.ofType, outMap);
return this.collectEnumMetaTypes(type.ofType, processedTypeNames, outMap);
}
if (type instanceof graphql_1.GraphQLNonNull) {
return this.collectEnumMetaTypes(type.ofType, outMap);
return this.collectEnumMetaTypes(type.ofType, processedTypeNames, outMap);
}
if (type.name.startsWith("__")) {
return false;
}
if (outMap.has(type.name)) {
return true;
}
if (processedTypeNames.has(type.name)) {
return false;
}
if (type instanceof graphql_1.GraphQLEnumType) {
outMap.set(type.name, undefined);
return true;
}
processedTypeNames.add(type.name);
const fieldMap = type.getFields();
const fields = [];
for (const fieldName in fieldMap) {
const field = fieldMap[fieldName];
if (this.collectEnumMetaTypes(field.type, outMap)) {
if (this.collectEnumMetaTypes(field.type, processedTypeNames, outMap)) {
fields.push(field);
}
}
Expand Down
20 changes: 7 additions & 13 deletions codegen/dist/Writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,15 @@ class Writer {
this.typeRef(type.ofType, objectRender);
}
else if (type instanceof graphql_1.GraphQLList) {
if (type.ofType instanceof graphql_1.GraphQLNonNull) {
if (!this.config.arrayEditable) {
this.text("readonly ");
}
this.typeRef(type.ofType, objectRender);
this.text("[]");
if (!this.config.arrayEditable) {
this.text("Readonly");
}
else {
if (!this.config.arrayEditable) {
this.text("Readonly");
}
this.text("Array<");
this.typeRef(type.ofType, objectRender);
this.text(" | undefined>");
this.text("Array<");
this.typeRef(type.ofType, objectRender);
if (!(type.ofType instanceof graphql_1.GraphQLNonNull)) {
this.text(" | undefined");
}
this.text(">");
}
}
gqlTypeRef(type) {
Expand Down
20 changes: 14 additions & 6 deletions codegen/src/EnumInputMetadataWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ export class EnumInputMetadataWriter extends Writer {
}

protected writeCode() {
const processedTypeNames = new Set<string>();
const enumInputMetaTypeMap = new Map<string, ReadonlyArray<GraphQLInputField> | undefined>();
const typeMap = this.schema.getTypeMap();
for (const typeName in typeMap) {
const type = typeMap[typeName];
if (type instanceof GraphQLEnumType) {
this.collectEnumMetaTypes(type, enumInputMetaTypeMap);
this.collectEnumMetaTypes(type, processedTypeNames, enumInputMetaTypeMap);
} else if (type instanceof GraphQLInputObjectType) {
this.collectEnumMetaTypes(type, enumInputMetaTypeMap);
this.collectEnumMetaTypes(type, processedTypeNames, enumInputMetaTypeMap);
}
}
this.text("export const ENUM_INPUT_METADATA = ");
Expand Down Expand Up @@ -59,17 +60,18 @@ export class EnumInputMetadataWriter extends Writer {

private collectEnumMetaTypes(
type: GraphQLInputType,
processedTypeNames: Set<string>,
outMap: Map<string, ReadonlyArray<GraphQLInputField> | undefined>
): boolean {

if (type instanceof GraphQLScalarType) {
return false;
}
if (type instanceof GraphQLList) {
return this.collectEnumMetaTypes(type.ofType, outMap);
return this.collectEnumMetaTypes(type.ofType, processedTypeNames, outMap);
}
if (type instanceof GraphQLNonNull) {
return this.collectEnumMetaTypes(type.ofType, outMap);
return this.collectEnumMetaTypes(type.ofType, processedTypeNames, outMap);
}

if (type.name.startsWith("__")) {
Expand All @@ -80,15 +82,21 @@ export class EnumInputMetadataWriter extends Writer {
return true;
}

if (type instanceof GraphQLEnumType) {
if (processedTypeNames.has(type.name)) {
return false;
}

if (type instanceof GraphQLEnumType) {
outMap.set(type.name, undefined);
return true;
}

processedTypeNames.add(type.name);
const fieldMap = type.getFields();
const fields: GraphQLInputField[] = [];
for (const fieldName in fieldMap) {
const field = fieldMap[fieldName];
if (this.collectEnumMetaTypes(field.type, outMap)) {
if (this.collectEnumMetaTypes(field.type, processedTypeNames, outMap)) {
fields.push(field);
}
}
Expand Down
21 changes: 8 additions & 13 deletions codegen/src/Writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,20 +288,15 @@ export abstract class Writer {
} else if (type instanceof GraphQLNonNull) {
this.typeRef(type.ofType, objectRender);
} else if (type instanceof GraphQLList) {
if (type.ofType instanceof GraphQLNonNull) {
if (!this.config.arrayEditable) {
this.text("readonly ");
}
this.typeRef(type.ofType, objectRender);
this.text("[]");
} else {
if (!this.config.arrayEditable) {
this.text("Readonly");
}
this.text("Array<");
this.typeRef(type.ofType, objectRender);
this.text(" | undefined>");
if (!this.config.arrayEditable) {
this.text("Readonly");
}
this.text("Array<");
this.typeRef(type.ofType, objectRender);
if (!(type.ofType instanceof GraphQLNonNull)) {
this.text(" | undefined");
}
this.text(">");
}
}

Expand Down
4 changes: 2 additions & 2 deletions example/client/apollo-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"graphql": "^15.5.1",
"graphql-ts-client-api": "^3.1.2",
"graphql-ts-client-api": "^3.1.5",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
Expand Down Expand Up @@ -50,6 +50,6 @@
]
},
"devDependencies": {
"graphql-ts-client-codegen": "^3.1.2"
"graphql-ts-client-codegen": "^3.1.5"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface DepartmentConnectionFetcher<T extends object, TVariables extend
>(
child: EdgeFetcher<'DepartmentEdge', X, XVariables>
): DepartmentConnectionFetcher<
T & {readonly "edges": readonly X[]},
T & {readonly "edges": ReadonlyArray<X>},
TVariables & XVariables
>;

Expand All @@ -79,8 +79,8 @@ export interface DepartmentConnectionFetcher<T extends object, TVariables extend
): DepartmentConnectionFetcher<
T & (
XDirectives extends { readonly include: any } | { readonly skip: any } ?
{readonly [key in XAlias]?: readonly X[]} :
{readonly [key in XAlias]: readonly X[]}
{readonly [key in XAlias]?: ReadonlyArray<X>} :
{readonly [key in XAlias]: ReadonlyArray<X>}
),
TVariables & XVariables & XDirectiveVariables
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export interface DepartmentFetcher<T extends object, TVariables extends object>
>(
child: ObjectFetcher<'Employee', X, XVariables>
): DepartmentFetcher<
T & {readonly "employees": readonly X[]},
T & {readonly "employees": ReadonlyArray<X>},
TVariables & XVariables
>;

Expand All @@ -102,8 +102,8 @@ export interface DepartmentFetcher<T extends object, TVariables extends object>
): DepartmentFetcher<
T & (
XDirectives extends { readonly include: any } | { readonly skip: any } ?
{readonly [key in XAlias]?: readonly X[]} :
{readonly [key in XAlias]: readonly X[]}
{readonly [key in XAlias]?: ReadonlyArray<X>} :
{readonly [key in XAlias]: ReadonlyArray<X>}
),
TVariables & XVariables & XDirectiveVariables
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface EmployeeConnectionFetcher<T extends object, TVariables extends
>(
child: EdgeFetcher<'EmployeeEdge', X, XVariables>
): EmployeeConnectionFetcher<
T & {readonly "edges": readonly X[]},
T & {readonly "edges": ReadonlyArray<X>},
TVariables & XVariables
>;

Expand All @@ -79,8 +79,8 @@ export interface EmployeeConnectionFetcher<T extends object, TVariables extends
): EmployeeConnectionFetcher<
T & (
XDirectives extends { readonly include: any } | { readonly skip: any } ?
{readonly [key in XAlias]?: readonly X[]} :
{readonly [key in XAlias]: readonly X[]}
{readonly [key in XAlias]?: ReadonlyArray<X>} :
{readonly [key in XAlias]: ReadonlyArray<X>}
),
TVariables & XVariables & XDirectiveVariables
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export interface EmployeeFetcher<T extends object, TVariables extends object> ex
>(
child: ObjectFetcher<'Employee', X, XVariables>
): EmployeeFetcher<
T & {readonly "subordinates": readonly X[]},
T & {readonly "subordinates": ReadonlyArray<X>},
TVariables & XVariables
>;

Expand All @@ -226,8 +226,8 @@ export interface EmployeeFetcher<T extends object, TVariables extends object> ex
): EmployeeFetcher<
T & (
XDirectives extends { readonly include: any } | { readonly skip: any } ?
{readonly [key in XAlias]?: readonly X[]} :
{readonly [key in XAlias]: readonly X[]}
{readonly [key in XAlias]?: ReadonlyArray<X>} :
{readonly [key in XAlias]: ReadonlyArray<X>}
),
TVariables & XVariables & XDirectiveVariables
>;
Expand Down
16 changes: 8 additions & 8 deletions example/client/apollo-demo/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5527,17 +5527,17 @@ graphql-tag@^2.12.3:
dependencies:
tslib "^2.1.0"

graphql-ts-client-api@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/graphql-ts-client-api/-/graphql-ts-client-api-3.1.2.tgz#c6ee66541af8473b28b860d0b33aacee6b3a1343"
integrity sha512-RurZAUMtZGWqhp3u/UMR0mBnUiNrO34LZ4pxcjT9p+IuG6lSGmmHxI36/0YLBX7Y0ZbP/m+Ym05iYvDW0yvUVg==
graphql-ts-client-api@^3.1.5:
version "3.1.5"
resolved "https://registry.yarnpkg.com/graphql-ts-client-api/-/graphql-ts-client-api-3.1.5.tgz#aaf7dd41ee06a903079b06ed1c551ec138dd9521"
integrity sha512-irLWGkkyusiWHkI7B3LoAYa1nLuII7UjuQ0BXy78T4DrReU6UK5lI5ZKMobmFVQYIkjyNcXbCvOceerB5OuepQ==
dependencies:
ts-md5 "^1.2.9"

graphql-ts-client-codegen@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/graphql-ts-client-codegen/-/graphql-ts-client-codegen-3.1.2.tgz#4076d4d26057bd16d9be4e5aeb1c9c1e5e002bd7"
integrity sha512-pYanD5MrNVio8O6qQnbVSftfaSDL1ol9LT9DNl2olg5htdI4zMG/iSQ1ZIaLi1GQGzUXHpzQm2Q3gK+jZMzTRQ==
graphql-ts-client-codegen@^3.1.5:
version "3.1.5"
resolved "https://registry.yarnpkg.com/graphql-ts-client-codegen/-/graphql-ts-client-codegen-3.1.5.tgz#91920e85c274a06ebafd62e61581849a4368e7f6"
integrity sha512-GFu300xP5T2AlJBIkAIwVNNGidY51HpIAZeyE2TSufV52eO48AF3X8YAzRRyO4ukRighq6Ovy++JAGrCAXiXZQ==
dependencies:
"@types/node" "^15.12.2"
"@types/node-fetch" "^2.5.10"
Expand Down
4 changes: 2 additions & 2 deletions example/client/async-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@types/node": "^12.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"graphql-ts-client-api": "^3.1.2",
"graphql-ts-client-api": "^3.1.5",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
Expand Down Expand Up @@ -45,7 +45,7 @@
},
"devDependencies": {
"graphql": "^15.5.0",
"graphql-ts-client-codegen": "^3.1.2",
"graphql-ts-client-codegen": "^3.1.5",
"node-fetch": "^2.6.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface DepartmentConnectionFetcher<T extends object, TVariables extend
>(
child: EdgeFetcher<'DepartmentEdge', X, XVariables>
): DepartmentConnectionFetcher<
T & {readonly "edges": readonly X[]},
T & {readonly "edges": ReadonlyArray<X>},
TVariables & XVariables
>;

Expand All @@ -79,8 +79,8 @@ export interface DepartmentConnectionFetcher<T extends object, TVariables extend
): DepartmentConnectionFetcher<
T & (
XDirectives extends { readonly include: any } | { readonly skip: any } ?
{readonly [key in XAlias]?: readonly X[]} :
{readonly [key in XAlias]: readonly X[]}
{readonly [key in XAlias]?: ReadonlyArray<X>} :
{readonly [key in XAlias]: ReadonlyArray<X>}
),
TVariables & XVariables & XDirectiveVariables
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export interface DepartmentFetcher<T extends object, TVariables extends object>
>(
child: ObjectFetcher<'Employee', X, XVariables>
): DepartmentFetcher<
T & {readonly "employees": readonly X[]},
T & {readonly "employees": ReadonlyArray<X>},
TVariables & XVariables
>;

Expand All @@ -102,8 +102,8 @@ export interface DepartmentFetcher<T extends object, TVariables extends object>
): DepartmentFetcher<
T & (
XDirectives extends { readonly include: any } | { readonly skip: any } ?
{readonly [key in XAlias]?: readonly X[]} :
{readonly [key in XAlias]: readonly X[]}
{readonly [key in XAlias]?: ReadonlyArray<X>} :
{readonly [key in XAlias]: ReadonlyArray<X>}
),
TVariables & XVariables & XDirectiveVariables
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface EmployeeConnectionFetcher<T extends object, TVariables extends
>(
child: EdgeFetcher<'EmployeeEdge', X, XVariables>
): EmployeeConnectionFetcher<
T & {readonly "edges": readonly X[]},
T & {readonly "edges": ReadonlyArray<X>},
TVariables & XVariables
>;

Expand All @@ -79,8 +79,8 @@ export interface EmployeeConnectionFetcher<T extends object, TVariables extends
): EmployeeConnectionFetcher<
T & (
XDirectives extends { readonly include: any } | { readonly skip: any } ?
{readonly [key in XAlias]?: readonly X[]} :
{readonly [key in XAlias]: readonly X[]}
{readonly [key in XAlias]?: ReadonlyArray<X>} :
{readonly [key in XAlias]: ReadonlyArray<X>}
),
TVariables & XVariables & XDirectiveVariables
>;
Expand Down
Loading

0 comments on commit 7d981cd

Please sign in to comment.