-
-
Notifications
You must be signed in to change notification settings - Fork 531
Update utils.ts #435
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
base: main
Are you sure you want to change the base?
Update utils.ts #435
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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]; | ||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Suggestion: Adjust Since 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
Suggested change
|
||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
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], | ||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.