task.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const gql = require('graphql-tag')
  2. // Subs
  3. const { withFilter } = require('graphql-subscriptions')
  4. const channels = require('../channels')
  5. // Connectors
  6. const tasks = require('../connectors/tasks')
  7. const plugins = require('../connectors/plugins')
  8. const projects = require('../connectors/projects')
  9. exports.types = gql`
  10. extend type Query {
  11. tasks: [Task]
  12. task (id: ID!): Task
  13. }
  14. extend type Mutation {
  15. taskRun (id: ID!): Task
  16. taskStop (id: ID!): Task
  17. taskLogsClear (id: ID!): Task
  18. taskOpen (id: ID!): Boolean
  19. taskSaveParameters (id: ID!): [Prompt]
  20. taskRestoreParameters (id: ID!): [Prompt]
  21. }
  22. extend type Subscription {
  23. taskChanged: Task
  24. taskLogAdded (id: ID!): TaskLog
  25. }
  26. type Task implements DescribedEntity {
  27. id: ID!
  28. status: TaskStatus!
  29. command: String!
  30. name: String
  31. description: String
  32. link: String
  33. icon: String
  34. logs: [TaskLog]
  35. prompts: [Prompt]
  36. views: [TaskView]
  37. defaultView: String
  38. plugin: Plugin
  39. project: Project
  40. }
  41. enum TaskStatus {
  42. idle
  43. running
  44. done
  45. error
  46. terminated
  47. }
  48. type TaskLog {
  49. taskId: ID!
  50. type: TaskLogType!
  51. text: String
  52. }
  53. enum TaskLogType {
  54. stdout
  55. stderr
  56. }
  57. type TaskView {
  58. id: ID!
  59. label: String!
  60. component: String!
  61. icon: String
  62. }
  63. `
  64. exports.resolvers = {
  65. Task: {
  66. prompts: (task, args, context) => tasks.getPrompts(task.id, context),
  67. plugin: (task, args, context) => plugins.findOne({ id: task.pluginId, file: task.path }, context),
  68. project: (task, args, context) => projects.findByPath(task.path, context)
  69. },
  70. Query: {
  71. tasks: (root, args, context) => tasks.list(undefined, context),
  72. task: (root, { id }, context) => tasks.findOne(id, context)
  73. },
  74. Mutation: {
  75. taskRun: (root, { id }, context) => tasks.run(id, context),
  76. taskStop: (root, { id }, context) => tasks.stop(id, context),
  77. taskLogsClear: (root, { id }, context) => tasks.clearLogs(id, context),
  78. taskOpen: (root, { id }, context) => tasks.open(id, context),
  79. taskSaveParameters: (root, { id }, context) => tasks.saveParameters({ id }, context),
  80. taskRestoreParameters: (root, { id }, context) => tasks.restoreParameters({ id }, context)
  81. },
  82. Subscription: {
  83. taskChanged: {
  84. subscribe: (parent, args, { pubsub }) => pubsub.asyncIterator(channels.TASK_CHANGED)
  85. },
  86. taskLogAdded: {
  87. subscribe: withFilter(
  88. (parent, args, { pubsub }) => pubsub.asyncIterator(channels.TASK_LOG_ADDED),
  89. (payload, vars) => payload.taskLogAdded.taskId === vars.id
  90. )
  91. }
  92. }
  93. }