Skip to content

Commit

Permalink
Fix issue #19
Browse files Browse the repository at this point in the history
  • Loading branch information
babyfish-ct committed Jun 23, 2022
1 parent 9dde6db commit 9060bc7
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 100 deletions.
19 changes: 11 additions & 8 deletions api/dist/DependencyManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DependencyManager {
this.rootTypeResourceMap = new Map();
/*
* level-1 key: FieldName
* level-2 key: Field name
* level-2 key: FieldName
*/
this.fieldResourceMap = new Map();
window.dependencyManager = this;
Expand Down Expand Up @@ -49,8 +49,8 @@ class DependencyManager {
}
registerTypes(resource, fetchers) {
for (const fetcher of fetchers) {
const isOperation = isOperationFetcher(fetcher);
for (const [fieldName, field] of fetcher.fieldMap) {
for (const [, field] of fetcher.fieldMap) {
const fieldName = field.name;
if (!fieldName.startsWith("...")) {
const declaringTypeNames = getDeclaringTypeNames(fieldName, fetcher);
for (const declaringTypeName of declaringTypeNames) {
Expand All @@ -66,7 +66,8 @@ class DependencyManager {
registerFields(resource, fetchers) {
for (const fetcher of fetchers) {
const isOperation = isOperationFetcher(fetcher);
for (const [fieldName, field] of fetcher.fieldMap) {
for (const [, field] of fetcher.fieldMap) {
const fieldName = field.name;
if (!isOperation && !fieldName.startsWith("...")) {
const subMap = compute(this.fieldResourceMap, fieldName, () => new Map());
const declaringTypeNames = getDeclaringTypeNames(fieldName, fetcher);
Expand Down Expand Up @@ -95,7 +96,8 @@ class DependencyManager {
(_b = this.rootTypeResourceMap.get(fetcher.fetchableType.name)) === null || _b === void 0 ? void 0 : _b.copyTo(output);
}
}
for (const [fieldName, field] of fetcher.fieldMap) {
for (const [fieldKey, field] of fetcher.fieldMap) {
const fieldName = field.name;
if (fieldName.startsWith("...")) { // Fragment, not assocaition
if (field.childFetchers !== undefined) {
for (const childFetcher of field.childFetchers) {
Expand All @@ -104,8 +106,8 @@ class DependencyManager {
}
}
else {
const oldValue = nullToUndefined(oldObject !== undefined ? oldObject[fieldName] : undefined);
const newValue = nullToUndefined(newObject !== undefined ? newObject[fieldName] : undefined);
const oldValue = nullToUndefined(oldObject !== undefined ? oldObject[fieldKey] : undefined);
const newValue = nullToUndefined(newObject !== undefined ? newObject[fieldKey] : undefined);
if (field.childFetchers !== undefined && field.childFetchers.length !== 0) { // association
if (oldValue !== newValue) { // Not both undefined
for (const childFetcher of field.childFetchers) {
Expand Down Expand Up @@ -168,7 +170,8 @@ class DependencyManager {
collectAllResources(fetcher, output) {
var _a, _b, _c, _d;
(_a = this.rootTypeResourceMap.get(fetcher.fetchableType.name)) === null || _a === void 0 ? void 0 : _a.copyTo(output);
for (const [fieldName, field] of fetcher.fieldMap) {
for (const [, field] of fetcher.fieldMap) {
const fieldName = field.name;
if (!fieldName.startsWith("...")) { // Not fragment
const declaringTypes = getDeclaringTypeNames(fieldName, fetcher);
for (const declaringType of declaringTypes) {
Expand Down
3 changes: 2 additions & 1 deletion api/dist/Fetcher.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export declare abstract class AbstractFetcher<E extends string, T extends object
get directiveMap(): ReadonlyMap<string, DirectiveArgs>;
private _getDirectiveMap;
get variableTypeMap(): ReadonlyMap<string, string>;
findField(fieldName: string): FetcherField | undefined;
findField(fieldKey: string): FetcherField | undefined;
toString(): string;
toFragmentString(): string;
toJSON(): string;
Expand All @@ -74,6 +74,7 @@ export declare abstract class AbstractFetcher<E extends string, T extends object
" $supressWarnings"(_: T, _2: TVariables): never;
}
export interface FetcherField {
readonly name: string;
readonly argGraphQLTypes?: ReadonlyMap<string, string>;
readonly args?: object;
readonly fieldOptionsValue?: FieldOptionsValue;
Expand Down
29 changes: 16 additions & 13 deletions api/dist/Fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AbstractFetcher {
return m;
}
_getFieldMap0() {
var _a, _b, _c, _d;
var _a, _b, _c, _d, _e, _f;
const fetchers = [];
for (let fetcher = this; fetcher !== undefined; fetcher = fetcher._prev) {
if (fetcher._field !== "") {
Expand All @@ -84,24 +84,26 @@ class AbstractFetcher {
const fieldMap = new Map();
for (let i = fetchers.length - 1; i >= 0; --i) {
const fetcher = fetchers[i];
const fetchKey = (_b = (_a = fetcher === null || fetcher === void 0 ? void 0 : fetcher._fieldOptionsValue) === null || _a === void 0 ? void 0 : _a.alias) !== null && _b !== void 0 ? _b : fetcher._field;
if (fetcher._field.startsWith('...')) {
let childFetchers = (_a = fieldMap.get(fetcher._field)) === null || _a === void 0 ? void 0 : _a.childFetchers;
let childFetchers = (_c = fieldMap.get(fetchKey)) === null || _c === void 0 ? void 0 : _c.childFetchers;
if (childFetchers === undefined) {
childFetchers = [];
fieldMap.set(fetcher._field, { plural: false, childFetchers }); // Fragment cause mutliple child fetchers
fieldMap.set(fetchKey, { name: fetcher._field, plural: false, childFetchers }); // Fragment cause mutliple child fetchers
}
childFetchers.push(fetcher._child);
}
else {
if (fetcher._negative) {
fieldMap.delete(fetcher._field);
fieldMap.delete(fetchKey);
}
else {
fieldMap.set(fetcher._field, {
argGraphQLTypes: (_b = fetcher.fetchableType.fields.get(fetcher._field)) === null || _b === void 0 ? void 0 : _b.argGraphQLTypeMap,
fieldMap.set(fetchKey, {
name: fetcher._field,
argGraphQLTypes: (_d = fetcher.fetchableType.fields.get(fetcher._field)) === null || _d === void 0 ? void 0 : _d.argGraphQLTypeMap,
args: fetcher._args,
fieldOptionsValue: fetcher._fieldOptionsValue,
plural: (_d = (_c = fetcher.fetchableType.fields.get(fetcher._field)) === null || _c === void 0 ? void 0 : _c.isPlural) !== null && _d !== void 0 ? _d : false,
plural: (_f = (_e = fetcher.fetchableType.fields.get(fetcher._field)) === null || _e === void 0 ? void 0 : _e.isPlural) !== null && _f !== void 0 ? _f : false,
childFetchers: fetcher._child === undefined ? undefined : [fetcher._child] // Association only cause one child fetcher
});
}
Expand Down Expand Up @@ -130,15 +132,15 @@ class AbstractFetcher {
get variableTypeMap() {
return this.result.variableTypeMap;
}
findField(fieldName) {
const field = this.fieldMap.get(fieldName);
findField(fieldKey) {
const field = this.fieldMap.get(fieldKey);
if (field !== undefined) {
return field;
}
for (const [fieldName, field] of this.fieldMap) {
if (fieldName.startsWith("...") && field.childFetchers !== undefined) {
for (const [fieldKey, field] of this.fieldMap) {
if (field.name.startsWith("...") && field.childFetchers !== undefined) {
for (const fragmentFetcher of field.childFetchers) {
const deeperField = fragmentFetcher.findField(fieldName);
const deeperField = fragmentFetcher.findField(fieldKey);
if (deeperField !== undefined) {
return deeperField;
}
Expand Down Expand Up @@ -224,7 +226,8 @@ class ResultContext {
accept(fetcher) {
var _a, _b;
const t = this.writer.text.bind(this.writer);
for (const [fieldName, field] of fetcher.fieldMap) {
for (const [, field] of fetcher.fieldMap) {
const fieldName = field.name;
if (fieldName !== "...") { // Inline fragment
const alias = (_a = field.fieldOptionsValue) === null || _a === void 0 ? void 0 : _a.alias;
if (alias !== undefined && alias !== "" && alias !== fieldName) {
Expand Down
19 changes: 11 additions & 8 deletions api/src/DependencyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class DependencyManager {

/*
* level-1 key: FieldName
* level-2 key: Field name
* level-2 key: FieldName
*/
private fieldResourceMap = new Map<string, Map<string, Resources>>();

Expand Down Expand Up @@ -68,8 +68,8 @@ export class DependencyManager {

private registerTypes(resource: string, fetchers: readonly Fetcher<string, object, object>[]) {
for (const fetcher of fetchers) {
const isOperation = isOperationFetcher(fetcher);
for (const [fieldName, field] of fetcher.fieldMap) {
for (const [, field] of fetcher.fieldMap) {
const fieldName = field.name;
if (!fieldName.startsWith("...")) {
const declaringTypeNames = getDeclaringTypeNames(fieldName, fetcher);
for (const declaringTypeName of declaringTypeNames) {
Expand All @@ -86,7 +86,8 @@ export class DependencyManager {
private registerFields(resource: string, fetchers: readonly Fetcher<string, object, object>[]) {
for (const fetcher of fetchers) {
const isOperation = isOperationFetcher(fetcher);
for (const [fieldName, field] of fetcher.fieldMap) {
for (const [, field] of fetcher.fieldMap) {
const fieldName = field.name;
if (!isOperation && !fieldName.startsWith("...")) {
const subMap = compute(this.fieldResourceMap, fieldName, () => new Map<string, Resources>());
const declaringTypeNames = getDeclaringTypeNames(fieldName, fetcher);
Expand Down Expand Up @@ -121,16 +122,17 @@ export class DependencyManager {
}
}

for (const [fieldName, field] of fetcher.fieldMap) {
for (const [fieldKey, field] of fetcher.fieldMap) {
const fieldName = field.name;
if (fieldName.startsWith("...")) { // Fragment, not assocaition
if (field.childFetchers !== undefined) {
for (const childFetcher of field.childFetchers) {
this.collectResources(childFetcher, oldObject, newObject, output);
}
}
} else {
const oldValue = nullToUndefined(oldObject !== undefined ? oldObject[fieldName] : undefined);
const newValue = nullToUndefined(newObject !== undefined ? newObject[fieldName] : undefined);
const oldValue = nullToUndefined(oldObject !== undefined ? oldObject[fieldKey] : undefined);
const newValue = nullToUndefined(newObject !== undefined ? newObject[fieldKey] : undefined);
if (field.childFetchers !== undefined && field.childFetchers.length !== 0) { // association
if (oldValue !== newValue) { // Not both undefined
for (const childFetcher of field.childFetchers) {
Expand Down Expand Up @@ -195,7 +197,8 @@ export class DependencyManager {

private collectAllResources(fetcher: Fetcher<string, object, object>, output: Set<string>) {
this.rootTypeResourceMap.get(fetcher.fetchableType.name)?.copyTo(output);
for (const [fieldName, field] of fetcher.fieldMap) {
for (const [, field] of fetcher.fieldMap) {
const fieldName = field.name;
if (!fieldName.startsWith("...")) { // Not fragment
const declaringTypes = getDeclaringTypeNames(fieldName, fetcher);
for (const declaringType of declaringTypes) {
Expand Down
24 changes: 14 additions & 10 deletions api/src/Fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,20 @@ export abstract class AbstractFetcher<E extends string, T extends object, TVaria
const fieldMap = new Map<string, FetcherField>();
for (let i = fetchers.length - 1; i >= 0; --i) {
const fetcher = fetchers[i];
const fetchKey = fetcher?._fieldOptionsValue?.alias ?? fetcher._field;
if (fetcher._field.startsWith('...')) {
let childFetchers = fieldMap.get(fetcher._field)?.childFetchers as AbstractFetcher<string, object, object>[];
let childFetchers = fieldMap.get(fetchKey)?.childFetchers as AbstractFetcher<string, object, object>[];
if (childFetchers === undefined) {
childFetchers = [];
fieldMap.set(fetcher._field, { plural: false, childFetchers }); // Fragment cause mutliple child fetchers
fieldMap.set(fetchKey, { name: fetcher._field, plural: false, childFetchers }); // Fragment cause mutliple child fetchers
}
childFetchers.push(fetcher._child!);
} else {
if (fetcher._negative) {
fieldMap.delete(fetcher._field);
fieldMap.delete(fetchKey);
} else {
fieldMap.set(fetcher._field, {
fieldMap.set(fetchKey, {
name: fetcher._field,
argGraphQLTypes: fetcher.fetchableType.fields.get(fetcher._field)?.argGraphQLTypeMap,
args: fetcher._args,
fieldOptionsValue: fetcher._fieldOptionsValue,
Expand Down Expand Up @@ -250,15 +252,15 @@ export abstract class AbstractFetcher<E extends string, T extends object, TVaria
return this.result.variableTypeMap;
}

findField(fieldName: string): FetcherField | undefined {
const field = this.fieldMap.get(fieldName);
findField(fieldKey: string): FetcherField | undefined {
const field = this.fieldMap.get(fieldKey);
if (field !== undefined) {
return field;
}
for (const [fieldName, field] of this.fieldMap) {
if (fieldName.startsWith("...") && field.childFetchers !== undefined) {
for (const [fieldKey, field] of this.fieldMap) {
if (field.name.startsWith("...") && field.childFetchers !== undefined) {
for (const fragmentFetcher of field.childFetchers) {
const deeperField = fragmentFetcher.findField(fieldName);
const deeperField = fragmentFetcher.findField(fieldKey);
if (deeperField !== undefined) {
return deeperField;
}
Expand Down Expand Up @@ -329,6 +331,7 @@ export abstract class AbstractFetcher<E extends string, T extends object, TVaria
}

export interface FetcherField {
readonly name: string;
readonly argGraphQLTypes?: ReadonlyMap<string, string>;
readonly args?: object;
readonly fieldOptionsValue?: FieldOptionsValue;
Expand Down Expand Up @@ -377,7 +380,8 @@ class ResultContext {
accept(fetcher: Fetcher<string, object, object>) {

const t = this.writer.text.bind(this.writer);
for (const [fieldName, field] of fetcher.fieldMap) {
for (const [, field] of fetcher.fieldMap) {
const fieldName = field.name;
if (fieldName !== "...") { // Inline fragment
const alias = field.fieldOptionsValue?.alias;
if (alias !== undefined && alias !== "" && alias !== fieldName) {
Expand Down
Binary file modified example/.DS_Store
Binary file not shown.
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.6",
"graphql-ts-client-api": "^3.1.7",
"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.6"
"graphql-ts-client-codegen": "^3.1.7"
}
}
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.6:
version "3.1.6"
resolved "https://registry.yarnpkg.com/graphql-ts-client-api/-/graphql-ts-client-api-3.1.6.tgz#64d281293f42ba42efcbf57bc113abbf8af04f6d"
integrity sha512-L+doXlwMjcA+5tCeZWCZxECFNOmnk7o5BGhO9VVCDg4qqPUSOS46xhD6VbckV/vz/iviOD1dAfbyT/wBNSH22Q==
graphql-ts-client-api@^3.1.7:
version "3.1.7"
resolved "https://registry.yarnpkg.com/graphql-ts-client-api/-/graphql-ts-client-api-3.1.7.tgz#7ed4f53abb411b780bb3d043fff71af6e7895f9a"
integrity sha512-PACVg9ivzKZTKQjnl/XqQqirJqQ8JGsKoAs2wX7rsd5vQRViH4X0NowB7KOcBKDhs9UznLD4kAD+bt0Dd73+/Q==
dependencies:
ts-md5 "^1.2.9"

graphql-ts-client-codegen@^3.1.6:
version "3.1.6"
resolved "https://registry.yarnpkg.com/graphql-ts-client-codegen/-/graphql-ts-client-codegen-3.1.6.tgz#4681eca625ab0398fc74c3ae260efc555c648519"
integrity sha512-FcH+2KUSxK7rxs2YxktEbPxiTusZVXIENi1fOhu8i9SCMncvkirAQezI8ScgAff1n2ZTdAszCnGHVE3+VuOphA==
graphql-ts-client-codegen@^3.1.7:
version "3.1.7"
resolved "https://registry.yarnpkg.com/graphql-ts-client-codegen/-/graphql-ts-client-codegen-3.1.7.tgz#155ceee50bc6410a35aa2ec65c337b67daf4909c"
integrity sha512-/DHUHhkD5wN2hfA/cxt4Vnv/ZCfhHIZ/ulLuT//8ZHo7d3RQ8k2YAv3hJd8mWRLQzsufaD5TCZnL2PKq26PAHA==
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.6",
"graphql-ts-client-api": "^3.1.7",
"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.6",
"graphql-ts-client-codegen": "^3.1.7",
"node-fetch": "^2.6.1"
}
}
16 changes: 8 additions & 8 deletions example/client/async-demo/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5439,17 +5439,17 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.6.tgz#ff040b2b0853b23c3d31027523706f1885d76bee"
integrity sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==

graphql-ts-client-api@^3.1.6:
version "3.1.6"
resolved "https://registry.yarnpkg.com/graphql-ts-client-api/-/graphql-ts-client-api-3.1.6.tgz#64d281293f42ba42efcbf57bc113abbf8af04f6d"
integrity sha512-L+doXlwMjcA+5tCeZWCZxECFNOmnk7o5BGhO9VVCDg4qqPUSOS46xhD6VbckV/vz/iviOD1dAfbyT/wBNSH22Q==
graphql-ts-client-api@^3.1.7:
version "3.1.7"
resolved "https://registry.yarnpkg.com/graphql-ts-client-api/-/graphql-ts-client-api-3.1.7.tgz#7ed4f53abb411b780bb3d043fff71af6e7895f9a"
integrity sha512-PACVg9ivzKZTKQjnl/XqQqirJqQ8JGsKoAs2wX7rsd5vQRViH4X0NowB7KOcBKDhs9UznLD4kAD+bt0Dd73+/Q==
dependencies:
ts-md5 "^1.2.9"

graphql-ts-client-codegen@^3.1.6:
version "3.1.6"
resolved "https://registry.yarnpkg.com/graphql-ts-client-codegen/-/graphql-ts-client-codegen-3.1.6.tgz#4681eca625ab0398fc74c3ae260efc555c648519"
integrity sha512-FcH+2KUSxK7rxs2YxktEbPxiTusZVXIENi1fOhu8i9SCMncvkirAQezI8ScgAff1n2ZTdAszCnGHVE3+VuOphA==
graphql-ts-client-codegen@^3.1.7:
version "3.1.7"
resolved "https://registry.yarnpkg.com/graphql-ts-client-codegen/-/graphql-ts-client-codegen-3.1.7.tgz#155ceee50bc6410a35aa2ec65c337b67daf4909c"
integrity sha512-/DHUHhkD5wN2hfA/cxt4Vnv/ZCfhHIZ/ulLuT//8ZHo7d3RQ8k2YAv3hJd8mWRLQzsufaD5TCZnL2PKq26PAHA==
dependencies:
"@types/node" "^15.12.2"
"@types/node-fetch" "^2.5.10"
Expand Down
6 changes: 3 additions & 3 deletions example/client/relay-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"@types/react-relay": "^11.0.2",
"@types/relay-runtime": "^11.0.3",
"antd": "^4.16.13",
"graphql-ts-client-api": "^3.1.6",
"graphql-ts-client-relay": "^3.1.6",
"graphql-ts-client-api": "^3.1.7",
"graphql-ts-client-relay": "^3.1.7",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-relay": "^12.0.0",
Expand Down Expand Up @@ -51,6 +51,6 @@
]
},
"devDependencies": {
"graphql-ts-client-codegen": "^3.1.6"
"graphql-ts-client-codegen": "^3.1.7"
}
}
Loading

0 comments on commit 9060bc7

Please sign in to comment.