Guest User

Untitled

a guest
Jan 7th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. ## GraphQL gateway example
  2.  
  3. ```js
  4. module.exports = {
  5. name: "boards",
  6. settings: {},
  7.  
  8. graphql: {
  9. // Define Graphql Query
  10. query: `
  11. boards(limit: Int, offset: Int, sort: String): [Board]
  12. board(id: String!): Board
  13. `,
  14.  
  15. // Define new Types
  16. type: `
  17. type Board {
  18. id: String!,
  19. title: String!,
  20. slug: String,
  21. description: String,
  22. owner: User,
  23. createdAt: Float
  24. }
  25. `,
  26. // Define GraphQL Mutation
  27. mutation: `
  28. createBoard(title: String!, description: String): Board
  29. `,
  30.  
  31. // Define resolvers
  32. resolvers: {
  33. Query: {
  34. boards: {
  35. // Call the local "find" action with params
  36. action: "find",
  37. params: {
  38. populate: ["owner"]
  39. }
  40. },
  41.  
  42. board: {
  43. // Call the local "get" action with params
  44. action: "get",
  45. params: {
  46. populate: ["owner"]
  47. }
  48. }
  49. },
  50. Mutation: {
  51. // Call the local "create" action
  52. createBoard: "create"
  53. }
  54. },
  55. },
  56.  
  57. actions: {
  58. //...
  59. }
  60. }
  61. ```
  62.  
  63. ```js
  64. module.exports = {
  65. name: "accounts",
  66. settings: {},
  67.  
  68. graphql: {
  69. // Define new Type
  70. type: `
  71. type User {
  72. id: String!
  73. username: String!
  74. firstName: String!
  75. lastName: String!
  76. email: String
  77. avatar: String
  78. status: Int
  79.  
  80. boards(limit: Int, offset: Int, sort: String): [Board]
  81. boardCount: Int
  82. }
  83. `,
  84.  
  85. // Define resolvers
  86. resolvers: {
  87. User: {
  88. boards: {
  89. // Call a remote "v1.boards.find" action
  90. action: "v1.boards.find",
  91. // Get `id` value from `root` and put it into `ctx.params.query.owner`
  92. rootParams: {
  93. "id": "query.owner"
  94. },
  95. params: {
  96. populate: ["owner"]
  97. }
  98. },
  99. boardCount: {
  100. // Call a remote "v1.boards.count" action
  101. action: "v1.boards.count",
  102. // Get `id` value from `root` and put it into `ctx.params.query.owner`
  103. rootParams: {
  104. "id": "query.owner"
  105. }
  106. }
  107. }
  108. }
  109. },
  110.  
  111. actions: {
  112. find: {
  113. graphql: {
  114. // Create a GraphQL Query for this action
  115. query: "users(limit: Int, offset: Int, sort: String): [User]"
  116. },
  117. handler(ctx) {
  118. // ...
  119. }
  120. },
  121.  
  122. create: {
  123. graphql: {
  124. // Create a GraphQL Mutation for this action
  125. mutation: "createUser(email: String!, password: String!): [User]"
  126. },
  127. handler(ctx) {
  128. // ...
  129. }
  130. }
  131. }
  132. }
  133. ```
Add Comment
Please, Sign In to add comment