Skip to content

Commit

Permalink
Fixed issue #2 of the other project graphql-state
Browse files Browse the repository at this point in the history
  • Loading branch information
babyfish-ct committed Jul 11, 2022
1 parent e41e6b0 commit 78bb105
Show file tree
Hide file tree
Showing 25 changed files with 857 additions and 775 deletions.
59 changes: 34 additions & 25 deletions codegen/dist/CommonTypesWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommonTypesWriter = void 0;
const graphql_1 = require("graphql");
const Utils_1 = require("./Utils");
const Writer_1 = require("./Writer");
class CommonTypesWriter extends Writer_1.Writer {
constructor(schema, inheritanceInfo, stream, config) {
Expand All @@ -33,23 +34,27 @@ class CommonTypesWriter extends Writer_1.Writer {
t("export type ImplementationType<T> = ");
this.enter("BLANK", true);
for (const [type, castTypes] of this.inheritanceInfo.downcastTypeMap) {
t("T extends '");
t(type.name);
t("' ? ");
this.enter("BLANK");
if (!(type instanceof graphql_1.GraphQLUnionType)) {
t("'");
if (!(0, Utils_1.isExecludedTypeName)(this.config, type.name)) {
t("T extends '");
t(type.name);
t("'");
}
for (const castType of castTypes) {
this.separator(" | ");
t("ImplementationType<'");
t(castType.name);
t("'>");
t("' ? ");
this.enter("BLANK");
if (!(type instanceof graphql_1.GraphQLUnionType)) {
t("'");
t(type.name);
t("'");
}
for (const castType of castTypes) {
if (!(0, Utils_1.isExecludedTypeName)(this.config, castType.name)) {
this.separator(" | ");
t("ImplementationType<'");
t(castType.name);
t("'>");
}
}
this.leave();
t(" :\n");
}
this.leave();
t(" :\n");
}
t("T\n");
this.leave();
Expand All @@ -70,16 +75,20 @@ class CommonTypesWriter extends Writer_1.Writer {
t("switch (typeName)");
this.scope({ type: "BLOCK", multiLines: true, suffix: "\n" }, () => {
for (const [type, castTypes] of castTypeMap) {
t(`case '${type.name}':`);
this.scope({ type: "BLANK", multiLines: true }, () => {
if (!(type instanceof graphql_1.GraphQLUnionType)) {
t(`output.push('${type.name}');\n`);
}
for (const castType of castTypes) {
t(`${prefix}castTypes0('${castType.name}', output);\n`);
}
t("break;\n");
});
if (!(0, Utils_1.isExecludedTypeName)(this.config, type.name)) {
t(`case '${type.name}':`);
this.scope({ type: "BLANK", multiLines: true }, () => {
if (!(type instanceof graphql_1.GraphQLUnionType)) {
t(`output.push('${type.name}');\n`);
}
for (const castType of castTypes) {
if (!(0, Utils_1.isExecludedTypeName)(this.config, castType.name)) {
t(`${prefix}castTypes0('${castType.name}', output);\n`);
}
}
t("break;\n");
});
}
}
t("default:");
this.scope({ type: "BLANK", multiLines: true }, () => {
Expand Down
20 changes: 16 additions & 4 deletions codegen/dist/FetcherWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Utils_1 = require("./Utils");
const Writer_1 = require("./Writer");
class FetcherWriter extends Writer_1.Writer {
constructor(modelType, ctx, stream, config) {
var _a, _b;
var _a, _b, _c;
super(stream, config);
this.modelType = modelType;
this.ctx = ctx;
Expand All @@ -39,9 +39,21 @@ class FetcherWriter extends Writer_1.Writer {
}
this.fieldMap = map;
}
else {
else if (config.excludedTypes === undefined) {
this.fieldMap = modelType.getFields();
}
else {
const fieldMap = modelType.getFields();
const filteredFieldMap = {};
for (const fieldName in fieldMap) {
const field = fieldMap[fieldName];
const targetTypeName = (_c = (0, Utils_1.targetTypeOf)(field.type)) === null || _c === void 0 ? void 0 : _c.name;
if (!(0, Utils_1.isExecludedTypeName)(config, targetTypeName)) {
filteredFieldMap[fieldName] = field;
}
}
this.fieldMap = filteredFieldMap;
}
const fieldArgsMap = new Map();
const fieldCategoryMap = new Map();
const defaultFetcherProps = [];
Expand Down Expand Up @@ -167,8 +179,8 @@ class FetcherWriter extends Writer_1.Writer {
this.writeDirective();
this.writeTypeName();
for (const fieldName in this.fieldMap) {
this.text("\n");
const field = this.fieldMap[fieldName];
this.text("\n");
this.writePositiveProp(field);
this.writeNegativeProp(field);
}
Expand Down Expand Up @@ -535,7 +547,7 @@ class FetcherWriter extends Writer_1.Writer {
getDeclaredFieldNames() {
const fields = new Set();
if (this.modelType instanceof graphql_1.GraphQLObjectType || this.modelType instanceof graphql_1.GraphQLInterfaceType) {
const fieldMap = this.modelType.getFields();
const fieldMap = this.fieldMap;
for (const fieldName in fieldMap) {
fields.add(fieldMap[fieldName].name);
}
Expand Down
19 changes: 11 additions & 8 deletions codegen/dist/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const InputWriter_1 = require("./InputWriter");
const CommonTypesWriter_1 = require("./CommonTypesWriter");
const InheritanceInfo_1 = require("./InheritanceInfo");
const EnumInputMetadataWriter_1 = require("./EnumInputMetadataWriter");
const Utils_1 = require("./Utils");
class Generator {
constructor(config) {
this.config = config;
Expand Down Expand Up @@ -62,14 +63,16 @@ class Generator {
edgeTypes.add(tuple[1]);
}
}
if (type instanceof graphql_1.GraphQLObjectType || type instanceof graphql_1.GraphQLInterfaceType || type instanceof graphql_1.GraphQLUnionType) {
fetcherTypes.push(type);
}
else if (type instanceof graphql_1.GraphQLInputObjectType) {
inputTypes.push(type);
}
else if (type instanceof graphql_1.GraphQLEnumType) {
enumTypes.push(type);
if (!(0, Utils_1.isExecludedTypeName)(this.config, type.name)) {
if (type instanceof graphql_1.GraphQLObjectType || type instanceof graphql_1.GraphQLInterfaceType || type instanceof graphql_1.GraphQLUnionType) {
fetcherTypes.push(type);
}
else if (type instanceof graphql_1.GraphQLInputObjectType) {
inputTypes.push(type);
}
else if (type instanceof graphql_1.GraphQLEnumType) {
enumTypes.push(type);
}
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions codegen/dist/GeneratorConfig.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ export interface GeneratorConfig {
readonly objectEditable?: boolean;
readonly arrayEditable?: boolean;
readonly fetcherSuffix?: string;
readonly scalarTypeMap: {
[key: string]: string | {
readonly excludedTypes?: ReadonlyArray<string>;
readonly scalarTypeMap?: {
readonly [key: string]: string | {
readonly typeName: string;
readonly importSource: string;
};
};
readonly idFieldMap?: {
[key: string]: string;
readonly [key: string]: string;
};
readonly defaultFetcherExcludeMap?: {
[key: string]: string[];
readonly [key: string]: string[];
};
}
export declare function validateConfig(config: any): void;
Expand Down
2 changes: 2 additions & 0 deletions codegen/dist/Utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
* 2. Automatically infers the type of the returned data according to the strongly typed query
*/
import { GraphQLInterfaceType, GraphQLObjectType, GraphQLType, GraphQLUnionType } from "graphql";
import { GeneratorConfig } from "./GeneratorConfig";
export declare function targetTypeOf(type: GraphQLType): GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType | undefined;
export declare function instancePrefix(name: string): string;
export declare function isExecludedTypeName(config: GeneratorConfig, typeName: string | undefined): boolean;
10 changes: 9 additions & 1 deletion codegen/dist/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* 2. Automatically infers the type of the returned data according to the strongly typed query
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.instancePrefix = exports.targetTypeOf = void 0;
exports.isExecludedTypeName = exports.instancePrefix = exports.targetTypeOf = void 0;
const graphql_1 = require("graphql");
function targetTypeOf(type) {
if (type instanceof graphql_1.GraphQLNonNull) {
Expand All @@ -28,3 +28,11 @@ function instancePrefix(name) {
return name.substring(0, 1).toLowerCase() + name.substring(1);
}
exports.instancePrefix = instancePrefix;
function isExecludedTypeName(config, typeName) {
if (typeName == undefined) {
return false;
}
const list = config.excludedTypes;
return list !== undefined && list.findIndex(v => v == typeName) !== -1;
}
exports.isExecludedTypeName = isExecludedTypeName;
15 changes: 10 additions & 5 deletions codegen/dist/state/TriggerEventWriter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TriggerEventWiter = void 0;
const Utils_1 = require("../Utils");
const Writer_1 = require("../Writer");
class TriggerEventWiter extends Writer_1.Writer {
constructor(modelType, idField, stream, config) {
var _a;
super(stream, config);
this.modelType = modelType;
this.idField = idField;
Expand All @@ -12,11 +14,14 @@ class TriggerEventWiter extends Writer_1.Writer {
const fieldMap = modelType.getFields();
for (const fieldName in fieldMap) {
if (fieldName !== (idField === null || idField === void 0 ? void 0 : idField.name)) {
if (fieldMap[fieldName].args.length === 0) {
simpleFieldNames.add(fieldName);
}
else {
parameterizedFieldNames.add(fieldName);
const targetTypeName = (_a = (0, Utils_1.targetTypeOf)(fieldMap[fieldName].type)) === null || _a === void 0 ? void 0 : _a.name;
if (!(0, Utils_1.isExecludedTypeName)(config, targetTypeName)) {
if (fieldMap[fieldName].args.length === 0) {
simpleFieldNames.add(fieldName);
}
else {
parameterizedFieldNames.add(fieldName);
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion codegen/dist/state/TypedConfigurationWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ class TypedConfigurationWriter extends Writer_1.Writer {
for (const fieldName in fieldMap) {
const field = fieldMap[fieldName];
const targetType = (0, Utils_1.targetTypeOf)(field.type);
if (targetType !== undefined && !this.ctx.embeddedTypes.has(targetType)) {
if (targetType !== undefined &&
!(0, Utils_1.isExecludedTypeName)(this.config, targetType.name) &&
!this.ctx.embeddedTypes.has(targetType)) {
const connection = this.ctx.connections.get(targetType);
if (connection !== undefined) {
map.set(fieldName, connection.nodeType.name);
Expand Down
61 changes: 35 additions & 26 deletions codegen/src/CommonTypesWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { WriteStream } from "fs";
import { GraphQLSchema, GraphQLUnionType } from "graphql";
import { GeneratorConfig } from "./GeneratorConfig";
import { InheritanceInfo } from "./InheritanceInfo";
import { isExecludedTypeName } from "./Utils";
import { Writer } from "./Writer";

export class CommonTypesWriter extends Writer {
Expand Down Expand Up @@ -45,23 +46,27 @@ export class CommonTypesWriter extends Writer {
t("export type ImplementationType<T> = ");
this.enter("BLANK", true);
for (const [type, castTypes] of this.inheritanceInfo.downcastTypeMap) {
t("T extends '");
t(type.name)
t("' ? ");
this.enter("BLANK")
if (!(type instanceof GraphQLUnionType)) {
t("'");
t(type.name);
t("'");
}
for (const castType of castTypes) {
this.separator(" | ");
t("ImplementationType<'");
t(castType.name);
t("'>");
if (!isExecludedTypeName(this.config, type.name)) {
t("T extends '");
t(type.name)
t("' ? ");
this.enter("BLANK")
if (!(type instanceof GraphQLUnionType)) {
t("'");
t(type.name);
t("'");
}
for (const castType of castTypes) {
if (!isExecludedTypeName(this.config, castType.name)) {
this.separator(" | ");
t("ImplementationType<'");
t(castType.name);
t("'>");
}
}
this.leave();
t(" :\n");
}
this.leave();
t(" :\n");
}
t("T\n");
this.leave();
Expand All @@ -87,16 +92,20 @@ export class CommonTypesWriter extends Writer {
t("switch (typeName)");
this.scope({type: "BLOCK", multiLines: true, suffix: "\n"}, () => {
for (const [type, castTypes] of castTypeMap) {
t(`case '${type.name}':`);
this.scope({type: "BLANK", multiLines: true}, () => {
if (!(type instanceof GraphQLUnionType)) {
t(`output.push('${type.name}');\n`);
}
for (const castType of castTypes) {
t(`${prefix}castTypes0('${castType.name}', output);\n`);
}
t("break;\n");
});
if (!isExecludedTypeName(this.config, type.name)) {
t(`case '${type.name}':`);
this.scope({type: "BLANK", multiLines: true}, () => {
if (!(type instanceof GraphQLUnionType)) {
t(`output.push('${type.name}');\n`);
}
for (const castType of castTypes) {
if (!isExecludedTypeName(this.config, castType.name)) {
t(`${prefix}castTypes0('${castType.name}', output);\n`);
}
}
t("break;\n");
});
}
}
t("default:");
this.scope({type: "BLANK", multiLines: true}, () => {
Expand Down
19 changes: 15 additions & 4 deletions codegen/src/FetcherWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import { WriteStream } from "fs";
import { GraphQLArgument, GraphQLField, GraphQLFieldMap, GraphQLInterfaceType, GraphQLList, GraphQLNamedType, GraphQLNonNull, GraphQLObjectType, GraphQLType, GraphQLUnionType } from "graphql";
import { targetTypeOf, instancePrefix } from "./Utils";
import { targetTypeOf, instancePrefix, isExecludedTypeName } from "./Utils";
import { GeneratorConfig } from "./GeneratorConfig";
import { ImportingBehavior, Writer } from "./Writer";
import { FetcherContext } from "./FetcherContext";
Expand Down Expand Up @@ -63,8 +63,19 @@ export class FetcherWriter extends Writer {
}
}
this.fieldMap = map;
} else {
} else if (config.excludedTypes === undefined) {
this.fieldMap = modelType.getFields();
} else {
const fieldMap = modelType.getFields();
const filteredFieldMap: GraphQLFieldMap<any, any> = {};
for (const fieldName in fieldMap) {
const field = fieldMap[fieldName];
const targetTypeName = targetTypeOf(field.type)?.name;
if (!isExecludedTypeName(config, targetTypeName)) {
filteredFieldMap[fieldName] = field;
}
}
this.fieldMap = filteredFieldMap;
}

const fieldArgsMap = new Map<string, GraphQLArgument[]>();
Expand Down Expand Up @@ -205,8 +216,8 @@ export class FetcherWriter extends Writer {
this.writeTypeName();

for (const fieldName in this.fieldMap) {
this.text("\n");
const field = this.fieldMap[fieldName]!;
this.text("\n");
this.writePositiveProp(field);
this.writeNegativeProp(field);
}
Expand Down Expand Up @@ -597,7 +608,7 @@ export class FetcherWriter extends Writer {
private getDeclaredFieldNames(): ReadonlySet<string> {
const fields = new Set<string>();
if (this.modelType instanceof GraphQLObjectType || this.modelType instanceof GraphQLInterfaceType) {
const fieldMap = this.modelType.getFields();
const fieldMap = this.fieldMap;
for (const fieldName in fieldMap) {
fields.add(fieldMap[fieldName]!.name);
}
Expand Down
Loading

0 comments on commit 78bb105

Please sign in to comment.