add dataloader to graphql modules / yoga template
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
"@envelop/graphql-modules": "^6.0.0",
|
"@envelop/graphql-modules": "^6.0.0",
|
||||||
"@graphql-tools/load-files": "^7.0.0",
|
"@graphql-tools/load-files": "^7.0.0",
|
||||||
"@prisma/client": "^5.15.0",
|
"@prisma/client": "^5.15.0",
|
||||||
|
"dataloader": "^2.2.2",
|
||||||
"graphql": "^16.8.1",
|
"graphql": "^16.8.1",
|
||||||
"graphql-modules": "^2.3.0",
|
"graphql-modules": "^2.3.0",
|
||||||
"graphql-yoga": "^5.3.1"
|
"graphql-yoga": "^5.3.1"
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
"@envelop/graphql-modules": "^6.0.0",
|
"@envelop/graphql-modules": "^6.0.0",
|
||||||
"@graphql-tools/load-files": "^7.0.0",
|
"@graphql-tools/load-files": "^7.0.0",
|
||||||
"@prisma/client": "^5.15.0",
|
"@prisma/client": "^5.15.0",
|
||||||
|
"dataloader": "^2.2.2",
|
||||||
"graphql": "^16.8.1",
|
"graphql": "^16.8.1",
|
||||||
"graphql-modules": "^2.3.0",
|
"graphql-modules": "^2.3.0",
|
||||||
"graphql-yoga": "^5.3.1"
|
"graphql-yoga": "^5.3.1"
|
||||||
|
|||||||
@@ -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 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 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 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 */
|
/** All built-in and custom scalars, mapped to their actual values */
|
||||||
export type Scalars = {
|
export type Scalars = {
|
||||||
ID: { input: string; output: string; }
|
ID: { input: string; output: string; }
|
||||||
@@ -29,12 +30,18 @@ export type MutationSetMessageArgs = {
|
|||||||
export type Query = {
|
export type Query = {
|
||||||
__typename?: 'Query';
|
__typename?: 'Query';
|
||||||
world: World;
|
world: World;
|
||||||
|
worlds: Array<World>;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export type QueryWorldsArgs = {
|
||||||
|
count: Scalars['Int']['input'];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type World = {
|
export type World = {
|
||||||
__typename?: 'World';
|
__typename?: 'World';
|
||||||
x: Scalars['Int']['output'];
|
x: Scalars['Float']['output'];
|
||||||
y: Scalars['Int']['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 */
|
/** Mapping between all available schema types and the resolvers types */
|
||||||
export type ResolversTypes = {
|
export type ResolversTypes = {
|
||||||
Boolean: ResolverTypeWrapper<Scalars['Boolean']['output']>;
|
Boolean: ResolverTypeWrapper<Scalars['Boolean']['output']>;
|
||||||
|
Float: ResolverTypeWrapper<Scalars['Float']['output']>;
|
||||||
Int: ResolverTypeWrapper<Scalars['Int']['output']>;
|
Int: ResolverTypeWrapper<Scalars['Int']['output']>;
|
||||||
Mutation: ResolverTypeWrapper<{}>;
|
Mutation: ResolverTypeWrapper<{}>;
|
||||||
Query: ResolverTypeWrapper<{}>;
|
Query: ResolverTypeWrapper<{}>;
|
||||||
@@ -119,6 +127,7 @@ export type ResolversTypes = {
|
|||||||
/** Mapping between all available schema types and the resolvers parents */
|
/** Mapping between all available schema types and the resolvers parents */
|
||||||
export type ResolversParentTypes = {
|
export type ResolversParentTypes = {
|
||||||
Boolean: Scalars['Boolean']['output'];
|
Boolean: Scalars['Boolean']['output'];
|
||||||
|
Float: Scalars['Float']['output'];
|
||||||
Int: Scalars['Int']['output'];
|
Int: Scalars['Int']['output'];
|
||||||
Mutation: {};
|
Mutation: {};
|
||||||
Query: {};
|
Query: {};
|
||||||
@@ -132,11 +141,12 @@ export type MutationResolvers<ContextType = any, ParentType extends ResolversPar
|
|||||||
|
|
||||||
export type QueryResolvers<ContextType = any, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = {
|
export type QueryResolvers<ContextType = any, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = {
|
||||||
world?: Resolver<ResolversTypes['World'], ParentType, ContextType>;
|
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']> = {
|
export type WorldResolvers<ContextType = any, ParentType extends ResolversParentTypes['World'] = ResolversParentTypes['World']> = {
|
||||||
x?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
|
x?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
|
||||||
y?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
|
y?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
|
||||||
__isTypeOf?: IsTypeOfResolverFn<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 { loadFilesSync } from "@graphql-tools/load-files";
|
||||||
import { resolvers } from "./resolvers";
|
import { resolvers } from "./resolvers";
|
||||||
import { join } from "node:path";
|
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({
|
export const exampleModule = createModule({
|
||||||
id: "example-module",
|
id: "example-module",
|
||||||
dirname: __dirname,
|
dirname: __dirname,
|
||||||
typeDefs: loadFilesSync(join(__dirname, "./typedefs/*.graphql")),
|
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,
|
resolvers,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import * as Types from "../../gql/graphql";
|
|||||||
import * as gm from "graphql-modules";
|
import * as gm from "graphql-modules";
|
||||||
export namespace ExampleModule {
|
export namespace ExampleModule {
|
||||||
interface DefinedFields {
|
interface DefinedFields {
|
||||||
Query: 'world';
|
Query: 'world' | 'worlds';
|
||||||
Mutation: 'setMessage';
|
Mutation: 'setMessage';
|
||||||
World: 'x' | 'y';
|
World: 'x' | 'y';
|
||||||
};
|
};
|
||||||
@@ -29,6 +29,7 @@ export namespace ExampleModule {
|
|||||||
Query?: {
|
Query?: {
|
||||||
'*'?: gm.Middleware[];
|
'*'?: gm.Middleware[];
|
||||||
world?: gm.Middleware[];
|
world?: gm.Middleware[];
|
||||||
|
worlds?: gm.Middleware[];
|
||||||
};
|
};
|
||||||
Mutation?: {
|
Mutation?: {
|
||||||
'*'?: gm.Middleware[];
|
'*'?: gm.Middleware[];
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { EXAMPLE_DATA_LOADER } from ".";
|
||||||
import { ExampleModule } from "./module-types";
|
import { ExampleModule } from "./module-types";
|
||||||
|
|
||||||
export const resolvers: ExampleModule.Resolvers = {
|
export const resolvers: ExampleModule.Resolvers = {
|
||||||
@@ -5,6 +6,11 @@ export const resolvers: ExampleModule.Resolvers = {
|
|||||||
world: async () => {
|
world: async () => {
|
||||||
return { x: 1, y: 2 };
|
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: {
|
Mutation: {
|
||||||
setMessage: async (_, { message }) => {
|
setMessage: async (_, { message }) => {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
type Query {
|
type Query {
|
||||||
world: World!
|
world: World!
|
||||||
|
worlds(count: Int!): [World!]!
|
||||||
}
|
}
|
||||||
|
|
||||||
type Mutation {
|
type Mutation {
|
||||||
@@ -7,6 +8,6 @@ type Mutation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type World {
|
type World {
|
||||||
x: Int!
|
x: Float!
|
||||||
y: Int!
|
y: Float!
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user