|
| 1 | +import { |
| 2 | + GraphQLID, |
| 3 | + GraphQLList, |
| 4 | + GraphQLNonNull, |
| 5 | + GraphQLObjectType, |
| 6 | + GraphQLSchema, |
| 7 | +} from '../../../src/type/index.ts'; |
| 8 | + |
| 9 | +import type { |
| 10 | + BenchContext, |
| 11 | + Comment, |
| 12 | + Dataset, |
| 13 | + Post, |
| 14 | + User, |
| 15 | +} from '../shared/fixture.ts'; |
| 16 | +import { firstArg, take } from '../shared/fixture.ts'; |
| 17 | +import type { ListFieldConfig } from '../shared/graphql-js.ts'; |
| 18 | +import { firstArgConfig } from '../shared/graphql-js.ts'; |
| 19 | +import { RequestBatchLoader } from '../shared/request-batch-loader.ts'; |
| 20 | + |
| 21 | +interface PostRequest { |
| 22 | + readonly userId: string; |
| 23 | + readonly first: number; |
| 24 | +} |
| 25 | + |
| 26 | +interface CommentRequest { |
| 27 | + readonly postId: string; |
| 28 | + readonly first: number; |
| 29 | +} |
| 30 | + |
| 31 | +interface LoaderContext { |
| 32 | + readonly postsByUser: RequestBatchLoader<PostRequest, ReadonlyArray<Post>>; |
| 33 | + readonly commentsByPost: RequestBatchLoader< |
| 34 | + CommentRequest, |
| 35 | + ReadonlyArray<Comment> |
| 36 | + >; |
| 37 | +} |
| 38 | + |
| 39 | +interface DataloaderBenchContext extends BenchContext { |
| 40 | + readonly loaders: LoaderContext; |
| 41 | +} |
| 42 | + |
| 43 | +export function createDataloaderSchema(): GraphQLSchema { |
| 44 | + const CommentType = new GraphQLObjectType<Comment, DataloaderBenchContext>({ |
| 45 | + name: 'DataloaderBenchComment', |
| 46 | + fields: { |
| 47 | + id: { type: new GraphQLNonNull(GraphQLID) }, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + const PostType = new GraphQLObjectType<Post, DataloaderBenchContext>({ |
| 52 | + name: 'DataloaderBenchPost', |
| 53 | + fields: () => ({ |
| 54 | + id: { type: new GraphQLNonNull(GraphQLID) }, |
| 55 | + comments: createCommentsField(CommentType), |
| 56 | + }), |
| 57 | + }); |
| 58 | + |
| 59 | + const UserType = new GraphQLObjectType<User, DataloaderBenchContext>({ |
| 60 | + name: 'DataloaderBenchUser', |
| 61 | + fields: () => ({ |
| 62 | + id: { type: new GraphQLNonNull(GraphQLID) }, |
| 63 | + posts: createPostsField(PostType), |
| 64 | + }), |
| 65 | + }); |
| 66 | + |
| 67 | + const QueryType = new GraphQLObjectType<Dataset, DataloaderBenchContext>({ |
| 68 | + name: 'DataloaderBenchQuery', |
| 69 | + fields: { |
| 70 | + users: { |
| 71 | + type: new GraphQLList(new GraphQLNonNull(UserType)), |
| 72 | + args: firstArgConfig(), |
| 73 | + resolve: (_source, args, context) => |
| 74 | + Promise.resolve(take(context.data.users, firstArg(args))), |
| 75 | + }, |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + return new GraphQLSchema({ query: QueryType }); |
| 80 | +} |
| 81 | + |
| 82 | +export function createDataloaderContext(data: Dataset): DataloaderBenchContext { |
| 83 | + return { |
| 84 | + data, |
| 85 | + loaders: { |
| 86 | + postsByUser: new RequestBatchLoader((requests) => |
| 87 | + Promise.resolve( |
| 88 | + requests.map((request) => |
| 89 | + take(data.postsByUserId.get(request.userId) ?? [], request.first), |
| 90 | + ), |
| 91 | + ), |
| 92 | + ), |
| 93 | + commentsByPost: new RequestBatchLoader((requests) => |
| 94 | + Promise.resolve( |
| 95 | + requests.map((request) => |
| 96 | + take( |
| 97 | + data.commentsByPostId.get(request.postId) ?? [], |
| 98 | + request.first, |
| 99 | + ), |
| 100 | + ), |
| 101 | + ), |
| 102 | + ), |
| 103 | + }, |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +function createPostsField( |
| 108 | + PostType: GraphQLObjectType<Post, DataloaderBenchContext>, |
| 109 | +): ListFieldConfig<User, DataloaderBenchContext> { |
| 110 | + return { |
| 111 | + type: new GraphQLList(new GraphQLNonNull(PostType)), |
| 112 | + args: firstArgConfig(), |
| 113 | + resolve: (source, args, context) => |
| 114 | + context.loaders.postsByUser.load({ |
| 115 | + userId: source.id, |
| 116 | + first: firstArg(args), |
| 117 | + }), |
| 118 | + }; |
| 119 | +} |
| 120 | + |
| 121 | +function createCommentsField( |
| 122 | + CommentType: GraphQLObjectType<Comment, DataloaderBenchContext>, |
| 123 | +): ListFieldConfig<Post, DataloaderBenchContext> { |
| 124 | + return { |
| 125 | + type: new GraphQLList(new GraphQLNonNull(CommentType)), |
| 126 | + args: firstArgConfig(), |
| 127 | + resolve: (source, args, context) => |
| 128 | + context.loaders.commentsByPost.load({ |
| 129 | + postId: source.id, |
| 130 | + first: firstArg(args), |
| 131 | + }), |
| 132 | + }; |
| 133 | +} |
0 commit comments