Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[F] Graphql requests to Craft use application/graphql Content Type #407

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions lib/fetch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { GraphQLClient } from "graphql-request";

export async function queryAPI(query, token, previewToken, variables = {}) {
let API_URL = process.env.NEXT_PUBLIC_API_URL;
// Check to see if the environment variable DOCKER_GATEWAY_IP is populated, if so
Expand All @@ -14,21 +12,32 @@ export async function queryAPI(query, token, previewToken, variables = {}) {
}

const isPreview = previewToken && previewToken !== "";
const url = isPreview ? `${API_URL}?token=${previewToken}` : API_URL;
const client = new GraphQLClient(url);
const url = variables
? `${API_URL}?variables=${JSON.stringify(variables)}`
: API_URL;
const urlWithToken = isPreview ? `${url}&token=${previewToken}` : url;
const headers = !token
? {}
? {
"Content-Type": "application/graphql",
}
: {
"Content-Type": "application/graphql",
authorization: `JWT ${token}`,
};

return client
.request(query, variables, headers)
.then((data) => data)
const results = await fetch(urlWithToken, {
method: "POST",
headers,
body: query,
})
.then((res) => res.json())
.then((json) => json?.data)
.catch((error) => {
process.exitCode = 1;
console.warn("Error in fetch.js :");
console.warn(error);
return error.response;
});

return results;
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"url": "https://github.com/lsst-epo/rubin-obs-client"
},
"scripts": {
"dev": "npm-run-all --parallel dev:*",
"dev:next": "next dev",
"dev": "next dev",
"static": "npm-run-all static:*",
"static:build": "next build --debug",
"static:export": "next export",
Expand Down