-
Hi there, short and simple beginner question: Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
There are two generic ways, the GraphQL api or the Content API. Pick whichever one works best for your use case. |
Beta Was this translation helpful? Give feedback.
-
Use whatever Javascript solution you're comfortable with for performing AJAX requests, and point it at either the GraphQL endpoint or a REST API endpoint. https://sabe.io/classes/javascript/ajax-fetch |
Beta Was this translation helpful? Give feedback.
-
Thanks for the direction. I could figure it out … it was quote a challenge to get everything up and running, but now it works. If anybody is looking for the same … heres how i've done it ;) 1. Activate GraphQL
2. Install HelpersThere are many ways to retrieve GraphQL messages. Have a look at @jasonvarga post. There are some good links to start. 3. What to do within JS?
import { GraphQLClient, gql } from 'graphql-request'
const query = gql`{
entries(collection: "handler") {
data {
... on Entry_Post_Post {
title
}
}
}
}`
const client = new GraphQLClient('http://your-domain.test/graphql');
(async () => {
try {
// send the query from above and wait for answer
const data = await client.request(query);
// maka a string out of data
var receivedData = JSON.stringify(data, undefined, 2);
// create an JS object from string.
var obj = JSON.parse(receivedData);
// access data like this
console.log( obj.entries.data[0].title );
}
catch (e) {
console.log("No GraphQL received");
}
})(); I don't know if this is the best way to do this, but it worked for me. But please feel free to comment improvements. |
Beta Was this translation helpful? Give feedback.
Thanks for the direction. I could figure it out … it was quote a challenge to get everything up and running, but now it works. If anybody is looking for the same … heres how i've done it ;)
1. Activate GraphQL
.env
file and set a variable ofSTATAMIC_GRAPHQL_ENABLED=true
config/statamic/graphql.php
and under GraphQL set'enabled' => true,
2. Install Helpers
There are many ways to retrieve GraphQL messages. Have a look at @jasonvarga post. There are some good links to start.
I used this
npm add graphql-request graphql
See documentation.3. What to do within JS?