Giter Club home page Giter Club logo

go-gqlclient's Introduction

gqlclient GoDoc

GraphQL client for Go.

  • Simple, familiar API
  • Pass context.Context to the http client
  • Build and execute a GraphQL request using json or multipart
  • Use strong Go types for response data
  • Use variables, custom headers and a custom http client
  • Advanced error handling

Installation

Make sure you have a working Go environment. To install gqlclient, simply run:

$ go get github.com/weavedev/go-gqlclient

Usage

import (
    gql "github.com/weavedev/go-gqlclient"
)


// Create a client (safe to share across requests)
client := gql.NewClient(
    "https://localhost/graphql",
    // Optionally supply options:
    // Set default headers.
    gql.WithDefaultHeader("Authorization", "Bearer " + token),
    // Use a custom http.Client.
    gql.WithHTTPClient(customClient),
    // Use another request builder (default: gql.JSONRequestBuilder).
    gql.WithRequestBuilder(gql.MultipartRequestBuilder),
)

// Make a request
req := gql.NewRequest(`
    query ($key: String!) {
        item(id: $key) {
            field1
            field2
            field3
        }
    }`,
    // Optionally supply options:
    // Set any variables.
    gql.WithVar("key", "value"),
    // Set header fields.
    gql.WithHeader("Cache-Control", "no-cache"),
    // Pass a Context for the request (default: context.Background()).
    gql.WithContext(ctx),
)

// Do the request and capture the response.
var resp struct {
    Item struct {
        Field1 string
        Field2 string
        Field3 string
    }
}
err := client.Do(req, &resp)

// Inspect the returned GraphQL errors
var gqlerrs gql.ErrorList
if errors.As(err, &gqlerrs) {
    // Check path
    println(gqlerrs[0].Path)
}

Thanks

Inspired by https://github.com/machinebox/graphql

go-gqlclient's People

Contributors

gerarts avatar mathieupost avatar

Watchers

 avatar  avatar  avatar

Forkers

mathieupost

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.