Skip to content

Commit a753f5b

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

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import stringify from "safe-stable-stringify";
55
const parsedSchema = new schemaParser("schema/schema.graphql");
66

77
// Dump stringified schema
8-
console.log(stringify(parsedSchema));
8+
// console.log(stringify(parsedSchema));
99

1010
// Dump a type
1111
console.log(parsedSchema.getTypename("Author"));

src/introspection/introspection.ts

+87-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import _ from "lodash";
1+
import _, { map, shuffle } from "lodash";
22

33
import {
44
GraphQLNamedType,
@@ -15,6 +15,16 @@ import {
1515
isInterfaceType,
1616
isScalarType,
1717
SingleFieldSubscriptionsRule,
18+
GraphQLEnumType,
19+
GraphQLInputObjectType,
20+
GraphQLInputType,
21+
GraphQLInterfaceType,
22+
GraphQLList,
23+
GraphQLNonNull,
24+
GraphQLObjectType,
25+
GraphQLOutputType,
26+
GraphQLScalarType,
27+
GraphQLUnionType,
1828
} from "graphql";
1929
import {
2030
SimplifiedIntrospection,
@@ -25,7 +35,31 @@ import {
2535
} from "./types.js";
2636
import { typeNameToId } from "./utils.js";
2737

28-
function unwrapType(type) {
38+
function unwrapType(
39+
type:
40+
| GraphQLScalarType<unknown, unknown>
41+
| GraphQLEnumType
42+
| GraphQLInputObjectType
43+
| GraphQLList<GraphQLInputType>
44+
| GraphQLNonNull<
45+
| GraphQLEnumType
46+
| GraphQLInputObjectType
47+
| GraphQLScalarType<unknown, unknown>
48+
| GraphQLList<GraphQLInputType>
49+
>
50+
| GraphQLInterfaceType
51+
| GraphQLUnionType
52+
| GraphQLObjectType<any, any>
53+
| GraphQLList<GraphQLOutputType>
54+
| GraphQLNonNull<
55+
| GraphQLEnumType
56+
| GraphQLInterfaceType
57+
| GraphQLUnionType
58+
| GraphQLScalarType<unknown, unknown>
59+
| GraphQLObjectType<any, any>
60+
| GraphQLList<GraphQLOutputType>
61+
>
62+
) {
2963
let unwrappedType = type;
3064
const typeWrappers = [];
3165

@@ -208,7 +242,57 @@ function assignTypesAndIDs(schema: SimplifiedIntrospection) {
208242
}
209243

210244
function addParent(schema: SimplifiedIntrospection) {
211-
return schema;
245+
function extractFields(type) {
246+
// no need to inspect circular types any further
247+
if (type.type === "[Circular]") return [type];
248+
249+
// same with scalars and enums
250+
if (_.includes(["SCALAR", "ENUM"], type.kind && type.kind)) return [];
251+
if (
252+
type.type &&
253+
type.type.kind &&
254+
_.includes(["SCALAR", "ENUM"], type.type.kind)
255+
)
256+
return [];
257+
258+
return _.flatMap(_.values(type.fields), (currentType) => {
259+
// if it's a composite object, use recursion to introspect the inner object
260+
const innerTypes = currentType.type.fields
261+
? _.flatMap(currentType.type.fields, (subType) =>
262+
extractFields(subType)
263+
)
264+
: [currentType];
265+
// dedupe types by their id
266+
return _.uniqBy(_.concat(innerTypes, currentType), (x) => x.id);
267+
});
268+
}
269+
270+
const allFields = _.flatMap(_.values(schema.types), (type) =>
271+
extractFields(type)
272+
);
273+
274+
/*
275+
* Group fields by their corresponding type id
276+
*/
277+
const groupedFieldsByType = _.groupBy(allFields, function (field) {
278+
return field.type.id;
279+
});
280+
281+
/*
282+
* Extract id and prepare the mapping
283+
*/
284+
const mappings = _.mapValues(groupedFieldsByType, function (t) {
285+
return _.map(t, (field) => field.id);
286+
});
287+
288+
/*
289+
* Assign the mapping
290+
*/
291+
_.each(Object.keys(schema.types), (type) => {
292+
if (mappings[type]) {
293+
(schema.types[type] as any).parents = mappings[type];
294+
}
295+
});
212296
}
213297

214298
export function getSchema(schema: GraphQLSchema) {

0 commit comments

Comments
 (0)