https://github.com/imqueue/type-graphql-dependency
Adoption of @imqueue/graphql-dependency for use with type-graphql
https://github.com/imqueue/type-graphql-dependency
Last synced: 3 months ago
JSON representation
Adoption of @imqueue/graphql-dependency for use with type-graphql
- Host: GitHub
- URL: https://github.com/imqueue/type-graphql-dependency
- Owner: imqueue
- License: isc
- Created: 2021-03-04T09:53:52.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-11-08T14:10:19.000Z (about 1 year ago)
- Last Synced: 2025-02-17T02:18:09.732Z (11 months ago)
- Language: TypeScript
- Size: 719 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @imqueue/type-graphql-dependency
[](https://github.com/imqueue/type-graphql-dependency)
[](https://rawgit.com/imqueue/type-graphql-dependency/master/LICENSE)
Adoption of @imqueue/graphql-dependency for use with type-graphql.
# Install
~~~bash
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:
~~~typescript
import {
Dependency,
DependencyFor,
schemaHooks,
} from '@imqueue/type-graphql-dependency';
import { Ctx, Info } from 'type-graphql';
import { fieldsMap } from 'graphql-fields-list';
@DependencyFor>({
// 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,
fields?: FieldsInput,
): Promise {
// ... 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[]> {
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
~~~
## License
This project is licensed under the GNU General Public License v3.0.
See the [LICENSE](LICENSE)