Sorry, no results found for "".
In REST, HTTP verbs determine the operation performed. In GraphQL, you'll provide a JSON-encoded body even if you're performing a query operation, so the HTTP verb is always POST
.
fetch( 'https://graphql.datocms.com/', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': `Bearer ${process.env.DATOCMS_READONLY_TOKEN}`, }, body: JSON.stringify({ query: }), }).then(res => res.json()).then((res) => { console.log(res.data)}).catch((error) => { console.log(error);});
We also offer a lightweight, TypeScript-ready package that offers various helpers around the native Fetch API to perform GraphQL requests towards DatoCMS Content Delivery API:
import { executeQuery } from '@datocms/cda-client';
const result = await executeQuery('{ allPosts { title } }', { token: process.env.DATOCMS_READONLY_TOKEN,});
console.log(result);
You can learn more about this package on it's README file.
This blog post ranks the best JavaScript GraphQL client libraries, helping you choose the right tool based on your project’s specific needs and ensuring efficient and optimized GraphQL data fetching.