项目作者: tenry92

项目描述 :
Provide mongo access using annotated classes and the GraphQL query language.
高级语言: TypeScript
项目地址: git://github.com/tenry92/graphql-decorators-mongo.git
创建时间: 2018-03-18T12:14:15Z
项目社区:https://github.com/tenry92/graphql-decorators-mongo

开源协议:

下载


@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

  1. // import the libraries
  2. import {MongoClient} from 'mongodb';
  3. import {decorators} from '@tenry/graphql-decorators';
  4. import {Manager, name} from '@tenry/graphql-decorators-mongo';
  5. // define an entity
  6. @decorators.entity()
  7. // define a name; class name is used by default
  8. @name('user')
  9. class User {
  10. @decorators.field('ID')
  11. id: string;
  12. // primitive types like string or number is automatically detected,
  13. // if the emitDecoratorMetadata flag is enabled
  14. @decorators.field()
  15. name: string;
  16. @decorators.field('JSON')
  17. data: Object;
  18. // use this syntax, if the data type is an array of something
  19. @decorators.field({list: User})
  20. friends: User[];
  21. }
  22. // now set everything up
  23. const mongo = await MongoClient.connect('mongodb://localhost');
  24. const db = mongo.db('my_database');
  25. const manager = new Manager(db);
  26. // register all available entities
  27. manager.registerEntity(User);
  28. // get GraphQL schema
  29. const schema = manager.createSchema();
  30. // now do whatever you would do with a GraphQL schema
  31. graphql(schema, someAwesomeGraphqlQuery).then(response => {
  32. console.log(response);
  33. });
  34. // or (using express and express-graphql):
  35. const app = express();
  36. app.use('/graphql', graphqlHTTP({
  37. schema,
  38. }));
  39. app.listen(8080);

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

  1. input MongoFilterInput {
  2. field: String
  3. operator: String
  4. value: JSON
  5. }
  6. input MongoOrderInput {
  7. field: String
  8. order: String
  9. }
  10. type Mutation {
  11. addUser(user: UserInput): UserType
  12. updateUser(id: ID, user: UserInput): UserType
  13. removeUser(id: ID): UserType
  14. }
  15. type Query {
  16. users(filter: [MongoFilterInput], order: [MongoOrderInput], limit: Int, offset: Int): [UserType]
  17. }
  18. input UserInput {
  19. data: JSON
  20. friends: [UserInput]
  21. id: ID
  22. name: String
  23. }
  24. type UserType {
  25. data: JSON
  26. friends: [UserType]
  27. id: ID
  28. name: String
  29. }

Installation and Usage

Use npm to install the package:

  1. $ 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:

  1. import {Manager, name} from '@tenry/graphql-decorators-mongo';
  2. import {decorators} from '@tenry/graphql-decorators';
  3. import {MongoClient} from 'mongodb';
  4. const mongo = await MongoClient.connect('mongodb://localhost');
  5. const db = mongo.db('my_database');
  6. const manager = new Manager(db);
  7. // define entities here
  8. // register entities to the manager via manager.registerEntity(MyEntity); here
  9. // retrieve GraphQL schema
  10. const schema = manager.createSchema();

License

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