Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the parent of a type #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import stringify from "safe-stable-stringify";
const parsedSchema = new schemaParser("schema/schema.graphql");

// Dump stringified schema
console.log(stringify(parsedSchema));
// console.log(stringify(parsedSchema));

// Dump a type
console.log(parsedSchema.getTypename("Author"));
Expand Down
90 changes: 87 additions & 3 deletions src/introspection/introspection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import _, { map, shuffle } from "lodash";

import {
GraphQLNamedType,
Expand All @@ -15,6 +15,16 @@ import {
isInterfaceType,
isScalarType,
SingleFieldSubscriptionsRule,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLInputType,
GraphQLInterfaceType,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLOutputType,
GraphQLScalarType,
GraphQLUnionType,
} from "graphql";
import {
SimplifiedIntrospection,
Expand All @@ -25,7 +35,31 @@ import {
} from "./types.js";
import { typeNameToId } from "./utils.js";

function unwrapType(type) {
function unwrapType(
type:
| GraphQLScalarType<unknown, unknown>
| GraphQLEnumType
| GraphQLInputObjectType
| GraphQLList<GraphQLInputType>
| GraphQLNonNull<
| GraphQLEnumType
| GraphQLInputObjectType
| GraphQLScalarType<unknown, unknown>
| GraphQLList<GraphQLInputType>
>
| GraphQLInterfaceType
| GraphQLUnionType
| GraphQLObjectType<any, any>
| GraphQLList<GraphQLOutputType>
| GraphQLNonNull<
| GraphQLEnumType
| GraphQLInterfaceType
| GraphQLUnionType
| GraphQLScalarType<unknown, unknown>
| GraphQLObjectType<any, any>
| GraphQLList<GraphQLOutputType>
>
) {
let unwrappedType = type;
const typeWrappers = [];

Expand Down Expand Up @@ -208,7 +242,57 @@ function assignTypesAndIDs(schema: SimplifiedIntrospection) {
}

function addParent(schema: SimplifiedIntrospection) {
return schema;
function extractFields(type) {
// no need to inspect circular types any further
if (type.type === "[Circular]") return [type];

// same with scalars and enums
if (_.includes(["SCALAR", "ENUM"], type.kind && type.kind)) return [];
if (
type.type &&
type.type.kind &&
_.includes(["SCALAR", "ENUM"], type.type.kind)
)
return [];

return _.flatMap(_.values(type.fields), (currentType) => {
// if it's a composite object, use recursion to introspect the inner object
const innerTypes = currentType.type.fields
? _.flatMap(currentType.type.fields, (subType) =>
extractFields(subType)
)
: [currentType];
// dedupe types by their id
return _.uniqBy(_.concat(innerTypes, currentType), (x) => x.id);
});
}

const allFields = _.flatMap(_.values(schema.types), (type) =>
extractFields(type)
);

/*
* Group fields by their corresponding type id
*/
const groupedFieldsByType = _.groupBy(allFields, function (field) {
return field.type.id;
});

/*
* Extract id and prepare the mapping
*/
const mappings = _.mapValues(groupedFieldsByType, function (t) {
return _.map(t, (field) => field.id);
});

/*
* Assign the mapping
*/
_.each(Object.keys(schema.types), (type) => {
if (mappings[type]) {
(schema.types[type] as any).parents = mappings[type];
}
});
}

export function getSchema(schema: GraphQLSchema) {
Expand Down