Skip to content

Commit

Permalink
New API to support project graphql-state after #19 is fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
babyfish-ct committed Jun 23, 2022
1 parent 59f754e commit 184afe8
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 70 deletions.
6 changes: 3 additions & 3 deletions api/dist/DependencyManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DependencyManager {
}
registerTypes(resource, fetchers) {
for (const fetcher of fetchers) {
for (const [, field] of fetcher.fieldMap) {
for (const field of fetcher.fieldMap.values()) {
const fieldName = field.name;
if (!fieldName.startsWith("...")) {
const declaringTypeNames = getDeclaringTypeNames(fieldName, fetcher);
Expand All @@ -66,7 +66,7 @@ class DependencyManager {
registerFields(resource, fetchers) {
for (const fetcher of fetchers) {
const isOperation = isOperationFetcher(fetcher);
for (const [, field] of fetcher.fieldMap) {
for (const field of fetcher.fieldMap.values()) {
const fieldName = field.name;
if (!isOperation && !fieldName.startsWith("...")) {
const subMap = compute(this.fieldResourceMap, fieldName, () => new Map());
Expand Down Expand Up @@ -170,7 +170,7 @@ 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 [, field] of fetcher.fieldMap) {
for (const field of fetcher.fieldMap.values()) {
const fieldName = field.name;
if (!fieldName.startsWith("...")) { // Not fragment
const declaringTypes = getDeclaringTypeNames(fieldName, fetcher);
Expand Down
7 changes: 6 additions & 1 deletion api/dist/Fetcher.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export interface Fetcher<E extends string, T extends object, TVariables extends
readonly fetchableType: FetchableType<E>;
readonly fieldMap: ReadonlyMap<string, FetcherField>;
readonly directiveMap: ReadonlyMap<string, DirectiveArgs>;
findField(fieldName: string): FetcherField | undefined;
findField(fieldKey: string): FetcherField | undefined;
findFieldsByName(fieldName: string): ReadonlyArray<FetcherField>;
findFieldByName(fieldName: string): FetcherField | undefined;
toString(): string;
toFragmentString(): string;
toJSON(): string;
Expand Down Expand Up @@ -66,6 +68,9 @@ export declare abstract class AbstractFetcher<E extends string, T extends object
private _getDirectiveMap;
get variableTypeMap(): ReadonlyMap<string, string>;
findField(fieldKey: string): FetcherField | undefined;
findFieldsByName(fieldName: string): ReadonlyArray<FetcherField>;
private collectFieldsByName;
findFieldByName(fieldName: string): FetcherField | undefined;
toString(): string;
toFragmentString(): string;
toJSON(): string;
Expand Down
30 changes: 29 additions & 1 deletion api/dist/Fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,34 @@ class AbstractFetcher {
}
return undefined;
}
findFieldsByName(fieldName) {
const fields = [];
this.collectFieldsByName(fieldName, fields);
return fields;
}
collectFieldsByName(fieldName, outArr) {
for (const field of this.fieldMap.values()) {
if (field.name === fieldName) {
outArr.push(field);
}
else if (field.name.startsWith("...") && field.childFetchers !== undefined) {
for (const fragmentFetcher of field.childFetchers) {
outArr.push(...fragmentFetcher.findFieldsByName(fieldName));
}
}
}
;
}
findFieldByName(fieldName) {
const fields = this.findFieldsByName(fieldName);
if (fields.length > 1) {
throw new Error(`Too many fields named "${fieldName}" are declared in the fetcher of type "${this.fetchableType.name}"`);
}
if (fields.length === 0) {
return undefined;
}
return fields[0];
}
toString() {
return this.result.text;
}
Expand Down Expand Up @@ -226,7 +254,7 @@ class ResultContext {
accept(fetcher) {
var _a, _b;
const t = this.writer.text.bind(this.writer);
for (const [, field] of fetcher.fieldMap) {
for (const field of fetcher.fieldMap.values()) {
const fieldName = field.name;
if (fieldName !== "...") { // Inline fragment
const alias = (_a = field.fieldOptionsValue) === null || _a === void 0 ? void 0 : _a.alias;
Expand Down
6 changes: 3 additions & 3 deletions api/src/DependencyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class DependencyManager {

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

private collectAllResources(fetcher: Fetcher<string, object, object>, output: Set<string>) {
this.rootTypeResourceMap.get(fetcher.fetchableType.name)?.copyTo(output);
for (const [, field] of fetcher.fieldMap) {
for (const field of fetcher.fieldMap.values()) {
const fieldName = field.name;
if (!fieldName.startsWith("...")) { // Not fragment
const declaringTypes = getDeclaringTypeNames(fieldName, fetcher);
Expand Down
39 changes: 37 additions & 2 deletions api/src/Fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export interface Fetcher<E extends string, T extends object, TVariables extends

readonly directiveMap: ReadonlyMap<string, DirectiveArgs>;

findField(fieldName: string): FetcherField | undefined;
findField(fieldKey: string): FetcherField | undefined;

findFieldsByName(fieldName: string): ReadonlyArray<FetcherField>;

findFieldByName(fieldName: string): FetcherField | undefined;

toString(): string;

Expand Down Expand Up @@ -270,6 +274,37 @@ export abstract class AbstractFetcher<E extends string, T extends object, TVaria
return undefined;
}

findFieldsByName(fieldName: string): ReadonlyArray<FetcherField> {
const fields: FetcherField[] = [];
this.collectFieldsByName(fieldName, fields);
return fields;
}

private collectFieldsByName(fieldName: string, outArr: Array<FetcherField>) {
for (const field of this.fieldMap.values()) {
if (field.name === fieldName) {
outArr.push(field);
} else if (field.name.startsWith("...") && field.childFetchers !== undefined) {
for (const fragmentFetcher of field.childFetchers) {
outArr.push(...fragmentFetcher.findFieldsByName(fieldName));
}
}
};
}

findFieldByName(fieldName: string): FetcherField | undefined {
const fields = this.findFieldsByName(fieldName);
if (fields.length > 1) {
throw new Error(
`Too many fields named "${fieldName}" are declared in the fetcher of type "${this.fetchableType.name}"`
);
}
if (fields.length === 0) {
return undefined;
}
return fields[0];
}

toString(): string {
return this.result.text;
}
Expand Down Expand Up @@ -380,7 +415,7 @@ class ResultContext {
accept(fetcher: Fetcher<string, object, object>) {

const t = this.writer.text.bind(this.writer);
for (const [, field] of fetcher.fieldMap) {
for (const field of fetcher.fieldMap.values()) {
const fieldName = field.name;
if (fieldName !== "...") { // Inline fragment
const alias = field.fieldOptionsValue?.alias;
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.7",
"graphql-ts-client-api": "^3.1.8",
"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.7"
"graphql-ts-client-codegen": "^3.1.8"
}
}
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.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==
graphql-ts-client-api@^3.1.8:
version "3.1.8"
resolved "https://registry.yarnpkg.com/graphql-ts-client-api/-/graphql-ts-client-api-3.1.8.tgz#493d39c3101569abf39680c2d3274343b3168a48"
integrity sha512-Dct05eoymzVLK7mScaWw8SD2cmHV5leOUOqiWmQw2IhjpHqsLSfuwnhPyGMv/QRzH1QO4ZQ7FbqUMzVR5/SZOA==
dependencies:
ts-md5 "^1.2.9"

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==
graphql-ts-client-codegen@^3.1.8:
version "3.1.8"
resolved "https://registry.yarnpkg.com/graphql-ts-client-codegen/-/graphql-ts-client-codegen-3.1.8.tgz#7b59816d62e11a443608457197b312df199ab2a2"
integrity sha512-qF16zpIZU9FxTAdc8KbZDuSBFazis2HbdqlwVb4WoMMOE/UyJpMcUp3Sn6ITbYNXtGEgeRBVP6ksAN22fQwezg==
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.7",
"graphql-ts-client-api": "^3.1.8",
"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.7",
"graphql-ts-client-codegen": "^3.1.8",
"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.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==
graphql-ts-client-api@^3.1.8:
version "3.1.8"
resolved "https://registry.yarnpkg.com/graphql-ts-client-api/-/graphql-ts-client-api-3.1.8.tgz#493d39c3101569abf39680c2d3274343b3168a48"
integrity sha512-Dct05eoymzVLK7mScaWw8SD2cmHV5leOUOqiWmQw2IhjpHqsLSfuwnhPyGMv/QRzH1QO4ZQ7FbqUMzVR5/SZOA==
dependencies:
ts-md5 "^1.2.9"

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==
graphql-ts-client-codegen@^3.1.8:
version "3.1.8"
resolved "https://registry.yarnpkg.com/graphql-ts-client-codegen/-/graphql-ts-client-codegen-3.1.8.tgz#7b59816d62e11a443608457197b312df199ab2a2"
integrity sha512-qF16zpIZU9FxTAdc8KbZDuSBFazis2HbdqlwVb4WoMMOE/UyJpMcUp3Sn6ITbYNXtGEgeRBVP6ksAN22fQwezg==
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.7",
"graphql-ts-client-relay": "^3.1.7",
"graphql-ts-client-api": "^3.1.8",
"graphql-ts-client-relay": "^3.1.8",
"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.7"
"graphql-ts-client-codegen": "^3.1.8"
}
}
30 changes: 15 additions & 15 deletions example/client/relay-demo/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6207,28 +6207,28 @@ 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.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==
graphql-ts-client-api@^3.1.8:
version "3.1.8"
resolved "https://registry.yarnpkg.com/graphql-ts-client-api/-/graphql-ts-client-api-3.1.8.tgz#493d39c3101569abf39680c2d3274343b3168a48"
integrity sha512-Dct05eoymzVLK7mScaWw8SD2cmHV5leOUOqiWmQw2IhjpHqsLSfuwnhPyGMv/QRzH1QO4ZQ7FbqUMzVR5/SZOA==
dependencies:
ts-md5 "^1.2.9"

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==
graphql-ts-client-codegen@^3.1.8:
version "3.1.8"
resolved "https://registry.yarnpkg.com/graphql-ts-client-codegen/-/graphql-ts-client-codegen-3.1.8.tgz#7b59816d62e11a443608457197b312df199ab2a2"
integrity sha512-qF16zpIZU9FxTAdc8KbZDuSBFazis2HbdqlwVb4WoMMOE/UyJpMcUp3Sn6ITbYNXtGEgeRBVP6ksAN22fQwezg==
dependencies:
"@types/node" "^15.12.2"
"@types/node-fetch" "^2.5.10"
graphql "^15.5.1"
node-fetch "^2.6.1"
typescript "^4.3.2"

graphql-ts-client-relay@^3.1.7:
version "3.1.7"
resolved "https://registry.yarnpkg.com/graphql-ts-client-relay/-/graphql-ts-client-relay-3.1.7.tgz#98a4d98e847e790ace3891a83cc7c4a9946e9b0a"
integrity sha512-clOmgXrRTwi6t2YZgebsOXN+7N0S7tk/lvjoUh0WwWCct2BYmfsBwnhwwGCLgxL50nkjzp7bP23n+ZFZTkplfQ==
graphql-ts-client-relay@^3.1.8:
version "3.1.8"
resolved "https://registry.yarnpkg.com/graphql-ts-client-relay/-/graphql-ts-client-relay-3.1.8.tgz#b89aa80e1402e8852c78bd1d0cb66042ab22e866"
integrity sha512-xDSMj7vM885WMU0hjZQ3ieSJy+VfjaQGKtAyFfH2w3VvJVTraGHimEcv0kbSeWDMjc5svoZZ7t+fQVqF6bEd3Q==
dependencies:
"@types/node" "^16.7.10"
"@types/react-relay" "^11.0.2"
Expand Down Expand Up @@ -12262,9 +12262,9 @@ upath@^1.1.1, upath@^1.1.2, upath@^1.2.0:
integrity sha512-aZwGpamFO61g3OlfT7OQCHqhGnW43ieH9WZeP7QxN/G/jS4jfqUkZxoryvJgVPEcrl5NL/ggHsSmLMHuH64Lhg==

update-browserslist-db@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/update-browserslist-db/-/update-browserslist-db-1.0.3.tgz#6c47cb996f34afb363e924748e2f6e4d983c6fc1"
integrity sha512-ufSazemeh9Gty0qiWtoRpJ9F5Q5W3xdIPm1UZQqYQv/q0Nyb9EMHUB2lu+O9x1re9WsorpMAUu4Y6Lxcs5n+XQ==
version "1.0.4"
resolved "https://registry.yarnpkg.com/update-browserslist-db/-/update-browserslist-db-1.0.4.tgz#dbfc5a789caa26b1db8990796c2c8ebbce304824"
integrity sha512-jnmO2BEGUjsMOe/Fg9u0oczOe/ppIDZPebzccl1yDWGLFP16Pa1/RM5wEoKYPG2zstNcDuAStejyxsOuKINdGA==
dependencies:
escalade "^3.1.1"
picocolors "^1.0.0"
Expand Down
6 changes: 3 additions & 3 deletions example/client/relay-tutorial/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"@types/react-dom": "^17.0.0",
"@types/react-relay": "^11.0.2",
"@types/relay-runtime": "^11.0.3",
"graphql-ts-client-api": "^3.1.7",
"graphql-ts-client-relay": "^3.1.7",
"graphql-ts-client-api": "^3.1.8",
"graphql-ts-client-relay": "^3.1.8",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-relay": "^12.0.0",
Expand Down Expand Up @@ -50,6 +50,6 @@
]
},
"devDependencies": {
"graphql-ts-client-codegen": "^3.1.7"
"graphql-ts-client-codegen": "^3.1.8"
}
}
Loading

0 comments on commit 184afe8

Please sign in to comment.