|
| 1 | +import { wrapSchema, introspectSchema } from '@graphql-tools/wrap'; |
| 2 | +import makeRemoteTransport from '../remoteTransport'; |
| 3 | +import { delegateToSchema } from '@graphql-tools/delegate'; |
| 4 | +import { TransformQuery } from '@graphql-tools/wrap'; |
| 5 | +import { Kind } from 'graphql'; |
| 6 | + |
| 7 | +function getConnectionTypes(prefix) { |
| 8 | + return ` |
| 9 | + extend type ${prefix}Badge { |
| 10 | + details: CmsBadge |
| 11 | + } |
| 12 | +
|
| 13 | + extend type ${prefix}User { |
| 14 | + sites: [CmsSite] |
| 15 | + } |
| 16 | + `; |
| 17 | +} |
| 18 | + |
| 19 | +function getConnectionResolvers(prefix, schemas) { |
| 20 | + return { |
| 21 | + [`${prefix}Badge`]: { |
| 22 | + details: { |
| 23 | + selectionSet: '{ id }', |
| 24 | + async resolve(parent, args, context, info) { |
| 25 | + return delegateToSchema({ |
| 26 | + schema: schemas.cms, |
| 27 | + operation: 'query', |
| 28 | + fieldName: 'badgeCollection', |
| 29 | + args: { |
| 30 | + where: { |
| 31 | + id: parent.id, |
| 32 | + }, |
| 33 | + limit: 1, |
| 34 | + }, |
| 35 | + context, |
| 36 | + info, |
| 37 | + transforms: [ |
| 38 | + new TransformQuery({ |
| 39 | + path: ['badgeCollection'], |
| 40 | + queryTransformer: (subtree) => ({ |
| 41 | + kind: Kind.SELECTION_SET, |
| 42 | + selections: [ |
| 43 | + { |
| 44 | + kind: Kind.FIELD, |
| 45 | + name: { |
| 46 | + kind: Kind.NAME, |
| 47 | + value: 'items', |
| 48 | + }, |
| 49 | + selectionSet: subtree, |
| 50 | + }, |
| 51 | + ], |
| 52 | + }), |
| 53 | + resultTransformer: (r) => r?.items[0], |
| 54 | + }), |
| 55 | + ], |
| 56 | + }); |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + [`${prefix}User`]: { |
| 61 | + sites: { |
| 62 | + selectionSet: '{ roles }', |
| 63 | + async resolve(parent, args, context, info) { |
| 64 | + return delegateToSchema({ |
| 65 | + schema: schemas.cms, |
| 66 | + operation: 'query', |
| 67 | + fieldName: 'siteCollection', |
| 68 | + args: { |
| 69 | + where: { |
| 70 | + OR: [...parent.roles.map((role) => { |
| 71 | + if (role.name == "Staff") return { type: "Volunteer" }; |
| 72 | + return { type: role.name }; |
| 73 | + }), { type: "Student" }] |
| 74 | + } |
| 75 | + }, |
| 76 | + context, |
| 77 | + info, |
| 78 | + transforms: [ |
| 79 | + new TransformQuery({ |
| 80 | + path: ['siteCollection'], |
| 81 | + queryTransformer: (subtree) => ({ |
| 82 | + kind: Kind.SELECTION_SET, |
| 83 | + selections: [ |
| 84 | + { |
| 85 | + kind: Kind.FIELD, |
| 86 | + name: { |
| 87 | + kind: Kind.NAME, |
| 88 | + value: 'items', |
| 89 | + }, |
| 90 | + selectionSet: subtree, |
| 91 | + }, |
| 92 | + ], |
| 93 | + }), |
| 94 | + resultTransformer: (r) => r?.items, |
| 95 | + }), |
| 96 | + ], |
| 97 | + }); |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }; |
| 102 | +} |
| 103 | + |
| 104 | +export default async function createAccountSchema(uri, wsUri) { |
| 105 | + console.log(` * account(${uri})`); |
| 106 | + const { executor, subscriber } = makeRemoteTransport(uri, wsUri, ""); |
| 107 | + const schema = wrapSchema({ |
| 108 | + schema: await introspectSchema(executor), |
| 109 | + executor, |
| 110 | + subscriber, |
| 111 | + }); |
| 112 | + return { |
| 113 | + schema, |
| 114 | + transforms: [], |
| 115 | + getConnectionTypes, |
| 116 | + getConnectionResolvers, |
| 117 | + }; |
| 118 | +} |
0 commit comments