-
-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(batchDelegate): add tests, docs (#1856)
complete code reorganization
- Loading branch information
Showing
8 changed files
with
211 additions
and
57 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import { BatchDelegateOptions, DataLoaderCache } from './types'; | ||
import { BatchDelegateOptions } from './types'; | ||
|
||
import { getLoader } from './getLoader'; | ||
|
||
export function batchDelegateToSchema<K = any, V = any, C = K>(options: BatchDelegateOptions): any { | ||
let cache: DataLoaderCache<K, V, C>; | ||
|
||
const loader = getLoader(cache, options); | ||
export function batchDelegateToSchema(options: BatchDelegateOptions): any { | ||
const loader = getLoader(options); | ||
return loader.load(options.key); | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { graphql } from 'graphql'; | ||
|
||
import { makeExecutableSchema } from '@graphql-tools/schema'; | ||
import { batchDelegateToSchema } from '@graphql-tools/batch-delegate'; | ||
import { stitchSchemas } from '@graphql-tools/stitch'; | ||
|
||
describe('batch delegation within basic stitching example', () => { | ||
test('works', async () => { | ||
let numCalls = 0; | ||
|
||
const chirpSchema = makeExecutableSchema({ | ||
typeDefs: ` | ||
type Chirp { | ||
chirpedAtUserId: ID! | ||
} | ||
type Query { | ||
trendingChirps: [Chirp] | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
trendingChirps: () => [{ chirpedAtUserId: 1 }, { chirpedAtUserId: 2 }] | ||
} | ||
} | ||
}); | ||
|
||
// Mocked author schema | ||
const authorSchema = makeExecutableSchema({ | ||
typeDefs: ` | ||
type User { | ||
email: String | ||
} | ||
type Query { | ||
usersByIds(ids: [ID!]): [User] | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
usersByIds: (_root, args) => { | ||
numCalls++; | ||
return args.ids.map((id: string) => ({ email: `${id}@test.com`})); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
const linkTypeDefs = ` | ||
extend type Chirp { | ||
chirpedAtUser: User | ||
} | ||
`; | ||
|
||
const stitchedSchema = stitchSchemas({ | ||
subschemas: [chirpSchema, authorSchema], | ||
typeDefs: linkTypeDefs, | ||
resolvers: { | ||
Chirp: { | ||
chirpedAtUser: { | ||
selectionSet: `{ chirpedAtUserId }`, | ||
resolve(chirp, _args, context, info) { | ||
return batchDelegateToSchema({ | ||
schema: authorSchema, | ||
operation: 'query', | ||
fieldName: 'usersByIds', | ||
key: chirp.chirpedAtUserId, | ||
mapKeysFn: (ids) => ({ ids }), | ||
context, | ||
info, | ||
}); | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const query = ` | ||
query { | ||
trendingChirps { | ||
chirpedAtUser { | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const result = await graphql(stitchedSchema, query); | ||
|
||
expect(numCalls).toEqual(1); | ||
expect(result.errors).toBeUndefined(); | ||
expect(result.data.trendingChirps[0].chirpedAtUser.email).not.toBe(null); | ||
}); | ||
}); |
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
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
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