inversify-graphql
TypeScript icon, indicating that this package has built-in type declarations

1.2.3 • Public • Published

inversify-graphql

build status npm version Dependencies img img Known Vulnerabilities

Build dependency-inverted GraphQL schemas with InversifyJS

Quickstart

See:

Usage

Install the package

npm i inversify reflect-metadata graphql inversify-graphql --save

Example using express and apollo-server

import { inversifySchema } from 'inversify-graphql';
/* ... initialize express & inversify container */
const srv = new agql.ApolloServer({
    // build inversified schema
    context: /* whateverContext */,
    schema: inversifySchema(myContainer, {
      query: MyRootQuery,
  }),
});
srv.applyMiddleware({ app, path: '/graphql'});
// === MyRootQuery definition ==
export class MyRootQuery extends InversifyObjectTypeBuilder<void, MyContext> {

    // Injected dependency, usable in our resolve() function
    @inject(MyDependency) dependency: MyDependency;

    config(): InversifyObjectConfig<void, MyContext> {
        return {
            name: 'MyRoot',
            // nb: "fields" supports 'partial roots', enabling you to describe one object in multiple separate builders
            //  fields: [PartialRoot1, PartialRoot2],
            fields: {
              // compatible with classic GraphQL objects/types
              classicField: {
                  type: GraphQLString,
                  resolve: () => 'classic'
              },
              // use your "type builders" to refernece inversified field types
              inversifiedField: {
                type: MyType,
                resolve: () => this.dependency.getWhateverEntity()
              },
              // use InversifiedList to build a GraphQLList of an inversified type.
              inversifiedListField: {
                type: InversifyList(MyType),
                resolve: () => this.dependency.getWhateverList()
              }
            }
        }
    }
}
// === MyType definition ==
export class MyType extends InversifyObjectTypeBuilder<MyEntity, MyContext> {

    // Injected dependency, usable in our resolve() function
    @inject(MyDependency) dependency: MyDependency;

    config(): InversifyObjectConfig<MyEntity, MyContext> {
      return {
        //  ... sub fields, using source, context AND inversified dependencies (injectable in this class)
      }
    }
}

Simple inline type definition

You can define sub-types "inline" (cleaner syntax)

export class MyType extends InversifyObjectTypeBuilder<MyEntity, MyContext> {

    // Injected dependency, usable in our resolve() function
    @inject(MyDependency) dependency: MyDependency;

    config(): InversifyObjectConfig<MyEntity, MyContext> {
      return {
        myField: {
          // resolver
          resolve: () => 42,
          // inline type definition
          type: {
            name: 'MyInlineType',
            fields: {
              subField: {
                type: GraphQLString,
                resolve: x => x + 'is the answer', // will output "42 is the answer"
              }
            }
          }
        }
      }
    }
}

Handy shortcuts

If like me, you're annoyed with those very long names like new GraphQLList(new GraphQLNonNull(GraphQLString))), you can use the inversify-graphql/shortcuts helpers to turn them into GList(NN(GString)).

nb: shortcuts are compatible with inversified types ;)

Modular schema definiton

Some type definitions can tend to be bloated as your app grows. In these cases, you might want to split the definition of types in several files or parts of your application.

This is the purpose of "extensible schema", which builds on top of inversify-graphql to enable you to define a more modular schema.

const adminSchema = extensibleSchema('RootName', container);

// those two partial roots will be merged in the schema root
adminSchema.query.merge(PartialRoot1);
adminSchema.query.merge(PartialRoot2);
adminSchema.mutation.merge(PartialMutationRoot2);

// extend a type
// the type 'MyTypeToExtend' will augmented with all the fields defined in MyPartialMap
// nb: this will work even if 'MyTypeToExtend' has not been defined yet
adminSchema.get('MyTypeToExtend').merge(MyPartialMap);

// you can concatenate two schemas:
// this will augment the root of "adminSchema" with everything defined in "userSchma"
adminSchema.concat(userSchma);

Package Sidebar

Install

npm i inversify-graphql

Weekly Downloads

54

Version

1.2.3

License

MIT

Unpacked Size

80.7 kB

Total Files

47

Last publish

Collaborators

  • oguimbal