Structure Graphql query
Hi
Please can you help.
I just want to confirm my understanding of how to pass graphql variables from php or any other lanuage into a graphql query. I am using php Curl to perform my request to the graphql api server.
code:
$data='
query($businessId : ID!, $page : Int!, $pageSize : Int!) {
business(id: $businessId) {
id
isClassicInvoicing
invoices(page: $page, pageSize: $pageSize) {'
...
$variables=array(
'$businessId'=>'"xxxx"',
'$page'=>1,
'$pageSize'=>5);
$payData = urlencode($data);
$payVariables = urlencode(json_encode($variables));
$apiURL=$apiURL . '?' . "query=".$payData."&variables=".$payVariables ;
The error I get is - Variable "$page" of required type "Int!" was not provided. I receive this error for $businessId and $pageSize also.
Comments
Sorry for the confusion as the graphql variables are $'s and the php way of declaring variables is also $.
Figured it out:
The structure of $variables was not correct. The correct way is
$variables='
{
"businessId": "xxxx",
"page": 1,
"pageSize": 5
}';
Hi @golabekp,
It looks like you are trying to define the operation and variables as part of the request url itself. However, we recommend sending the operation and variables as part of the request body (docs). I believe you may be receiving that error because the variables cannot be passed within the URL (or the extra quotes around
xxxx
).Hope that helps provide some additional direction.
Glad we could help!
Thanks for the assistance guys