Giter VIP home page Giter VIP logo

type-graphql-dependency's Introduction

@imqueue/type-graphql-dependency

Build Status License

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

License

ISC

type-graphql-dependency's People

Contributors

mikhus avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.