gqlgenc

command module
v0.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2022 License: MIT Imports: 7 Imported by: 0

README

gqlgenc

gqlgenc is a fully featured go gql client, powered by codegen

Why yet another Go GQL client ?

Package Codegen Subscription Extensions
https://github.com/shurcooL/graphql
https://github.com/Yamashou/gqlgenc
https://github.com/hasura/go-graphql-client
https://github.com/beyondan/gqlgenc

GQL Client

Transports

gqlgenc is transport agnostic, and ships with 3 transport implementations:

  • http: Transports GQL queries over http
  • ws: Transports GQL queries over websocket
  • split: Can be used to have a single client use multiple transports depending on the type of query (query, mutation over http and subscription over ws)
Quickstart

Quickstart with a client with http & ws transports:

package main

import (
    "context"
    "github.com/beyondan/gqlgenc/client"
    "github.com/beyondan/gqlgenc/client/transport"
)

func main() {
    wstr := &transport.Ws{
        URL: "wss://example.org/graphql",
    }
    wstr.Start(context.Background())
    defer wstr.Close()

    httptr := &transport.Http{
        URL: "https://example.org/graphql",
    }

    tr := transport.SplitSubscription(wstr, httptr)

    cli := &client.Client {
        Transport: tr,
    }
}
Query/Mutation
var res struct {
    Room string `json:"room"`
}
_, err := cli.Query(ctx, "", "query { room }", nil, &res) // or Mutation
if err != nil {
    panic(err)
}
Subscription
sub, stop := cli.Subscription(ctx, "", "subscription { newRoom }", nil)
defer stop()

for sub.Next() {
    msg := sub.Get()
    
    if len(msg.Errors) > 0 {
        // Do something with them
    }
    
    var res struct {
        Room string `json:"newRoom"`
    }
    err := msg.UnmarshalData(&res)
    if err != nil {
        // Do something with that
    }
}

if err := sub.Err(); err != nil {
    panic(err)
}

GQL Client Codegen

Create a .gqlgenc.yml at the root of your module:

client:
  package: graph
  filename: ./graph/gen_client.go
models:
  Int:
    model: github.com/beyondan/gqlgen/graphql.Int64
  DateTime:
    model: github.com/beyondan/gqlgen/graphql.Time
# The schema can be fetched from files or through introspection
schema:
  - schema.graphqls
endpoint:
  url: https://api.annict.com/graphql # Where do you want to send your request?
  headers: # If you need header for getting introspection query, set it
    Authorization: "Bearer ${ANNICT_KEY}" # support environment variables
query:
  - query.graphql

Fill your query.graphql with queries:

query GetRoom {
    room(name: "secret room") {
        name
    }
}

Run go run github.com/beyondan/gqlgenc

Enjoy:

// Create codegen client
gql := &graph.Client{
    Client: cli,
}

gql.GetRoom(...)

Input as map

In Go, working with JSON and nullity can be tricky. The recommended way to deal with such case is through maps. You can ask gqlgenc to generate such maps with helpers through config:

Globally:

client:
  input_as_map: true

Per model:

models:
  SomeInput:
    as_map: true

Extensions

APQ

Automatic Persisted Queries can be enabled by adding:

cli.Use(&extensions.APQ{})

File Upload

  • In the Http transport, set UseFormMultipart to true

  • In .gqlgenc.yaml:

models:
  Upload:
    model: github.com/beyondan/gqlgenc/client/transport.Upload
  • Profit
up := transport.NewUpload(someFile)

_, _, err := gql.MyUploadFile(ctx, up)

Acknowledgements

This repo is based on the great work of Yamashou/gqlgenc and hasura/go-graphql-client

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL