The frontend/UI server, written in JS using the MarkoJS library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.2 KiB

6 years ago
  1. const fetch = require("isomorphic-fetch")
  2. const config = require("../config")
  3. const compressQuery = require("graphql-query-compress")
  4. /**
  5. * Run a GraphQL query against the rpdata backend
  6. *
  7. * @param {string} query The query to run
  8. * @param {{[x:string]: any}} variables
  9. * @param {{operationName:string, token: string, permissions: string[]}} options
  10. */
  11. function query(query, variables = {}, options = {}) {
  12. return fetch(config.graphqlEndpoint, {
  13. method: "POST",
  14. headers: {
  15. "Content-Type": "application/json",
  16. "Authorization": options.token ? `Bearer ${options.token}` : null,
  17. "X-Permissions": options.permissions ? options.permissions.join(",") : null,
  18. },
  19. body: JSON.stringify({query: query.length > 256 ? compressQuery(query || "") : query, variables, operationName: options.operationName}),
  20. credentials: "include",
  21. }).then(res => {
  22. if (!res.ok) {
  23. return Promise.reject([{
  24. message: `HTTP: ${res.statusText}`,
  25. location: null,
  26. }])
  27. }
  28. return res.json()
  29. }).then(json => {
  30. if (json.errors != null && json.errors.length > 0) {
  31. return Promise.reject(json.errors)
  32. }
  33. return json.data
  34. })
  35. }
  36. module.exports = { query }