Skip to content

Commit

Permalink
Make batch delegate send key arrays to loadMany (#1865)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmac committed Aug 2, 2020
1 parent 66081f7 commit 7c55fbe
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/batch-delegate/src/batchDelegateToSchema.ts
Expand Up @@ -3,6 +3,12 @@ import { BatchDelegateOptions } from './types';
import { getLoader } from './getLoader';

export function batchDelegateToSchema(options: BatchDelegateOptions): any {
const key = options.key;
if (key == null) {
return null;
} else if (Array.isArray(key) && !key.length) {
return [];
}
const loader = getLoader(options);
return loader.load(options.key);
return Array.isArray(key) ? loader.loadMany(key) : loader.load(key);
}
110 changes: 109 additions & 1 deletion packages/batch-delegate/tests/basic.example.test.ts
Expand Up @@ -5,7 +5,7 @@ import { batchDelegateToSchema } from '@graphql-tools/batch-delegate';
import { stitchSchemas } from '@graphql-tools/stitch';

describe('batch delegation within basic stitching example', () => {
test('works', async () => {
test('works with single keys', async () => {
let numCalls = 0;

const chirpSchema = makeExecutableSchema({
Expand Down Expand Up @@ -91,4 +91,112 @@ describe('batch delegation within basic stitching example', () => {
expect(result.errors).toBeUndefined();
expect(result.data.trendingChirps[0].chirpedAtUser.email).not.toBe(null);
});

test('works with key arrays', async () => {
let numCalls = 0;

const postsSchema = makeExecutableSchema({
typeDefs: `
type Post {
id: ID!
title: String
}
type Query {
posts(ids: [ID]!): [Post]!
}
`,
resolvers: {
Query: {
posts: (obj, args) => {
numCalls += 1;
return args.ids.map(id => ({ id, title: `Post ${id}` }));
}
}
}
});

const usersSchema = makeExecutableSchema({
typeDefs: `
type User {
id: ID!
postIds: [ID]!
}
type Query {
users(ids: [ID!]!): [User]!
}
`,
resolvers: {
Query: {
users: (obj, args) => {
return args.ids.map(id => {
return { id, postIds: [Number(id)+1, Number(id)+2] };
});
}
}
}
});

const linkTypeDefs = `
extend type User {
posts: [Post]!
}
`;

const stitchedSchema = stitchSchemas({
subschemas: [postsSchema, usersSchema],
typeDefs: linkTypeDefs,
resolvers: {
User: {
posts: {
selectionSet: `{ postIds }`,
resolve(user, _args, context, info) {
return batchDelegateToSchema({
schema: postsSchema,
operation: 'query',
fieldName: 'posts',
key: user.postIds,
context,
info,
});
},
},
},
},
});

const query = `
query {
users(ids: [1, 7]) {
id
posts {
id
title
}
}
}
`;

const result = await graphql(stitchedSchema, query);
expect(numCalls).toEqual(1);
expect(result.data).toEqual({
users: [
{
id: '1',
posts: [
{ id: '2', title: 'Post 2' },
{ id: '3', title: 'Post 3' }
]
},
{
id: '7',
posts: [
{ id: '8', title: 'Post 8' },
{ id: '9', title: 'Post 9' }
]
}
]
});
});
});

0 comments on commit 7c55fbe

Please sign in to comment.