|
| 1 | +import { execute } from 'graphql/execution/execute.js'; |
| 2 | +import { parse } from 'graphql/language/parser.js'; |
| 3 | +import { |
| 4 | + GraphQLID, |
| 5 | + GraphQLInt, |
| 6 | + GraphQLList, |
| 7 | + GraphQLObjectType, |
| 8 | + GraphQLSchema, |
| 9 | +} from 'graphql/type/index.js'; |
| 10 | + |
| 11 | +const WidgetType = new GraphQLObjectType({ |
| 12 | + name: 'Widget', |
| 13 | + fields: () => ({ |
| 14 | + id: { type: GraphQLID }, |
| 15 | + widget: { |
| 16 | + type: WidgetType, |
| 17 | + resolve: async (source) => { |
| 18 | + await Promise.resolve(); |
| 19 | + return source.widget; |
| 20 | + }, |
| 21 | + experimentalBatchResolve: async (sources) => { |
| 22 | + await Promise.resolve(); |
| 23 | + return sources.map((source) => source.widget); |
| 24 | + }, |
| 25 | + }, |
| 26 | + widgets: { type: new GraphQLList(WidgetType) }, |
| 27 | + }), |
| 28 | +}); |
| 29 | + |
| 30 | +const QueryType = new GraphQLObjectType({ |
| 31 | + name: 'Query', |
| 32 | + fields: { |
| 33 | + widgets: { |
| 34 | + type: new GraphQLList(WidgetType), |
| 35 | + args: { first: { type: GraphQLInt } }, |
| 36 | + resolve: (source, args) => source.widgets.slice(0, args.first), |
| 37 | + }, |
| 38 | + }, |
| 39 | +}); |
| 40 | + |
| 41 | +const schema = new GraphQLSchema({ query: QueryType }); |
| 42 | + |
| 43 | +const rootValue = { |
| 44 | + widgets: Array.from({ length: 10000 }, () => ({ |
| 45 | + id: 'gid://owner/Widget/1', |
| 46 | + widget: { id: 'gid://owner/Widget/1' }, |
| 47 | + })), |
| 48 | +}; |
| 49 | +const document = parse(`{ widgets(first: 10000) { id widget { id } } }`); |
| 50 | + |
| 51 | +export const benchmark = { |
| 52 | + name: 'Execute Async Widget Field With Batch Resolver', |
| 53 | + measure: () => |
| 54 | + execute({ |
| 55 | + schema, |
| 56 | + document, |
| 57 | + rootValue, |
| 58 | + enableBatchResolvers: true, |
| 59 | + }), |
| 60 | +}; |
0 commit comments