Skip to content

Commit d9da41f

Browse files
committed
feat(parser): code improvements
1 parent 0921717 commit d9da41f

File tree

3 files changed

+198
-24
lines changed

3 files changed

+198
-24
lines changed

src/helpers.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const ROOT_TYPE_LOCALE = {
2+
DIRECTIVE: { singular: "directive", plural: "directives" },
3+
ENUM: { singular: "enum", plural: "enums" },
4+
INPUT: { singular: "input", plural: "inputs" },
5+
INTERFACE: { singular: "interface", plural: "interfaces" },
6+
MUTATION: { singular: "mutation", plural: "mutations" },
7+
OPERATION: { singular: "operation", plural: "operations" },
8+
QUERY: { singular: "query", plural: "queries" },
9+
SCALAR: { singular: "scalar", plural: "scalars" },
10+
SUBSCRIPTION: { singular: "subscription", plural: "subscriptions" },
11+
OBJECT: { singular: "object", plural: "objects" },
12+
UNION: { singular: "union", plural: "unions" },
13+
};

src/parser.ts

Lines changed: 100 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
import { loadSchemaSync } from "@graphql-tools/load";
22
import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader";
33
import { UrlLoader } from "@graphql-tools/url-loader";
4-
5-
// import * as fs from "fs";
6-
// import stringifyObject from "stringify-object";
7-
84
import { getSchema } from "./introspection/introspection.js";
95
import { GraphQLSchema } from "graphql";
106
import {
11-
SimplifiedIntrospection,
12-
SimplifiedType,
13-
} from "./introspection/types.js";
7+
SimplifiedField,
8+
SimplifiedIntrospectionWithIds,
9+
SimplifiedTypeWithIDs,
10+
} from "./types.js";
1411
import map from "lodash/map.js";
1512
import compact from "lodash/compact.js";
1613
import find from "lodash/find.js";
1714
import groupBy from "lodash/groupBy.js";
1815
import orderBy from "lodash/orderBy.js";
1916

2017
import stringify from "safe-stable-stringify";
18+
import { ROOT_TYPE_LOCALE } from "./helpers.js";
2119

