graph.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. function getGql (endpoint){
  2. let headers = {
  3. 'Content-Type': 'application/json;charset=utf-8',
  4. 'Accept': 'application/json'
  5. }
  6. if ("authToken" in localStorage) {
  7. headers.Authorization = "Bearer " + localStorage.authToken
  8. }
  9. return async function gql(query, variables={}) {
  10. let result = await fetch(endpoint, {
  11. method: 'POST',
  12. headers,
  13. body: JSON.stringify({
  14. query,
  15. variables
  16. })
  17. }).then(res => res.json())
  18. if (("errors" in result) && !("data" in result)) {
  19. throw new Error(JSON.stringify(result.errors))
  20. }
  21. result = Object.values(result.data)[0]
  22. return result
  23. }
  24. }
  25. gql = getGql('http://shop-roles.node.ed.asmer.org.ua/graphql')
  26. let queryFindRoots = `query findRoots($q:String) {
  27. CategoryFind(query: $q){
  28. _id name parent{
  29. _id name
  30. }
  31. }
  32. }`
  33. let variablesFindRoots = {
  34. q:JSON.stringify([{parent:null}])
  35. }
  36. gql(queryFindRoots , variablesFindRoots ).then(console.log)
  37. const actionFindRoots = () => actionPromise("findRoots",gql(queryFindRoots, variablesFindRoots))
  38. let queryCatFindOne = `query categoryFindOne ($q : String) {
  39. CategoryFindOne(query: $q){
  40. _id name parent{
  41. _id name
  42. }
  43. goods{
  44. _id name description price
  45. images{
  46. url
  47. }
  48. }
  49. subCategories{
  50. _id name
  51. }
  52. }
  53. }
  54. `
  55. let variablesCatFindOne = {
  56. q:JSON.stringify([{ _id }])
  57. }
  58. gql(queryCatFindOne,variablesCatFindOne).then(console.log)
  59. const actionCatFindOne = (_id) => actionPromise("CatFindOne",gql(queryCatFindOne, variablesCatFindOne))
  60. let queryGoodFindOne = `query goodfindOne ($q : String) {
  61. GoodFindOne(query: $q){
  62. _id name price description
  63. images{
  64. url
  65. }
  66. }
  67. }`
  68. let variablesGoodFindOne = {
  69. q:JSON.stringify([{ _id }])
  70. }
  71. gql(queryGoodFindOne,variablesGoodFindOne).then(console.log)
  72. const actionGoodFindOne = (_id) => actionPromise("GoodFindOne",gql(queryGoodFindOne, variablesGoodFindOne))
  73. let mutationRegistr = `mutation registration($login:String, $password: String) {
  74. UserUpsert(user : {login:$login, password: $password}){
  75. _id createdAt login
  76. }
  77. }`
  78. let variablesRegistr = {
  79. "login" : login , "password" : password
  80. }
  81. gql( mutationRegistr,variablesRegistr).then(console.log)
  82. const actionRegistr = (login,password) => actionPromise("Registr",gql( mutationRegistr, variablesRegistr))
  83. let queryLogin = `query login($login:String,$password:String){
  84. login(login:$login,password:$password)
  85. }`
  86. let variablesLogin = {
  87. login, password
  88. }
  89. gql(queryLogin,variablesLogin).then(console.log)
  90. const actionLogin = (login,password) => actionPromise("Login",gql(queryLogin, variablesLogin))
  91. let queryHistoryOrder = `query historyOrder($order : String){
  92. OrderFind(query: $order){
  93. _id createdAt total owner {
  94. _id
  95. createdAt
  96. login
  97. nick
  98. } orderGoods{
  99. _id count good {
  100. _id
  101. createdAt
  102. name
  103. description
  104. price
  105. } total
  106. }
  107. }
  108. }
  109. `
  110. let variablesHistoryOrder = {
  111. q:JSON.stringify([{order : [{}]}])
  112. }
  113. gql(queryHistoryOrder,variablesHistoryOrder).then(console.log)
  114. const actionHistoryOrder = () => actionPromise("HistoryOrder",gql(queryHistoryOrder, variablesHistoryOrder))
  115. //
  116. const NewOrder = (orderGoods) =>
  117. actionPromise(
  118. "NewOrder",
  119. gql(
  120. `mutation NewOrder($order: OrderInput) {
  121. OrderUpsert(order: $order) {
  122. _id
  123. orderGoods {
  124. _id
  125. price
  126. count
  127. total
  128. good {
  129. name
  130. _id
  131. price
  132. images {
  133. url
  134. }
  135. }
  136. }
  137. }
  138. }`,
  139. { order: { orderGoods } }
  140. )
  141. )