GraphQL API
Last updated
Was this helpful?
Was this helpful?
{
query: "...", // your GraphQL query as a string
variables: { ... } // any applicable variables to include in the query (optional)
}// if successful
{
"data": { ... }
}
// if error
{
"errors": [ ... ],
"data": null
}// third-party fetch package for making HTTP calls
import fetch from "node-fetch";
const response = await fetch("https://api.pitchly.com/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json", // must be included in GraphQL requests
"Authorization": "Bearer " + accessToken
},
body: JSON.stringify({
query: `
query workspace($id: ID!) {
workspace(id: $id) {
tables {
id
name
}
}
}
`,
variables: {
id: workspaceId
}
})
});
// GraphQL will always return a 200 OK status code,
// even when there is an error. So first check that
// the HTTP status code is 200, and then check that
// "data" exists in the response, indicating success.
if (!response.ok) {
throw new Error("HTTP request returned error status code.");
}
// decode response json
const data = await response.json();
if (!data.data) {
throw new Error("GraphQL query failed.");
}
console.log(data);{
"data": {
"workspace": {
"tables": [
{
"id": "wksEtbAYd6bcTKigHGm4|tbloJnfkHi85WscsjTvG",
"name": "Companies"
},
{
"id": "wksEtbAYd6bcTKigHGm4|tblR59H2hbufDMpp5BTp",
"name": "Matters"
},
{
"id": "wksEtbAYd6bcTKigHGm4|tblqafbaJW9ZqeiZbJ9Q",
"name": "Employees"
}
]
}
}
}{
"errors": [
{
"message": "You must be authenticated to access this resource. Please provide a valid Bearer Token in the Authorization header.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"workspace"
],
"extensions": {
"code": "UNAUTHENTICATED"
}
}
],
"data": null
}