Calling GraphQL
GraphQL operates over HTTP, you can use any language, client library, or tool to fetch GraphQL data so long as you format the HTTP request properly.
Command line tool and library for transferring data with URLs
curl -X POST -H "Content-Type: application/json" \
--data '{ "query": "{ myProfile { id program { name }}}" }' \
-H "Authorization: Bearer {TOKEN}" \
https://production.suggestic.com/graphql
Fetch module will enable you to use
fetch
in your Node code in a cross-browser compliant fashion. The Fetch API is part of the Web platform API defined by the standards bodies WHATWG and W3C.require("isomorphic-fetch");
fetch("https://production.suggestic.com/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer {TOKEN}",
},
body: JSON.stringify({
query: `
{
myProfile {
id
program {
name
}
}
}`,
}),
})
.then((res) => res.json())
.then((res) => console.log(res.data));
Apollo Client is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. Use it to fetch, cache, and modify application data, all while automatically updating your UI.
import React from "react";
import { gql, useQuery } from "@apollo/client";
const MY_PROFILE = gql`
{
myProfile {
id
program {
name
}
}
}
`;
export const Profile = () => {
const { loading, error, data } = useQuery(MY_PROFILE);
if (loading) return "Loading...";
if (error) return `Error! ${error.message}`;
return <div>{data.myProfile.program.name}</div>;
};
<?php
$token = 'TOKEN';
// https://docs.suggestic.com/graphql/query/mutations/user-profile/new-user
$query = <<<'GQL'
mutation createUser($name: String!, $email: String!) {
createUser(name: $name, email: $email) {
success
message
user {
id
}
}
}
GQL;
$variables = array(
'name' => 'Ernesto',
'email' => '[email protected]'
);
$json = json_encode(['query' => $query, 'variables' => $variables]);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://production.suggestic.com/graphql',
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array(
'Authorization: Token ' . $token,
'Content-Type: application/json',
) ,
));
$response = curl_exec($curl);
$result = curl_close($curl);
echo $result;
Last modified 2yr ago