Repository navigation
GraphQL Support
#2768
Replies: 1 comment
|
Yes. For a GraphQL endpoint that accepts JSON over HTTP, Undici can send the operation as a normal POST request. Put the GraphQL document in the JSON For example, in an ES module: import { fetch } from "undici";
const response = await fetch("https://your-api.example/graphql", {
method: "POST",
headers: {
"content-type": "application/json",
accept: "application/graphql-response+json, application/json",
// Add the authentication headers required by your API here.
},
body: JSON.stringify({
query: "query { __typename }",
}),
});
// Consume the response body even when the HTTP status indicates failure.
const text = await response.text();
if (!response.ok) {
throw new Error(`GraphQL HTTP ${response.status}: ${text}`);
}
const result = JSON.parse(text);
if (result.errors?.length) {
console.error("GraphQL errors:", result.errors);
}
console.log("GraphQL data:", result.data);Replace the URL and the small Check References: Undici fetch API, GraphQL POST request format, and GraphQL response errors. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi, Can we make a call by using GraphQL query as body using this node package? If yes, please suggest the way to make a post call using GraphQL.
Thank you
All reactions