chore(orchestrator): remote joiner using entitymap (#9205)

This commit is contained in:
Carlos R. L. Rodrigues
2024-09-20 05:30:08 -03:00
committed by GitHub
parent 2cb9322ef5
commit 1215a7c094
6 changed files with 361 additions and 140 deletions

View File

@@ -0,0 +1,41 @@
import { isListType, isNonNullType, isObjectType } from "graphql"
/**
* Extracts only the relation fields from the GraphQL type map.
* @param {Map<string, any>} typeMap - The GraphQL schema TypeMap.
* @returns {Map<string, Map<string, string>>} A map where each key is an entity name, and the values are a map of relation fields and their corresponding entity type.
*/
export function extractRelationsFromGQL(
typeMap: Map<string, any>
): Map<string, Map<string, string>> {
const relationMap = new Map()
// Extract the actual type
const getBaseType = (type) => {
if (isNonNullType(type) || isListType(type)) {
return getBaseType(type.ofType)
}
return type
}
for (const [typeName, graphqlType] of Object.entries(typeMap)) {
if (!isObjectType(graphqlType)) {
continue
}
const fields = graphqlType.getFields()
const entityRelations = new Map()
for (const [fieldName, fieldConfig] of Object.entries(fields)) {
const fieldType = getBaseType((fieldConfig as any).type)
// only add relation fields
if (isObjectType(fieldType)) {
entityRelations.set(fieldName, fieldType.name)
}
}
relationMap.set(typeName, entityRelations)
}
return relationMap
}

View File

@@ -26,6 +26,7 @@ export * from "./get-node-version"
export * from "./get-selects-and-relations-from-object-array"
export * from "./get-set-difference"
export * from "./graceful-shutdown-server"
export * from "./graphql-relations-entity-map"
export * from "./group-by"
export * from "./handle-postgres-database-error"
export * from "./is-big-number"