Skip to content

Commit 12f8330

Browse files
committed
included nico's code
1 parent a84e896 commit 12f8330

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

package-lock.json

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"@graphql-tools/graphql-file-loader": "^7.5.17",
3535
"@graphql-tools/load": "^7.8.14",
3636
"graphql": "^16.6.0",
37-
"lodash": "4.17.21"
37+
"lodash": "4.17.21",
38+
"ramda": "^0.29.0"
3839
},
3940
"devDependencies": {
4041
"@types/eslint": "^8.21.1",

src/introspection/introspection.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import _ from "lodash";
2+
import * as R from "ramda";
23

34
import {
45
GraphQLNamedType,
@@ -208,7 +209,40 @@ function assignTypesAndIDs(schema: SimplifiedIntrospection) {
208209
}
209210

210211
function addParent(schema: SimplifiedIntrospection) {
211-
return schema;
212+
function extractFields(type) {
213+
// no need to inspect circular types any further
214+
if (type.type === "[Circular]") return [type];
215+
216+
// same with scalars and enums
217+
if (R.includes(type.kind && type.kind, ["SCALAR", "ENUM"])) return [];
218+
if (
219+
type.type &&
220+
type.type.kind &&
221+
R.includes(type.type.kind, ["SCALAR", "ENUM"])
222+
)
223+
return [];
224+
225+
return R.chain((currentType) => {
226+
// if it's a composite object, use recursion to introspect the inner object
227+
const innerTypes = currentType.type.fields
228+
? R.chain((subType) => extractFields(subType), currentType.type.fields)
229+
: [currentType];
230+
// dedupe types by their id
231+
return R.uniqBy((x) => x.id, R.append(currentType, innerTypes));
232+
}, R.values(type.fields));
233+
}
234+
235+
const allFields = R.chain(
236+
(type) => extractFields(type),
237+
R.values(schema.types)
238+
);
239+
240+
/*
241+
* Group fields by their corresponding type id
242+
*/
243+
schema.types = R.groupBy(function (field) {
244+
return field.type.id;
245+
}, allFields);
212246
}
213247

214248
export function getSchema(schema: GraphQLSchema) {

0 commit comments

Comments
 (0)