Adoption of @imqueue/graphql-dependency for use with type-graphql.
Using an AI assistant? Point it at imqueue.org/llms.txt for a machine-readable index of the docs. Current version, licence and Node floor for every package: imqueue.org/status.json.
npm i --save @imqueue/type-graphql-dependencyThis module allows describing cross-service dependencies and fetch user requested data in an optimal manner in a schemas defined using type-graphql library.
Example:
import{Dependency,DependencyFor,schemaHooks,}from'@imqueue/type-graphql-dependency';import{Ctx,Info}from'type-graphql';import{fieldsMap}from'graphql-fields-list';
@DependencyFor<Partial<Consumer>>({// this defines dependency relations for Consumer object.// refers to: @imqueue/graphql-dependency:Dependency.require()require: [[()=>ApiKey,[{as: 'apiKeys',filter: {'consumerId': 'id'}}],],],// this defines initializer for Consumer, all dependencies will wait// for initializer to finish before load// refers to: @imqueue/graphql-dependency:Dependency.defineInitializer()asyncinit(context: Context,result: Partial<Consumer>,fields?: FieldsInput,): Promise<DataInitializerResult>{// ... do initializer stuff here ...returnresult;},// this defines loader for Consumer entity, which should be used by// other entities, which depend on Consumer// refers to: @imqueue/graphql-dependency:Dependency.defineLoader()asyncload(context: Context,filter: ConsumerListInput,fields?: FieldsInput,): Promise<Partial<Consumer>[]>{const{ data }=awaitcontext.consumer.listConsumer(filter,fields);returntoConsumers(data);},})
@ObjectType()exportclassConsumer{// ... Consumer fields definitions goes here ...}// now within a resolver:asyncfunctionconsumerResolver(
@Ctx()context: Context,
@Info()info: GraphQLResolveInfo,){// load consumer data from some service or databaseconstdata=awaitloadConsumers(/* ... */);// fill dependent data into loaded dataawaitDependency(Consumer).load(data,context,fieldsMap(info));returndata;}// Now where schema is created using type-graphql:constschema=awaitbuildSchema({// your schema options due to type-graphql docs});// and(schemaHooks||[]).forEach(handle=>handle&&handle(schema));// so now all deps initialized within schemaThis project is licensed under the GNU General Public License v3.0. See the LICENSE