@imqueue/type-graphql-dependency
v1.5.0
Published
Adoption of @imqueue/graphql-dependency for use with type-graphql
Downloads
112
Readme
@imqueue/type-graphql-dependency
Adoption of @imqueue/graphql-dependency for use with type-graphql.
Install
npm i --save @imqueue/type-graphql-dependency
Usage
This 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()
async init(
context: Context,
result: Partial<Consumer>,
fields?: FieldsInput,
): Promise<DataInitializerResult> {
// ... do initializer stuff here ...
return result;
},
// this defines loader for Consumer entity, which should be used by
// other entities, which depend on Consumer
// refers to: @imqueue/graphql-dependency:Dependency.defineLoader()
async load(
context: Context,
filter: ConsumerListInput,
fields?: FieldsInput,
): Promise<Partial<Consumer>[]> {
const { data } = await context.consumer.listConsumer(filter, fields);
return toConsumers(data);
},
})
@ObjectType()
export class Consumer {
// ... Consumer fields definitions goes here ...
}
// now within a resolver:
async function consumerResolver(
@Ctx() context: Context,
@Info() info: GraphQLResolveInfo,
) {
// load consumer data from some service or database
const data = await loadConsumers(/* ... */);
// fill dependent data into loaded data
await Dependency(Consumer).load(data, context, fieldsMap(info));
return data;
}
// Now where schema is created using type-graphql:
const schema = await buildSchema({
// your schema options due to type-graphql docs
});
// and
(schemaHooks || []).forEach(handle => handle && handle(schema));
// so now all deps initialized within schema