GithubHelp home page GithubHelp logo

maaft / gqlgenc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from infiotinc/gqlgenc

0.0 0.0 0.0 168 KB

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

License: MIT License

Makefile 0.50% Go 99.50%

gqlgenc's Introduction

gqlgenc

I needed a newer gqlgen version, therefore I forked from https://github.com/infiotinc/gqlgenc due to inactivity. All credits belong the original authors.

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/maaft/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/maaft/gqlgenc/client"
    "github.com/maaft/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/99designs/gqlgen/graphql.Int64
  DateTime:
    model: github.com/99designs/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/maaft/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/maaft/gqlgenc/client/transport.Upload
  • Profit
up := transport.NewUpload(someFile)

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

Acknowledgements

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

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.