New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

inversify-graphql

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

inversify-graphql

Builds dependency-inverted GraphQL schemas with InversifyJS

  • 1.2.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

inversify-graphql

build status npm version Dependencies img img Known Vulnerabilities

Build dependency-inverted GraphQL schemas with InversifyJS

Quickstart

See:

  • the sample app for a minimal sample
  • the complex types app to dive inito more complex inversified types

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);

Keywords

FAQs

Package last updated on 20 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc