graphqlclientgen

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 4 Imported by: 1

README

graphqlclientgen

Here Be Dragons 🐉

This is an experiment of generating GraphQL clients in Golang. It's heavily inspired by gqlgen.

  • Goals

    • Structure config and generated clients in a future proof way to enable redesign of codegen
    • Single client generated for entire schema
    • ProtoClient interface to support multiple protocols and custom clients
    • Use static types in client interface to check api compability during build time
    • Use GraphQL comments in Golang type and function definitions
    • Enable field selection in client to partially fetch objects
    • Reuse gqlgen custom scalars
  • Limitations

    • Mostly designed to use GraphQL as standard RPC
    • Weak support for deep graph fetching (can't have multiple fields with same object type)
    • Only supports scalars and objects
    • Only supports hard-coded basic scalars (bool, float, id, string, int)

    Examples

    func Example() {
      // create client, supports custom protocols through ProtoClient interface
      c := client.New(graphqlclientgen.NewHttpClient("http://localhost:8080/query"))
    
      todo, err := c.CreateTodo(context.TODO(),
        // static typed inputs
        client.NewTodo{
          Text:   "bar",
          UserId: "5",
        },
        // explicit requested fields, easy to use with auto-complete
        client.TodoFields{
          client.TodoFieldID,
          client.TodoFieldText,
          // supports requesting specific fields of related objects
          client.UserFields{
            client.UserFieldID,
            client.UserFieldName,
          },
        })
      }
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Body

type Body struct {
	Query     string         `json:"query"`
	Variables map[string]any `json:"variables"`
}

type Error

type Error struct {
	Message    string            `json:"message"`
	Extensions map[string]string `json:"extensions"`
}

type HttpClient

type HttpClient struct {
	// contains filtered or unexported fields
}

func NewHttpClient

func NewHttpClient(url string, opts ...HttpClientOpts) HttpClient

NewHttpClient creates a new HttpClient for graphqlclientgen clients

func (HttpClient) Do

func (httpClient HttpClient) Do(ctx context.Context, in Body, out any) error

Do performs http post request towards a GraphQL api

type HttpClientOpts

type HttpClientOpts func(*HttpClient)

func WithHttpClient

func WithHttpClient(c *http.Client) HttpClientOpts

WithHttpClient sets a custom http.Client for HttpClient

type ProtoClient

type ProtoClient interface {
	Do(ctx context.Context, in Body, out any) error
}

type Response

type Response struct {
	Data   json.RawMessage `json:"data"`
	Errors []Error         `json:"errors"`
}

Directories

Path Synopsis
natsproto module

Jump to

Keyboard shortcuts

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