blok/graphql

A graphql query and client helper

1.1.2 2022-04-06 16:24 UTC

This package is not auto-updated.

Last update: 2024-11-16 05:26:17 UTC


README

Build Status Packagist Packagist Packagist

Package description :

Very simple GraphqlClient helper to make a graphql call.

Installation

Install via composer

composer require blok/graphql

Register Service Provider

Note! This and next step are optional if you use laravel>=5.5 with package auto discovery feature.

Add service provider to config/app.php in providers section

Blok\Graphql\ServiceProvider::class,

Register Facade

Register package facade in config/app.php in aliases section

Blok\Graphql\Facades\Graphql::class,

Publish Configuration File

php artisan vendor:publish --provider="Blok\Graphql\ServiceProvider" --tag="config"

Usage as a standalone php

Simply add your query and param like that (here is an exemple with the Opencollective GraphQL api) :


use Blok\Graphql\Graphql;

    $query = <<<EOT
query allTransactions(\$slug: String){
  allTransactions(collectiveSlug: \$slug){
    id,
    uuid,
    amount,
    currency,
    description,
    hostCurrency,
    hostCurrencyFxRate,
    netAmountInCollectiveCurrency,
    type,
    createdAt,
    updatedAt,
    refundTransaction{id},
    collective{id, name, slug},
  }
}
EOT;

    $graphql = new Graphql(YOUR_URL, YOUR_BEARER_TOKEN);
    $result = $graphql->query($query, ['slug' => 'co-labs']);

Use as a Laravel Graphql instance

For convenience you have also an instanciated graphql service provider that will use the setup from your env file : GRAPHQL_URL and GRAPHQL_TOKEN.

To use it you can use the Facade or app :


use Blok\Graphql\Facades\Graphql;

    $query = <<<EOT
query allTransactions(\$slug: String){
  allTransactions(collectiveSlug: \$slug){
    id,
    uuid,
    amount,
    currency,
    description,
    hostCurrency,
    hostCurrencyFxRate,
    netAmountInCollectiveCurrency,
    type,
    createdAt,
    updatedAt,
    refundTransaction{id},
    collective{id, name, slug},
  }
}
EOT;

    $result = app('graphql')->query($query, ['slug' => 'co-labs']);

    or

    $result = Graphql::query($query, ['slug' => 'co-labs']);

Security

If you discover any security related issues, please email instead of using the issue tracker.

Credits

This package is bootstrapped with the help of blok/laravel-package-generator.