Coder Social home page Coder Social logo

graphql-client's Introduction

Minimal GraphQL Laravel Client

Minimal GraphQL client for Laravel.

Requirements

  • Composer 2+

Installation

Install Package (Composer 2+)

composer require bendeckdavid/graphql-client

Usage

Enviroment variable

GRAPHQL_ENDPOINT="https://api.spacex.land/graphql/"

Authentication

We provide a minimal authentication integration by appending the Authorization header to the request client. You can pass the credentials using an env variable.

GRAPHQL_CREDENTIALS="YOUR_CREDENTIALS"

You can also pass auth credentials at runtime using withToken($credentials) method.

'Authorization' header and 'Bearer' Schema are used by default. You can override the default behaviour by defining following variables in your .env file.

GRAPHQL_AUTHENTICATION_HEADER="Authorization"

// Allowed: basic, bearer, custom
GRAPHQL_AUTHENTICATION="bearer"

Usage/Examples

Import GraphQL Client Facades

use BendeckDavid\GraphqlClient\Facades\GraphQL;

Basic use

return GraphQL::query('
    capsules {
        id
        original_launch
        status
        missions {
            name
            flight
        }
    }
')->get();
//->get('json'); //get response as json object

Mutator request:

return GraphQL::mutator('
    insert_user(name: "David") {
        id
        name
        date_added
    }
')->get();
//->get('json');

You can access "query" or "mutator" as a shortcut if you are not passing variables, if is not the case you must use the "raw" attribute:

return GraphQL::raw('
    mutation($name: String) {
        insert_user(name: $name) {
            id
            name
            date_added
        }
    }
')
->with(["name" => "David"])
->get();
//->get('json');

The variables or payload to the GraphQL request can also be passed using magic methods like:

return GraphQL::raw('
    mutation($name: String) {
        insert_user(name: $name) {
            id
            name
            date_added
        }
    }
')
->withName("David")
->get();
//->get('json');

If you want to address te request to another endpoint, you can do :

return GraphQL::endpoint("https://api.spacex.land/graphql/")
->query('
    capsules {
        id
        original_launch
        status
        missions {
            name
            flight
        }
    }
')->get();
//->get('json');

Headers

You can include a header to the request by using the attribute "header" or add multiple headers by "withHeaders":

return GraphQL::query($query)
->header('name', 'value')
->withHeaders([
    'name' => 'value',
    'name' => 'value'
])->get();

Context

Add additional context to the request

return GraphQL::query($query)
->context([
    'ssl' => [
         "verify_peer" => false,
         "verify_peer_name" => false,
    ]
  ])->get();

Author

Top Contributors ⭐

Contributors

graphql-client's People

Contributors

bendeckdavid avatar chiendv avatar ehsanquddusi avatar kerkness avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.