todo.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. local types = require 'graphql.types'
  2. local schema = require 'graphql.schema'
  3. local clients = {
  4. [1] = { id = 1, name = "Microsoft" },
  5. [2] = { id = 2, name = "Oracle'" },
  6. [3] = { id = 3, name = "Apple" }
  7. }
  8. local projects = {
  9. [1] = { id = 1, name = "Windows 7", client_id = 1 },
  10. [2] = { id = 2, name = "Windows 10", client_id = 1 },
  11. [3] = { id = 3, name = "IOS", client_id = 3 },
  12. [4] = { id = 4, name = "OSX", client_id = 3 }
  13. }
  14. local tasks = {
  15. [1] = { id = 1, name = "Design w7", project_id = 1 },
  16. [2] = { id = 2, name = "Code w7", project_id = 1 },
  17. [3] = { id = 3, name = "Unassigned Task", project_id = 1 },
  18. [4] = { id = 4, name = "Design w10", project_id = 2 },
  19. [5] = { id = 5, name = "Code w10", project_id = 2 },
  20. [6] = { id = 6, name = "Design IOS", project_id = 3 },
  21. [7] = { id = 7, name = "Code IOS", project_id = 3 },
  22. [8] = { id = 8, name = "Design OSX", project_id = 4 },
  23. [9] = { id = 9, name = "Code OSX", project_id = 4 }
  24. }
  25. local getObjectById = function(store, id)
  26. return store[id]
  27. end
  28. local getObjectsByKey = function(store, key, value)
  29. local results = {}
  30. for k,v in pairs(store) do
  31. if v[key] == value then
  32. table.insert(results, v)
  33. end
  34. end
  35. return results
  36. end
  37. local getClientById = function (id) return getObjectById(clients, id) end
  38. local getProjectById = function (id) return getObjectById(projects, id) end
  39. local getTaskById = function (id) return getObjectById(clients, id) end
  40. local getProjectByClientId = function (id) return getObjectsByKey(projects, "client_id") end
  41. local getTasksByProjectId = function (id) return getObjectsByKey(tasks, "project_id") end
  42. local project, client, task
  43. client = types.object {
  44. name = 'Client',
  45. description = 'Client',
  46. fields = function()
  47. return {
  48. id = { kind = types.nonNull(types.int), description = 'The id of the client.'},
  49. name = { kind = types.string, description = 'The name of the client.'},
  50. projects = {
  51. kind = types.list(project),
  52. description = 'projects',
  53. resolve = function(client, arguments)
  54. return getProjectByClientId(client.id)
  55. end
  56. }
  57. }
  58. end
  59. }
  60. project = types.object {
  61. name = 'Project',
  62. description = 'Project',
  63. fields = function()
  64. return {
  65. id = { kind = types.nonNull(types.int), description = 'The id of the project.'},
  66. name = { kind = types.string, description = 'The name of the project.'},
  67. client_id = { kind = types.nonNull(types.int), description = 'client id'},
  68. client = {
  69. kind = client,
  70. description = 'client',
  71. resolve = function(project)
  72. return getClientById(project.client_id)
  73. end
  74. },
  75. tasks = {
  76. kind = types.list(task),
  77. description = 'tasks',
  78. resolve = function(project, arguments)
  79. return getTasksByProjectId(project.id)
  80. end
  81. }
  82. }
  83. end
  84. }
  85. task = types.object {
  86. name = 'Task',
  87. description = 'Task',
  88. fields = function()
  89. return {
  90. id = { kind = types.nonNull(types.int), description = 'The id of the task.'},
  91. name = { kind = types.string, description = 'The name of the task.'},
  92. project_id = { kind = types.nonNull(types.int), description = 'project id'},
  93. project = {
  94. kind = project,
  95. description = 'project',
  96. resolve = function(task)
  97. return getProjectById(task.project_id)
  98. end
  99. }
  100. }
  101. end
  102. }
  103. -- Create a schema
  104. return schema.create {
  105. query = types.object {
  106. name = 'Query',
  107. fields = {
  108. client = {
  109. kind = client,
  110. arguments = {
  111. id = {
  112. kind = types.nonNull(types.int),
  113. description = 'id of the client'
  114. }
  115. },
  116. resolve = function(rootValue, arguments)
  117. return getClientById(arguments.id)
  118. end
  119. },
  120. project = {
  121. kind = project,
  122. arguments = {
  123. id = {
  124. kind = types.nonNull(types.int),
  125. description = 'id of the project'
  126. }
  127. },
  128. resolve = function(rootValue, arguments)
  129. return getProjectById(arguments.id)
  130. end
  131. },
  132. task = {
  133. kind = task,
  134. arguments = {
  135. id = {
  136. kind = types.nonNull(types.int),
  137. description = 'id of the task'
  138. }
  139. },
  140. resolve = function(rootValue, arguments)
  141. return getTaskById(arguments.id)
  142. end
  143. }
  144. }
  145. }
  146. }