inversify-graphql

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';
const srv = new agql.ApolloServer({
context: ,
schema: inversifySchema(myContainer, {
query: MyRootQuery,
}),
});
srv.applyMiddleware({ app, path: '/graphql'});
export class MyRootQuery extends InversifyObjectTypeBuilder<void, MyContext> {
@inject(MyDependency) dependency: MyDependency;
config(): InversifyObjectConfig<void, MyContext> {
return {
name: 'MyRoot',
fields: {
classicField: {
type: GraphQLString,
resolve: () => 'classic'
},
inversifiedField: {
type: MyType,
resolve: () => this.dependency.getWhateverEntity()
},
inversifiedListField: {
type: InversifyList(MyType),
resolve: () => this.dependency.getWhateverList()
}
}
}
}
}
export class MyType extends InversifyObjectTypeBuilder<MyEntity, MyContext> {
@inject(MyDependency) dependency: MyDependency;
config(): InversifyObjectConfig<MyEntity, MyContext> {
return {
}
}
}
Simple inline type definition
You can define sub-types "inline" (cleaner syntax)
export class MyType extends InversifyObjectTypeBuilder<MyEntity, MyContext> {
@inject(MyDependency) dependency: MyDependency;
config(): InversifyObjectConfig<MyEntity, MyContext> {
return {
myField: {
resolve: () => 42,
type: {
name: 'MyInlineType',
fields: {
subField: {
type: GraphQLString,
resolve: x => x + '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);
adminSchema.query.merge(PartialRoot1);
adminSchema.query.merge(PartialRoot2);
adminSchema.mutation.merge(PartialMutationRoot2);
adminSchema.get('MyTypeToExtend').merge(MyPartialMap);
adminSchema.concat(userSchma);