Skip to content

rabelos-coder/php-graphql-client

Repository files navigation

PHP GraphQL Client

Latest Version Software License Total Downloads

PHP Client for GraphQL

Main features

  • Client with file attachment support
  • Easy query/mutation execution
  • Simple array results for mutation and queries
  • Powerful object results for mutation and queries

Installation

Via composer:

composer require rabelos-coder/php-graphql-client

Documentation

Instantiate a client

You can instantiate a simple client.

Simple Client:

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

Using the GraphQL Client

You can use the client to execute queries and mutations and get the results.

<?php

/**
 * 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
}

In the previous examples, the client is used to execute queries and mutations. The response object is used to get the results in array format.

License

The MIT license. Please see LICENSE for more information.

About

A PHP library that simplifies the process of interacting with GraphQL API's by providing simple client.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages