Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. const graphql = require('graphql');
  2. const axios = require('axios');
  3. const {
  4. GraphQLObjectType,
  5. GraphQLString,
  6. GraphQLInt,
  7. GraphQLSchema,
  8. GraphQLList,
  9. GraphQLNonNull,
  10. GraphQLBoolean
  11. } = graphql;
  12.  
  13. const CompanyType = new GraphQLObjectType({
  14. name: 'Company',
  15. fields: () => ({
  16. id: { type: GraphQLString },
  17. name: { type: GraphQLString },
  18. description: { type: GraphQLString },
  19. users: {
  20. type: new GraphQLList(UserType),
  21. resolve(parentValue, args) {
  22. return axios.get(`http://localhost:3000/companies/${parentValue.id}/users`)
  23. .then(res => res.data)
  24. }
  25. }
  26. })
  27. });
  28.  
  29. const UserType = new GraphQLObjectType({
  30. name: 'User',
  31. fields: () => ({
  32. id: { type: GraphQLString },
  33. firstName: { type: GraphQLString },
  34. age: { type: GraphQLInt },
  35. company: {
  36. type: CompanyType,
  37. resolve(parentValue, args) {
  38. return axios.get(`http://localhost:3000/companies/${parentValue.companyId}`)
  39. .then(res => res.data);
  40. }
  41. }
  42. })
  43. });
  44.  
  45. const RepoType = new GraphQLObjectType({
  46. name: 'Repo',
  47. fields: () => ({
  48. total_count: { type: GraphQLInt },
  49. incomplete_results: { type: GraphQLBoolean },
  50. // items: {type: [RepoItemType]}
  51. items: {
  52. type: new GraphQLList(RepoItemType),
  53. resolve(parentValue, args) {
  54. //console.log(parentValue.items)
  55. return parentValue.items
  56. }
  57. },
  58.  
  59. })
  60. });
  61.  
  62. const RepoItemType = new GraphQLObjectType({
  63. name: 'Item',
  64. fields: () => ({
  65. id: { type: GraphQLInt },
  66. html_url: { type: GraphQLString },
  67. description: { type: GraphQLString },
  68. owner: {
  69. type: OwnerType,
  70. resolve(parentValue, args) {
  71. //console.log(parentValue.items)
  72. return parentValue.owner
  73. }
  74. },
  75. stats: {
  76. type: new GraphQLList(StateType),
  77. resolve(parentValue, args) {
  78. return axios.get(`https://api.github.com/repos/${parentValue.owner.login}/${parentValue.name}/stats/contributors`)
  79. .then(res => res.data)
  80.  
  81. }}
  82. })
  83. });
  84.  
  85. const StateType = new GraphQLObjectType({
  86. name: 'State',
  87. fields: () => ({
  88. stat: { type: new GraphQLList(StateObj) }
  89. })
  90. });
  91. const StateObj = new GraphQLObjectType({
  92. name: 'StateObj',
  93. fields: () => ({
  94. total: {type: GraphQLInt},
  95. weeks: {type: new GraphQLList(WeekArray)}
  96. })
  97. });
  98.  
  99. const WeekArray = new GraphQLObjectType({
  100. name: 'WeekArray',
  101. fields: () => ({
  102. w: {type: GraphQLInt}
  103. })
  104. });
  105.  
  106.  
  107. const OwnerType = new GraphQLObjectType({
  108. name: 'Owner',
  109. fields: () => ({
  110. id: { type: GraphQLInt },
  111. login: { type: GraphQLString },
  112. })
  113. });
  114.  
  115.  
  116. const RootQuery = new GraphQLObjectType({
  117. name: 'RootQueryType',
  118. fields: {
  119. repo: {
  120. type: RepoType,
  121. args: { q: { type: GraphQLString }, sort: {type: GraphQLString}, order: {type: GraphQLString} },
  122. resolve(parentValue, args) {
  123. return axios.get(`https://api.github.com/search/repositories?q=${args.q}&sort=${args.sort}&order=${args.order}`)
  124. .then(resp => resp.data);
  125. }
  126. },
  127. // company: {
  128. // type: CompanyType,
  129. // args: { id: { type: GraphQLString } },
  130. // resolve(parentValue, args) {
  131. // return axios.get(`http://localhost:3000/companies/${args.id}`)
  132. // .then(resp => resp.data);
  133. // }
  134. // }
  135. }
  136. });
  137.  
  138. const mutation = new GraphQLObjectType({
  139. name: 'Mutation',
  140. fields: {
  141. addUser: {
  142. type: UserType,
  143. args: {
  144. firstName: { type: new GraphQLNonNull(GraphQLString) },
  145. age: { type: GraphQLInt },
  146. companyId: { type: GraphQLString }
  147. },
  148. resolve(parentValue, { firstName, age, companyId }) {
  149. return axios.post('http://localhost:3000/users', { firstName, age, companyId })
  150. .then(res => res.data);
  151. }
  152. },
  153. deleteUser: {
  154. type: UserType,
  155. args: {
  156. id: { type: new GraphQLNonNull(GraphQLString) }
  157. },
  158. resolve(parentValue, { id }) {
  159. return axios.delete(`http://localhost:3000/users/${id}`)
  160. .then(res => res.data);
  161. }
  162. }
  163. }
  164. });
  165.  
  166. module.exports = new GraphQLSchema({
  167. mutation,
  168. query: RootQuery
  169. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement