Skip to content

Commit 875dbe2

Browse files
committed
add batchDelegate to badges
1 parent 40b505c commit 875dbe2

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

src/remotes/account.js

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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';
1+
import { wrapSchema, introspectSchema } from "@graphql-tools/wrap";
2+
import makeRemoteTransport from "../remoteTransport";
3+
import { delegateToSchema } from "@graphql-tools/delegate";
4+
import { batchDelegateToSchema } from "@graphql-tools/batch-delegate";
5+
import { TransformQuery } from "@graphql-tools/wrap";
6+
import { Kind } from "graphql";
7+
import { AddFieldToRequestTransform } from "../gql-utils";
68

79
function getConnectionTypes(prefix) {
810
return `
@@ -20,37 +22,38 @@ function getConnectionResolvers(prefix, schemas) {
2022
return {
2123
[`${prefix}Badge`]: {
2224
details: {
23-
selectionSet: '{ id }',
25+
selectionSet: "{ id }",
2426
async resolve(parent, args, context, info) {
25-
return delegateToSchema({
27+
return batchDelegateToSchema({
2628
schema: schemas.cms,
27-
operation: 'query',
28-
fieldName: 'badgeCollection',
29-
args: {
30-
where: {
31-
id: parent.id,
32-
},
33-
limit: 1,
34-
},
29+
operation: "query",
30+
fieldName: "badgeCollection",
31+
key: parent.id,
32+
argsFromKeys: (ids) => ({
33+
where: { OR: [...ids?.map((id) => ({ id }))] },
34+
}),
3535
context,
3636
info,
37+
valuesFromResults: (results, keys) =>
38+
keys?.map((key) => results.find((result) => result?.id === key)),
3739
transforms: [
40+
new AddFieldToRequestTransform(schemas.cms, "Badge", "id"),
3841
new TransformQuery({
39-
path: ['badgeCollection'],
42+
path: ["badgeCollection"],
4043
queryTransformer: (subtree) => ({
4144
kind: Kind.SELECTION_SET,
4245
selections: [
4346
{
4447
kind: Kind.FIELD,
4548
name: {
4649
kind: Kind.NAME,
47-
value: 'items',
50+
value: "items",
4851
},
4952
selectionSet: subtree,
5053
},
5154
],
5255
}),
53-
resultTransformer: (r) => r?.items[0],
56+
resultTransformer: (result) => result?.items || [],
5457
}),
5558
],
5659
});
@@ -59,33 +62,36 @@ function getConnectionResolvers(prefix, schemas) {
5962
},
6063
[`${prefix}User`]: {
6164
sites: {
62-
selectionSet: '{ roles }',
65+
selectionSet: "{ roles }",
6366
async resolve(parent, args, context, info) {
6467
return delegateToSchema({
6568
schema: schemas.cms,
66-
operation: 'query',
67-
fieldName: 'siteCollection',
69+
operation: "query",
70+
fieldName: "siteCollection",
6871
args: {
6972
where: {
70-
OR: [...parent.roles.map((role) => {
71-
if (role.name == "Staff") return { type: "Volunteer" };
72-
return { type: role.name };
73-
}), { type: "Student" }]
74-
}
73+
OR: [
74+
...parent.roles.map((role) => {
75+
if (role.name == "Staff") return { type: "Volunteer" };
76+
return { type: role.name };
77+
}),
78+
{ type: "Student" },
79+
],
80+
},
7581
},
7682
context,
7783
info,
7884
transforms: [
7985
new TransformQuery({
80-
path: ['siteCollection'],
86+
path: ["siteCollection"],
8187
queryTransformer: (subtree) => ({
8288
kind: Kind.SELECTION_SET,
8389
selections: [
8490
{
8591
kind: Kind.FIELD,
8692
name: {
8793
kind: Kind.NAME,
88-
value: 'items',
94+
value: "items",
8995
},
9096
selectionSet: subtree,
9197
},

0 commit comments

Comments
 (0)