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.

32 lines
1.1 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. return res.json()
  23. }).then(json => {
  24. if (json.errors != null && json.errors.length > 0) {
  25. return Promise.reject(json.errors)
  26. }
  27. return json.data
  28. })
  29. }
  30. module.exports = { query }