Skip to content

tenry92/graphql-decorators-mongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@tenry/graphql-decorators-mongo

This package provides useful TypeScript decorators for creating class-based entities, persisted to a mongo database and queryable and manipulable using the GraphQL query language.

It is based on the @tenry/graphql-decorators package.

Example

// import the libraries
import {MongoClient} from 'mongodb';
import {decorators} from '@tenry/graphql-decorators';
import {Manager, name} from '@tenry/graphql-decorators-mongo';

// define an entity
@decorators.entity()
// define a name; class name is used by default
@name('user')
class User {
  @decorators.field('ID')
  id: string;

  // primitive types like string or number is automatically detected,
  // if the emitDecoratorMetadata flag is enabled
  @decorators.field()
  name: string;

  @decorators.field('JSON')
  data: Object;

  // use this syntax, if the data type is an array of something
  @decorators.field({list: User})
  friends: User[];
}

// now set everything up
const mongo = await MongoClient.connect('mongodb://localhost');
const db = mongo.db('my_database');
const manager = new Manager(db);

// register all available entities
manager.registerEntity(User);

// get GraphQL schema
const schema = manager.createSchema();

// now do whatever you would do with a GraphQL schema
graphql(schema, someAwesomeGraphqlQuery).then(response => {
  console.log(response);
});

// or (using express and express-graphql):
const app = express();

app.use('/graphql', graphqlHTTP({
  schema,
}));

app.listen(8080);

Using this example the library would create the following GraphQL schema:

input MongoFilterInput {
  field: String
  operator: String
  value: JSON
}

input MongoOrderInput {
  field: String
  order: String
}

type Mutation {
  addUser(user: UserInput): UserType
  updateUser(id: ID, user: UserInput): UserType
  removeUser(id: ID): UserType
}

type Query {
  users(filter: [MongoFilterInput], order: [MongoOrderInput], limit: Int, offset: Int): [UserType]
}

input UserInput {
  data: JSON
  friends: [UserInput]
  id: ID
  name: String
}

type UserType {
  data: JSON
  friends: [UserType]
  id: ID
  name: String
}

Installation and Usage

Use npm to install the package:

$ npm install graphql graphql-type-json mongodb @tenry/graphql-decorators @tenry/graphql-decorators-mongo

Now import the Manager, the name decorator along with the decorators from @tenry/graphql-decorators:

import {Manager, name} from '@tenry/graphql-decorators-mongo';
import {decorators} from '@tenry/graphql-decorators';

import {MongoClient} from 'mongodb';

const mongo = await MongoClient.connect('mongodb://localhost');
const db = mongo.db('my_database');

const manager = new Manager(db);

// define entities here
// register entities to the manager via manager.registerEntity(MyEntity); here

// retrieve GraphQL schema
const schema = manager.createSchema();

License

@tenry/graphql-decorators-mongo is licensed under the MIT License.

About

Provide mongo access using annotated classes and the GraphQL query language.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published