client

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2024 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go GraphQL Client

This is a flexible and robust GraphQL client implementation in Go. It supports queries, mutations, and subscriptions, with built-in validation and error handling.

Features

  • Support for GraphQL queries, mutations, and subscriptions
  • WebSocket support for subscriptions
  • Flexible Call function for easy execution of any GraphQL operation
    • Works well with gqlgen generated GraphQL resolvers
  • Strong type checking and validation
  • Clear error messages for easier debugging
  • Customizable HTTP and WebSocket endpoints

Installation

To use this GraphQL client in your project, you can clone this repository or import it in your Go project:

go get github.com/amarpal/go-graphql-client

Usage

Here's a basic example of how to use the client:

package main

import (
    "fmt"
    "context"
    "github.com/amarpal/go-graphql-client"
)

func main() {
    // Initialize the client
    client := graphql.New(
        "http://localhost:8080/query",
        "ws://localhost:8080/query",
    )

    // Example query
    err := client.Call(
        generated.QueryResolver.SomeQuery,
        func(response *model.SomeResponse, err error) {
            if err != nil {
                fmt.Printf("Query error: %v\n", err)
                return
            }
            fmt.Printf("Query response: %+v\n", response)
        },
        someInputVariable,
    )
    if err != nil {
        fmt.Printf("Call error: %v\n", err)
    }

    // Example mutation
    err = client.Call(
        generated.MutationResolver.SomeMutation,
        func(response *model.SomeResponse, err error) {
            if err != nil {
                fmt.Printf("Mutation error: %v\n", err)
                return
            }
            fmt.Printf("Mutation response: %+v\n", response)
        },
        someMutationInput,
    )
    if err != nil {
        fmt.Printf("Call error: %v\n", err)
    }

    // Example subscription
    err = client.Call(
        generated.SubscriptionResolver.SomeSubscription,
        func(response *model.SomeResponse, err error) {
            if err != nil {
                fmt.Printf("Subscription error: %v\n", err)
                return
            }
            fmt.Printf("Subscription response: %+v\n", response)
        },
        someSubscriptionInput,
    )
    if err != nil {
        fmt.Printf("Call error: %v\n", err)
    }
}

API Reference

New(graphqlEndpoint, wsEndpoint string) *Client

Creates a new GraphQL client with the specified HTTP and WebSocket endpoints.

(c *Client) Call(resolver interface{}, callback interface{}, args ...interface{}) error

Executes a GraphQL operation (query, mutation, or subscription) based on the provided resolver function. It validates the callback and arguments, then calls the appropriate execution function.

(c *Client) Query(ctx context.Context, query string, variables map[string]interface{}, result interface{}) error

Sends a GraphQL query to the server and unmarshals the response into the result parameter.

(c *Client) Mutate(ctx context.Context, mutation string, variables map[string]interface{}, result interface{}) error

Sends a GraphQL mutation to the server and unmarshals the response into the result parameter.

(c *Client) Subscribe(ctx context.Context, subscription string, variables map[string]interface{}, handler func([]byte) error) error

Initiates a GraphQL subscription over a WebSocket connection.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func New

func New(graphqlEndpoint, wsEndpoint string) *Client

NewClient creates a new GraphQL client with the specified HTTP and WebSocket endpoints. It initializes the HTTP client and stores the endpoints for later use.

func (*Client) Call

func (c *Client) Call(resolver interface{}, callback interface{}, args ...interface{}) error

Call is a generic function that executes a GraphQL operation (query, mutation, or subscription) based on the provided resolver function. It validates the callback and arguments, then calls the appropriate execution function.

func (*Client) Mutate

func (c *Client) Mutate(ctx context.Context, mutation string, variables map[string]interface{}, result interface{}) error

Mutate sends a GraphQL mutation to the server and unmarshals the response into the result parameter. It takes a context for cancellation, the mutation string, a map of variables, and a pointer to the result.

func (*Client) Query

func (c *Client) Query(ctx context.Context, query string, variables map[string]interface{}, result interface{}) error

Query sends a GraphQL query to the server and unmarshals the response into the result parameter. It takes a context for cancellation, the query string, a map of variables, and a pointer to the result.

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, subscription string, variables map[string]interface{}, handler func([]byte) error) error

Subscribe initiates a GraphQL subscription over a WebSocket connection. It takes a context for cancellation, the subscription string, a map of variables, and a handler function to process incoming messages.

Jump to

Keyboard shortcuts

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