Structure Graphql query

golabekpgolabekp Member Posts: 14

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.

Tagged:

Comments

  • golabekpgolabekp Member Posts: 14

    Sorry for the confusion as the graphql variables are $'s and the php way of declaring variables is also $.

  • golabekpgolabekp Member Posts: 14

    Figured it out:

    The structure of $variables was not correct. The correct way is

    $variables='
    {
    "businessId": "xxxx",
    "page": 1,
    "pageSize": 5
    }';

  • RobVGRobVG Member Posts: 53 admin

    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).

    $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);
    
    $apiBody=json_encode(array(
    'query'=>$data,
    'variables'=>$variables));
    

    Hope that helps provide some additional direction.

    edited June 11, 2020
  • PaulCPaulC Member Posts: 186 ✭✭✭

    Glad we could help! :)

  • golabekpgolabekp Member Posts: 14

    Thanks for the assistance guys

Sign In or Register to comment.