add dataloader to graphql modules / yoga template
This commit is contained in:
@@ -7,6 +7,7 @@ export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?:
|
||||
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
|
||||
export type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };
|
||||
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
|
||||
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
|
||||
/** All built-in and custom scalars, mapped to their actual values */
|
||||
export type Scalars = {
|
||||
ID: { input: string; output: string; }
|
||||
@@ -29,12 +30,18 @@ export type MutationSetMessageArgs = {
|
||||
export type Query = {
|
||||
__typename?: 'Query';
|
||||
world: World;
|
||||
worlds: Array<World>;
|
||||
};
|
||||
|
||||
|
||||
export type QueryWorldsArgs = {
|
||||
count: Scalars['Int']['input'];
|
||||
};
|
||||
|
||||
export type World = {
|
||||
__typename?: 'World';
|
||||
x: Scalars['Int']['output'];
|
||||
y: Scalars['Int']['output'];
|
||||
x: Scalars['Float']['output'];
|
||||
y: Scalars['Float']['output'];
|
||||
};
|
||||
|
||||
|
||||
@@ -109,6 +116,7 @@ export type DirectiveResolverFn<TResult = {}, TParent = {}, TContext = {}, TArgs
|
||||
/** Mapping between all available schema types and the resolvers types */
|
||||
export type ResolversTypes = {
|
||||
Boolean: ResolverTypeWrapper<Scalars['Boolean']['output']>;
|
||||
Float: ResolverTypeWrapper<Scalars['Float']['output']>;
|
||||
Int: ResolverTypeWrapper<Scalars['Int']['output']>;
|
||||
Mutation: ResolverTypeWrapper<{}>;
|
||||
Query: ResolverTypeWrapper<{}>;
|
||||
@@ -119,6 +127,7 @@ export type ResolversTypes = {
|
||||
/** Mapping between all available schema types and the resolvers parents */
|
||||
export type ResolversParentTypes = {
|
||||
Boolean: Scalars['Boolean']['output'];
|
||||
Float: Scalars['Float']['output'];
|
||||
Int: Scalars['Int']['output'];
|
||||
Mutation: {};
|
||||
Query: {};
|
||||
@@ -132,11 +141,12 @@ export type MutationResolvers<ContextType = any, ParentType extends ResolversPar
|
||||
|
||||
export type QueryResolvers<ContextType = any, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = {
|
||||
world?: Resolver<ResolversTypes['World'], ParentType, ContextType>;
|
||||
worlds?: Resolver<Array<ResolversTypes['World']>, ParentType, ContextType, RequireFields<QueryWorldsArgs, 'count'>>;
|
||||
};
|
||||
|
||||
export type WorldResolvers<ContextType = any, ParentType extends ResolversParentTypes['World'] = ResolversParentTypes['World']> = {
|
||||
x?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
|
||||
y?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
|
||||
x?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
|
||||
y?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
|
||||
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,11 +1,27 @@
|
||||
import { createModule } from "graphql-modules";
|
||||
import { InjectionToken, Scope, createModule } from "graphql-modules";
|
||||
import { loadFilesSync } from "@graphql-tools/load-files";
|
||||
import { resolvers } from "./resolvers";
|
||||
import { join } from "node:path";
|
||||
import DataLoader from "dataloader";
|
||||
import { ExampleModule } from "./module-types";
|
||||
|
||||
export const EXAMPLE_DATA_LOADER = new InjectionToken("EXAMPLE_DATA_LOADER");
|
||||
|
||||
export const exampleModule = createModule({
|
||||
id: "example-module",
|
||||
dirname: __dirname,
|
||||
typeDefs: loadFilesSync(join(__dirname, "./typedefs/*.graphql")),
|
||||
providers: [
|
||||
{
|
||||
provide: EXAMPLE_DATA_LOADER,
|
||||
scope: Scope.Operation,
|
||||
useFactory: () =>
|
||||
new DataLoader<string, ExampleModule.World>(async (keys) => {
|
||||
return keys.map(() => {
|
||||
return { x: Math.random(), y: Math.random() };
|
||||
});
|
||||
}),
|
||||
},
|
||||
],
|
||||
resolvers,
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as Types from "../../gql/graphql";
|
||||
import * as gm from "graphql-modules";
|
||||
export namespace ExampleModule {
|
||||
interface DefinedFields {
|
||||
Query: 'world';
|
||||
Query: 'world' | 'worlds';
|
||||
Mutation: 'setMessage';
|
||||
World: 'x' | 'y';
|
||||
};
|
||||
@@ -29,6 +29,7 @@ export namespace ExampleModule {
|
||||
Query?: {
|
||||
'*'?: gm.Middleware[];
|
||||
world?: gm.Middleware[];
|
||||
worlds?: gm.Middleware[];
|
||||
};
|
||||
Mutation?: {
|
||||
'*'?: gm.Middleware[];
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { EXAMPLE_DATA_LOADER } from ".";
|
||||
import { ExampleModule } from "./module-types";
|
||||
|
||||
export const resolvers: ExampleModule.Resolvers = {
|
||||
@@ -5,6 +6,11 @@ export const resolvers: ExampleModule.Resolvers = {
|
||||
world: async () => {
|
||||
return { x: 1, y: 2 };
|
||||
},
|
||||
worlds: async (_, { count }, ctx) => {
|
||||
const keys = Array.from({ length: count }, (_, i) => i.toString());
|
||||
|
||||
return ctx.injector.get(EXAMPLE_DATA_LOADER).loadMany(keys);
|
||||
},
|
||||
},
|
||||
Mutation: {
|
||||
setMessage: async (_, { message }) => {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
type Query {
|
||||
world: World!
|
||||
worlds(count: Int!): [World!]!
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
@@ -7,6 +8,6 @@ type Mutation {
|
||||
}
|
||||
|
||||
type World {
|
||||
x: Int!
|
||||
y: Int!
|
||||
x: Float!
|
||||
y: Float!
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user