|
| 1 | +# @imqueue/type-graphql-dependency |
| 2 | + |
| 3 | +[](https://travis-ci.com/imqueue/type-graphql-dependency) |
| 4 | +[](https://rawgit.com/imqueue/type-graphql-dependency/master/LICENSE) |
| 5 | + |
| 6 | +Adoption of @imqueue/graphql-dependency for use with type-graphql. |
| 7 | + |
| 8 | +# Install |
| 9 | + |
| 10 | +~~~bash |
| 11 | +npm i --save @imqueue/type-graphql-dependency |
| 12 | +~~~ |
| 13 | + |
| 14 | +# Usage |
| 15 | + |
| 16 | +This module allows describing cross-service dependencies and fetch user |
| 17 | +requested data in an optimal manner in a schemas defined using type-graphql |
| 18 | +library. |
| 19 | + |
| 20 | +Example: |
| 21 | + |
| 22 | +~~~typescript |
| 23 | +import { |
| 24 | + Dependency, |
| 25 | + DependencyFor, |
| 26 | + schemaHooks, |
| 27 | +} from '@imqueue/type-graphql-dependency'; |
| 28 | +import { Ctx, Info } from 'type-graphql'; |
| 29 | +import { fieldsMap } from 'graphql-fields-list'; |
| 30 | + |
| 31 | +@DependencyFor<Partial<Consumer>>({ |
| 32 | + // this defines dependency relations for Consumer object. |
| 33 | + // refers to: @imqueue/graphql-dependency:Dependency.require() |
| 34 | + require: [ |
| 35 | + [() => ApiKey, [ |
| 36 | + { as: 'apiKeys', filter: { 'consumerId': 'id' } }], |
| 37 | + ], |
| 38 | + ], |
| 39 | + // this defines initializer for Consumer, all dependencies will wait |
| 40 | + // for initializer to finish before load |
| 41 | + // refers to: @imqueue/graphql-dependency:Dependency.defineInitializer() |
| 42 | + async init( |
| 43 | + context: Context, |
| 44 | + result: Partial<Consumer>, |
| 45 | + fields?: FieldsInput, |
| 46 | + ): Promise<DataInitializerResult> { |
| 47 | + // ... do initializer stuff here ... |
| 48 | + return result; |
| 49 | + }, |
| 50 | + // this defines loader for Consumer entity, which should be used by |
| 51 | + // other entities, which depend on Consumer |
| 52 | + // refers to: @imqueue/graphql-dependency:Dependency.defineLoader() |
| 53 | + async load( |
| 54 | + context: Context, |
| 55 | + filter: ConsumerListInput, |
| 56 | + fields?: FieldsInput, |
| 57 | + ): Promise<Partial<Consumer>[]> { |
| 58 | + const { data } = await context.consumer.listConsumer(filter, fields); |
| 59 | + return toConsumers(data); |
| 60 | + }, |
| 61 | +}) |
| 62 | +@ObjectType() |
| 63 | +export class Consumer { |
| 64 | + // ... Consumer fields definitions goes here ... |
| 65 | +} |
| 66 | + |
| 67 | +// now within a resolver: |
| 68 | +async function consumerResolver( |
| 69 | + @Ctx() context: Context, |
| 70 | + @Info() info: GraphQLResolveInfo, |
| 71 | +) { |
| 72 | + // load consumer data from some service or database |
| 73 | + const data = await loadConsumers(/* ... */); |
| 74 | + // fill dependent data into loaded data |
| 75 | + await Dependency(Consumer).load(data, context, fieldsMap(info)); |
| 76 | + return data; |
| 77 | +} |
| 78 | + |
| 79 | +// Now where schema is created using type-graphql: |
| 80 | +const schema = await buildSchema({ |
| 81 | + // your schema options due to type-graphql docs |
| 82 | +}); |
| 83 | +// and |
| 84 | +(schemaHooks || []).forEach(handle => handle && handle(schema)); |
| 85 | +// so now all deps initialized within schema |
| 86 | +~~~ |
| 87 | + |
| 88 | +# License |
| 89 | + |
| 90 | +[ISC](https://github.com/imqueue/type-graphql-dependency/blob/master/LICENSE) |
0 commit comments