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.
33 lines
1.1 KiB
33 lines
1.1 KiB
const fetch = require("isomorphic-fetch")
|
|
const config = require("../config")
|
|
const compressQuery = require("graphql-query-compress")
|
|
|
|
/**
|
|
* Run a GraphQL query against the rpdata backend
|
|
*
|
|
* @param {string} query The query to run
|
|
* @param {{[x:string]: any}} variables
|
|
* @param {{operationName:string, token: string, permissions: string[]}} options
|
|
*/
|
|
function query(query, variables = {}, options = {}) {
|
|
return fetch(config.graphqlEndpoint, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Authorization": options.token ? `Bearer ${options.token}` : null,
|
|
"X-Permissions": options.permissions ? options.permissions.join(",") : null,
|
|
},
|
|
body: JSON.stringify({query: query.length > 256 ? compressQuery(query || "") : query, variables, operationName: options.operationName}),
|
|
credentials: "include",
|
|
}).then(res => {
|
|
return res.json()
|
|
}).then(json => {
|
|
if (json.errors != null && json.errors.length > 0) {
|
|
return Promise.reject(json.errors)
|
|
}
|
|
|
|
return json.data
|
|
})
|
|
}
|
|
|
|
module.exports = { query }
|