项目作者: mrphu3074

项目描述 :
Create graphql server easier
高级语言: TypeScript
项目地址: git://github.com/mrphu3074/graphql-server-decorators.git
创建时间: 2017-04-11T13:03:53Z
项目社区:https://github.com/mrphu3074/graphql-server-decorators

开源协议:Apache License 2.0

下载


Circle CI

Build Status


graphql-server-decorators

Decorators that help creating graphql server easier
These decorators don’t create types or input.
It just help create domain’s mutations or queries

@controller

Add prototypes getQueries and getMutations into a class

@query(specs)

  • specs: Object
    • name: String. Optional. Default is generate from class name and method name. Ex: User_find, User_findOne
    • description: String. Optional
    • type: GraphqlType. Required
    • args: query arguments. Optional

@mutation(specs)

  • specs: Object
    • name: String. Optional. Default is generate from class name and method name. Ex: User_create, User_delete
    • description: String. Optional
    • type: GraphqlType. Required
    • args: mutation arguments. Optional
  1. @controller
  2. class Product {
  3. @query({
  4. type: Products
  5. })
  6. find(root, args, context) {
  7. const collection = context.collections.Product;
  8. return collection.find();
  9. }
  10. @query({
  11. name: 'getProduct'
  12. type: Product,
  13. args: {
  14. id: {
  15. type: new GraphQLNonNull(GraphQLString)
  16. }
  17. }
  18. })
  19. findOne(root, args, context) {
  20. const collection = context.collections.Product;
  21. return collection.findOne({id: args.id});
  22. }
  23. // mutations
  24. @mutation({
  25. type: Product,
  26. args: CreateProductArgs
  27. })
  28. create(root, args, context) {
  29. const collection = context.collections.Product;
  30. return collection.save(args.data);
  31. }
  32. }
  33. // Add to schema
  34. const product = new Product();
  35. let schemaDef = {
  36. query: new GraphQLObjectType({
  37. name: 'Query',
  38. fields: {
  39. ...product.getQueries()
  40. }
  41. }),
  42. mutation: new GraphQLObjectType({
  43. name: 'Mutation',
  44. fields: {
  45. ...product.getMutations()
  46. }
  47. }),
  48. };
  49. const schema = new GraphQLSchema(schemaDef);