Last active
August 17, 2021 22:49
-
-
Save b4n92uid/00ba81ba0dd123af50992d3cbd250f6a to your computer and use it in GitHub Desktop.
[Apollo] propertyAccess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function propertyAccess(object, key) { | |
if (isFunction(object[key])) return object[key](); | |
if (object[key] instanceof Date) return object[key].toISOString(); | |
if (key in object) return object[key]; | |
const conn = getConnection(); | |
if (conn.hasMetadata(object.constructor.name)) { | |
const rel = conn | |
.getMetadata(object.constructor.name) | |
.findRelationWithPropertyPath(key); | |
if (rel) { | |
if (rel.isManyToOne || rel.isOneToOne) { | |
return conn | |
.createQueryBuilder() | |
.relation(object.constructor.name, key) | |
.of(object) | |
.loadOne(); | |
} else { | |
return conn | |
.createQueryBuilder() | |
.relation(object.constructor.name, key) | |
.of(object) | |
.loadMany(); | |
} | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment