PHP code example of rabelos-coder / php-graphql-client

1. Go to this page and download the library: Download rabelos-coder/php-graphql-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rabelos-coder / php-graphql-client example snippets



$client = new \RabelosCoder\GraphQL\Client('https://your-domain/graphql');



/**
 * Query Example
 */
$query = <<<'GQL'
query GetFooBar($idFoo: String, $idBar: String) {
  foo(id: $idFoo) {
    id_foo
    bar (id: $idBar) {
      id_bar
    }
  }
}
GQL;

$variables = [
    'idFoo' => 'foo',
    'idBar' => 'bar',
];

/** @var \RabelosCoder\GraphQL\Client $client */
$request = $client->query($query, $variables);

try {
    // returns response array
    $response = $request->send();

    return $response;
} catch (\Exception $e) {
    // Returns exception message
}

/**
 * Mutation Example
 */
$mutation = <<<'GQL'
mutation ($foo: ObjectInput!){
  CreateObjectMutation (object: $foo) {
    status
  }
}
GQL;

$variables = [
    'foo' => [
        'id_foo' => 'foo',
        'bar' => [
            'id_bar' => 'bar'
        ]
    ]
];

/** @var \RabelosCoder\GraphQL\Client $client */
$request = $client->query($mutation, $variables);

try {
    // returns response array
    $response = $request->send();

    return $response;
} catch (\Exception $e) {
    // Returns exception message
}

/**
 * Mutation With Single File Upload Example
 */
$mutation = <<<'GQL'
mutation ($file: Upload!){
  CreateObjectMutation (object: $file) {
    fileName
    filePath
  }
}
GQL;

$file = $_FILES['fieldName'];
$uploaded =  [
    'fileName' => $file['name'],
    'mimeType' => $file['type'],
    'filePath' => $file['tmp_name'],
];


$variables = [
    'file' => null,
];

/** @var \RabelosCoder\GraphQL\Client $client */
$request = $client->fileField('file')
            ->attachment($uploaded)
            ->query($mutation, $variables);

try {
    // returns response array
    $response = $request->send();

    return $response;
} catch (\Exception $e) {
    // Returns exception message
}

/**
 * Mutation With Multiple File Upload Example
 */
$mutation = <<<'GQL'
mutation ($files: [Upload!]!){
  CreateObjectMutation (object: $files) {
    fileName
    filePath
  }
}
GQL;

$files = $_FILES['fieldName']; // Remember that form field input name must contains [] at the end and the property multiple setted.
$uploaded = [];

foreach ($files as $file) {
    $uploaded[] = [
        'fileName' => $file['name'],
        'mimeType' => $file['type'],
        'filePath' => $file['tmp_name'],
    ];
}

$variables = [
    'files' => array_map(fn() => null, array_keys($uploaded)),
];

/** @var \RabelosCoder\GraphQL\Client $client */
$request = $client->filesField('files')
            ->attachments($uploaded)
            ->query($mutation, $variables);

try {
    // returns response array
    $response = $request->send();

    return $response;
} catch (\Exception $e) {
    // Returns exception message
}


composer