2220
export class schemaParser {
2321
location: string;
2422
schema: GraphQLSchema;
25-
simplifiedSchema: SimplifiedIntrospection;
23+
simplifiedSchema: SimplifiedIntrospectionWithIds;
2624

2725
constructor(uri: string) {
2826
this.schema = this.loadSchema(uri);
@@ -66,14 +64,37 @@ export class schemaParser {
6664
// );
6765
// }
6866

67+
//
6968
getRoutes(): { params: { type: string[] } }[] {
70-
const routes = map(this.simplifiedSchema.types, (type) => {
71-
if (!type.name.startsWith("__")) {
72-
return [type.kind.toLowerCase(), type.name.toLocaleLowerCase()];
69+
const queryRoot = this.simplifiedSchema.queryType.name;
70+
const mutationRoot = this.simplifiedSchema.mutationType.name;
71+
72+
const objectRoutes = map(this.simplifiedSchema.types, (type) => {
73+
const segment = ROOT_TYPE_LOCALE[type.kind].plural;
74+
75+
if (
76+
!type.name.startsWith("__") &&
77+
!(type.name == queryRoot) &&
78+
!(type.name == mutationRoot)
79+
) {
80+
return [segment, type.name.toLocaleLowerCase()];
7381
}
7482
});
7583

76-
return compact(routes).map((route) => {
84+
const queryRoutes = map(this.simplifiedSchema.queryType.fields, (query) => {
85+
return ["queries", query.name.toLocaleLowerCase()];
86+
});
87+
88+
const mutationRoutes = map(
89+
this.simplifiedSchema.mutationType.fields,
90+
(mutation) => {
91+
return ["mutations", mutation.name.toLocaleLowerCase()];
92+
}
93+
);
94+
95+
const allRoutes = [...objectRoutes, ...queryRoutes, ...mutationRoutes];
96+
97+
return compact(allRoutes).map((route) => {
7798
return { params: { type: route } };
7899
});
79100
}
@@ -82,28 +103,83 @@ export class schemaParser {
82103
// FIXME: it could be defined as constructor to improve performance
83104
// FIXME: also construct prefix if needed or empty
84105
getSidebar(prefix?: string) {
85-
const sidebar = map(this.simplifiedSchema.types, (type) => {
106+
const objects = map(this.simplifiedSchema.types, (type) => {
107+
const segment = ROOT_TYPE_LOCALE[type.kind].plural;
108+
86109
if (!type.name.startsWith("__")) {
87110
return {
88111
kind: type.kind,
89112
name: type.name,
90-
path: `${type.kind.toLowerCase()}/${type.name.toLowerCase()}`,
113+
path: `${segment}/${type.name.toLowerCase()}`,
91114
};
92115
}
93116
});
94-
return groupBy(orderBy(compact(sidebar), "name", "asc"), "kind");
117+
118+
const queries = map(this.simplifiedSchema.queryType.fields, (query) => {
119+
return {
120+
kind: "QUERY",
121+
name: query.name,
122+
path: `queries/${query.name.toLowerCase()}`,
123+
};
124+
});
125+
126+
const mutations = map(
127+
this.simplifiedSchema.mutationType.fields,
128+
(mutation) => {
129+
return {
130+
kind: "MUTATION",
131+
name: mutation.name,
132+
path: `mutations/${mutation.name.toLowerCase()}`,
133+
};
134+
}
135+
);
136+
137+
const allObjects = [...objects, ...queries, ...mutations];
138+
const orderedObjects = groupBy(
139+
orderBy(compact(allObjects), "name", "asc"),
140+
"kind"
141+
);
142+
console.log(orderedObjects);
143+
return orderedObjects;
95144
}
96145

97146
// FIXME: Define a type? maybe reuse a type?
98-
getTypename(name: string): object {
99-
// FIXME: there should be a better way to do this ... can it be right to uppercase all objects by default?
100-
const type: SimplifiedType = find(
101-
this.simplifiedSchema.types,
102-
function (o) {
103-
return o.name.toUpperCase() == name.toUpperCase();
147+
// FIXME: there should be a better way to do this ... can it be right to uppercase all objects by default?
148+
// FIXME: also, the kind is not a kind, given it's plural and part of grouping on the sidebar
149+
getTypeName(kind: string, name: string): object {
150+
switch (kind) {
151+
case "queries": {
152+
const query: SimplifiedField<SimplifiedTypeWithIDs> = find(
153+
this.simplifiedSchema.queryType.fields,
154+
function (o) {
155+
return o.name.toUpperCase() == name.toUpperCase();
156+
}
157+
);
158+
const t = stringify(query);
159+
return JSON.parse(t);
104160
}
105-
);
106-
const t = stringify(type);
107-
return JSON.parse(t);
161+
162+
case "mutations": {
163+
const mutation: SimplifiedField<SimplifiedTypeWithIDs> = find(
164+
this.simplifiedSchema.mutationType.fields,
165+
function (o) {
166+
return o.name.toUpperCase() == name.toUpperCase();
167+
}
168+
);
169+
const t = stringify(mutation);
170+
return JSON.parse(t);
171+
}
172+
173+
default: {
174+
const type: SimplifiedTypeWithIDs = find(
175+
this.simplifiedSchema.types,
176+
function (o) {
177+
return o.name.toUpperCase() == name.toUpperCase();
178+
}
179+
);
180+
const t = stringify(type);
181+
return JSON.parse(t);
182+
}
183+
}
108184
}
109185
}

src/types.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { IntrospectionEnumValue } from "graphql";
2+
3+
export interface SimplifiedArg {
4+
name: string;
5+
description: string;
6+
defaultValue: any;
7+
typeWrappers: Array<"NON_NULL" | "LIST">;
8+
id?: string;
9+
}
10+
11+
export interface SimplifiedField<T> {
12+
name: string;
13+
type: T;
14+
id?: string;
15+
relayType?: T;
16+
description: string | null;
17+
typeWrappers: Array<"NON_NULL" | "LIST">;
18+
isDeprecated: boolean;
19+
deprecationReason: string | null;
20+
args: {
21+
[name: string]: SimplifiedArg;
22+
};
23+
relayArgs?: {
24+
[name: string]: SimplifiedArg;
25+
};
26+
}
27+
28+
export type SimplifiedInputField = SimplifiedArg;
29+
30+
export interface SimplifiedTypeBase {
31+
kind: "OBJECT" | "INTERFACE" | "UNION" | "ENUM" | "INPUT_OBJECT" | "SCALAR";
32+
name: string;
33+
description: string;
34+
enumValues?: Array<IntrospectionEnumValue>;
35+
inputFields?: {
36+
[name: string]: SimplifiedInputField;
37+
};
38+
isRelayType?: boolean;
39+
}
40+
41+
export type SimplifiedType = SimplifiedTypeBase & {
42+
fields?: {
43+
[name: string]: SimplifiedField<string>;
44+
};
45+
interfaces?: Array<string>;
46+
derivedTypes?: Array<string>;
47+
possibleTypes?: Array<string>;
48+
};
49+
50+
export type SimplifiedTypeWithIDs = SimplifiedTypeBase & {
51+
id: string;
52+
fields?: {
53+
[name: string]: SimplifiedField<SimplifiedTypeWithIDs>;
54+
};
55+
interfaces?: Array<{
56+
id: string;
57+
type: SimplifiedTypeWithIDs;
58+
}>;
59+
derivedTypes?: Array<{
60+
id: string;
61+
type: SimplifiedTypeWithIDs;
62+
}>;
63+
possibleTypes?: Array<{
64+
id: string;
65+
type: SimplifiedTypeWithIDs;
66+
}>;
67+
};
68+
69+
export interface SimplifiedIntrospection {
70+
types: {
71+
[typeName: string]: SimplifiedType;
72+
};
73+
queryType: string;
74+
mutationType: string | null;
75+
subscriptionType: string | null;
76+
}
77+
78+
export interface SimplifiedIntrospectionWithIds {
79+
types: {
80+
[typeName: string]: SimplifiedTypeWithIDs;
81+
};
82+
queryType: SimplifiedTypeWithIDs;
83+
mutationType: SimplifiedTypeWithIDs | null;
84+
subscriptionType: SimplifiedTypeWithIDs | null;
85+
}

0 commit comments

Comments
 (0)