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

Update utils.ts #435

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
136 changes: 79 additions & 57 deletions src/introspection/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,107 @@ import {
isUnionType,
} from 'graphql/type';

export function typeObjToId(type: GraphQLNamedType) {
return typeNameToId(type.name);
type Mapper<T, R> = (id: string, item: T) => R | null;
type Payload<T, R> = {
items: T[];
mapper: Mapper<T, R>;
idParts: string[];
};

const SEPARATOR = "::";

function joinParts(parts: string[]) {
return parts.join(SEPARATOR);
}

export function typeNameToId(name: string) {
return `TYPE::${name}`;
return joinParts(["TYPE", name]);
}

export function typeObjToId(type: GraphQLNamedType) {
return typeNameToId(type.name);
}

export function extractTypeName(typeID: string): string {
const [, type] = typeID.split('::');
const [, type] = typeID.split(SEPARATOR);

return type;
}

function getDerivedTypes(schema: GraphQLSchema, type: GraphQLNamedType) {
const { interfaces, objects } = schema.getImplementations(type);

return [...interfaces, ...objects];
};
IvanGoncharov marked this conversation as resolved.
Show resolved Hide resolved

function mapItems<T extends { name: string }, R>({
items,
mapper,
idParts,
}: Payload<T, R>) {
return items.reduce((results: R[], item) => {
const id = joinParts([...idParts, item.name]);
const result = mapper(id, item);

if (result != null) results.push(result);

return results;
}, []);
}

export function mapFields<R>(
type: GraphQLNamedType,
fn: (id: string, field: GraphQLField<any, any>) => R | null,
): Array<R> {
const array = [];
if (isInterfaceType(type) || isObjectType(type)) {
for (const field of Object.values(type.getFields())) {
const id = `FIELD::${type.name}::${field.name}`;
const result = fn(id, field);
if (result != null) {
array.push(result);
}
}
}
return array;
mapper: Mapper<GraphQLField<any, any>, R>
) {
const isValidType = isInterfaceType(type) || isObjectType(type);

if (!isValidType) return [];

return mapItems({
items: Object.values(type.getFields()),
mapper,
idParts: ["FIELD", type.name],
});
}

export function mapPossibleTypes<R>(
type: GraphQLNamedType,
fn: (id: string, type: GraphQLObjectType) => R | null,
): Array<R> {
const array = [];
if (isUnionType(type)) {
for (const possibleType of type.getTypes()) {
const id = `POSSIBLE_TYPE::${type.name}::${possibleType.name}`;
const result = fn(id, possibleType);
if (result != null) {
array.push(result);
}
}
}
return array;
mapper: Mapper<GraphQLObjectType, R>
) {
if (!isUnionType(type)) return [];

return mapItems({
items: type.getTypes(),
mapper,
idParts: ["POSSIBLE_TYPE", type.name],
});
}

export function mapDerivedTypes<R>(
schema: GraphQLSchema,
type: GraphQLNamedType,
fn: (id: string, type: GraphQLObjectType | GraphQLInterfaceType) => R | null,
): Array<R> {
const array = [];
if (isInterfaceType(type)) {
const { interfaces, objects } = schema.getImplementations(type);
for (const derivedType of [...interfaces, ...objects]) {
const id = `DERIVED_TYPE::${type.name}::${derivedType.name}`;
const result = fn(id, derivedType);
if (result != null) {
array.push(result);
}
}
}
return array;
mapper: Mapper<GraphQLObjectType | GraphQLInterfaceType, R>
) {
if (!isInterfaceType(type)) return [];

return mapItems({
items: getDerivedTypes(schema, type),
mapper,
idParts: ["DERIVED_TYPE", type.name],
});
Comment on lines +91 to +99
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Suggestion: Adjust type parameter to GraphQLInterfaceType in mapDerivedTypes

Since mapDerivedTypes is applicable only to interface types, narrowing the type parameter enhances type safety and eliminates unnecessary type checks.

Apply this diff to refine the parameter type and remove redundant checks:

-export function mapDerivedTypes<R>(
-  schema: GraphQLSchema,
-  type: GraphQLNamedType,
-  mapper: Mapper<GraphQLObjectType | GraphQLInterfaceType, R>
-) {
-  if (!isInterfaceType(type)) return [];
+export function mapDerivedTypes<R>(
+  schema: GraphQLSchema,
+  type: GraphQLInterfaceType,
+  mapper: Mapper<GraphQLObjectType | GraphQLInterfaceType, R>
+) {

   return mapItems({
     items: getDerivedTypes(schema, type),
     mapper,
     idParts: ["DERIVED_TYPE", type.name],
   });
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
mapper: Mapper<GraphQLObjectType | GraphQLInterfaceType, R>
) {
if (!isInterfaceType(type)) return [];
return mapItems({
items: getDerivedTypes(schema, type),
mapper,
idParts: ["DERIVED_TYPE", type.name],
});
mapper: Mapper<GraphQLObjectType | GraphQLInterfaceType, R>
) {
return mapItems({
items: getDerivedTypes(schema, type),
mapper,
idParts: ["DERIVED_TYPE", type.name],
});

}

export function mapInterfaces<R>(
type: GraphQLNamedType,
fn: (id: string, type: GraphQLInterfaceType) => R | null,
): Array<R> {
const array = [];
if (isInterfaceType(type) || isObjectType(type)) {
for (const baseType of type.getInterfaces()) {
const id = `INTERFACE::${type.name}::${baseType.name}`;
const result = fn(id, baseType);
if (result != null) {
array.push(result);
}
}
}
return array;
mapper: Mapper<GraphQLNamedType, R>
) {
const isValidType = isInterfaceType(type) || isObjectType(type);

if (!isValidType) return [];

return mapItems({
items: type.getInterfaces(),
mapper,
idParts: ["INTERFACE", type.name],
});
}