graphql-decorators-mongo
Provide mongo access using annotated classes and the GraphQL query language.
Last updated 2 years ago by tenry .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install graphql-decorators-mongo 
SYNC missed versions from official npm registry.

@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
@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: UserType})
  friends: UserType[];
}

// 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.

Current Tags

  • 0.1.1                                ...           latest (2 years ago)

2 Versions

  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 2
This Month 2
Last Day 0
Last Week 0
Last Month 3
Dependencies (3)
Dev Dependencies (12)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |