appsync

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 24 Imported by: 2

README

appsync-client-go

Go Reference Job Status Report

AWS AppSync Go client library

Features

  • GraphQL Query(Queries, Mutations and Subscriptions).
  • MQTT over Websocket for subscriptions.
  • Pure Websockets subscriptions.

Getting Started

Installation
$ go get github.com/sony/appsync-client-go
Example

See example.

License

This library is distributed under The MIT license. See LICENSE and NOTICE for more information.

Documentation

Index

Examples

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
}

Client is the AppSync GraphQL API client

Example (Graphqlws_subscription)
package main

import (
	"encoding/json"
	"fmt"
	"log/slog"
	"os"
	"strings"

	"github.com/sony/appsync-client-go/internal/appsynctest"

	appsync "github.com/sony/appsync-client-go"
	"github.com/sony/appsync-client-go/graphql"
)

func main() {
	server := appsynctest.NewAppSyncEchoServer()
	defer server.Close()

	client := appsync.NewClient(appsync.NewGraphQLClient(graphql.NewClient(server.URL)))
	subscription := `subscription SubscribeToEcho() { subscribeToEcho }`

	ch := make(chan *graphql.Response)
	subscriber := appsync.NewPureWebSocketSubscriber(
		strings.Replace(server.URL, "http", "ws", 1),
		graphql.PostRequest{
			Query: subscription,
		},
		func(r *graphql.Response) { ch <- r },
		func(err error) {
			slog.Warn("unable to create new pure websocket subscriber  ", "error", err)
		},
	)

	if err := subscriber.Start(); err != nil {
		slog.Error("unable to start subscriber", "error", err)
		os.Exit(1)
	}
	defer subscriber.Stop()

	mutation := `mutation Echo($message: String!) { echo(message: $message) }`
	variables := json.RawMessage(fmt.Sprintf(`{ "message": "%s" }`, "Hi, AppSync!"))
	_, err := client.Post(graphql.PostRequest{
		Query:     mutation,
		Variables: &variables,
	})
	if err != nil {
		slog.Error("unable to post mutation", "error", err)
		os.Exit(1)
	}

	response := <-ch
	data := new(string)
	if err := response.DataAs(data); err != nil {
		slog.Error("unable to process data", "error", err, "response", response)
		os.Exit(1)
	}
	fmt.Println(*data)

}
Output:

Hi, AppSync!
Example (Mqtt_subscription)
package main

import (
	"encoding/json"
	"fmt"
	"log/slog"
	"os"

	"github.com/sony/appsync-client-go/internal/appsynctest"

	appsync "github.com/sony/appsync-client-go"
	"github.com/sony/appsync-client-go/graphql"
)

func main() {
	server := appsynctest.NewAppSyncEchoServer()
	defer server.Close()

	client := appsync.NewClient(appsync.NewGraphQLClient(graphql.NewClient(server.URL)))
	subscription := `subscription SubscribeToEcho() { subscribeToEcho }`
	response, err := client.Post(graphql.PostRequest{
		Query: subscription,
	})
	if err != nil {
		slog.Error("unable to post subscription", "error", err)
		os.Exit(1)
	}

	ext, err := appsync.NewExtensions(response)
	if err != nil {
		slog.Error("unable to process extensions", "error", err)
		os.Exit(1)
	}

	ch := make(chan *graphql.Response)
	subscriber := appsync.NewSubscriber(*ext,
		func(r *graphql.Response) { ch <- r },
		func(err error) {
			slog.Warn("unable to create new subscriber", "error", err)
		},
	)

	if err := subscriber.Start(); err != nil {
		slog.Error("unable to start subscriber", "error", err)
		os.Exit(1)
	}
	defer subscriber.Stop()

	mutation := `mutation Echo($message: String!) { echo(message: $message) }`
	variables := json.RawMessage(fmt.Sprintf(`{ "message": "%s" }`, "Hi, AppSync!"))
	_, err = client.Post(graphql.PostRequest{
		Query:     mutation,
		Variables: &variables,
	})
	if err != nil {
		slog.Error("unable to post mutation", "error", err)
		os.Exit(1)
	}

	response = <-ch
	data := new(string)
	if err := response.DataAs(data); err != nil {
		slog.Error("unable to process data", "error", err, "response", response)
		os.Exit(1)
	}
	fmt.Println(*data)

}
Output:

Hi, AppSync!

func NewClient

func NewClient(graphql GraphQLClient, opts ...ClientOption) *Client

NewClient returns a Client instance.

func (*Client) Post

func (c *Client) Post(request graphql.PostRequest) (*graphql.Response, error)

Post is a synchronous AppSync GraphQL POST request.

Example (Mutation)
package main

import (
	"encoding/json"
	"fmt"
	"log/slog"
	"os"

	"github.com/sony/appsync-client-go/internal/appsynctest"

	appsync "github.com/sony/appsync-client-go"
	"github.com/sony/appsync-client-go/graphql"
)

func main() {
	server := appsynctest.NewAppSyncEchoServer()
	defer server.Close()

	client := appsync.NewClient(appsync.NewGraphQLClient(graphql.NewClient(server.URL)))
	mutation := `mutation Echo($message: String!) { echo(message: $message) }`
	variables := json.RawMessage(fmt.Sprintf(`{ "message": "%s"	}`, "Hi, AppSync!"))
	response, err := client.Post(graphql.PostRequest{
		Query:     mutation,
		Variables: &variables,
	})
	if err != nil {
		slog.Error("unable to post mutation", "error", err)
		os.Exit(1)
	}

	data := new(string)
	if err := response.DataAs(data); err != nil {
		slog.Error("unable to process data", "error", err, "response", response)
		os.Exit(1)
	}
	fmt.Println(*data)

}
Output:

Hi, AppSync!
Example (Query)
package main

import (
	"fmt"
	"log/slog"
	"os"

	"github.com/sony/appsync-client-go/internal/appsynctest"

	appsync "github.com/sony/appsync-client-go"
	"github.com/sony/appsync-client-go/graphql"
)

func main() {
	server := appsynctest.NewAppSyncEchoServer()
	defer server.Close()

	query := `query Message() { message }`
	client := appsync.NewClient(appsync.NewGraphQLClient(graphql.NewClient(server.URL)))
	response, err := client.Post(graphql.PostRequest{
		Query: query,
	})
	if err != nil {
		slog.Error("unable to post query", "error", err)
		os.Exit(1)
	}

	data := new(string)
	if err := response.DataAs(data); err != nil {
		slog.Error("unable to process data", "error", err, "response", response)
		os.Exit(1)
	}
	fmt.Println(*data)

}
Output:

Hello, AppSync!

func (*Client) PostAsync

func (c *Client) PostAsync(request graphql.PostRequest, callback func(*graphql.Response, error)) (context.CancelFunc, error)

PostAsync is an asynchronous AppSync GraphQL POST request.

type ClientOption

type ClientOption func(*Client)

ClientOption represents options for an AppSync client.

func WithIAMAuthorization deprecated added in v1.1.0

func WithIAMAuthorization(signer sdkv1_v4.Signer, region, host string) ClientOption

WithIAMAuthorization returns a ClientOption configured with the given sdk v1 signature version 4 signer.

Deprecated: for backward compatibility.

func WithIAMAuthorizationV1 added in v1.5.0

func WithIAMAuthorizationV1(signer *sdkv1_v4.Signer, region, url string) ClientOption

WithIAMAuthorizationV1 returns a ClientOption configured with the given sdk v1 signature version 4 signer.

func WithIAMAuthorizationV2 added in v1.5.0

func WithIAMAuthorizationV2(signer *sdkv2_v4.Signer, creds aws.Credentials, region, url string) ClientOption

WithIAMAuthorizationV2 returns a ClientOption configured with the given sdk v2 signature version 4 signer.

func WithSubscriberID

func WithSubscriberID(subscriberID string) ClientOption

WithSubscriberID returns a ClientOption configured with the given AppSync subscriber ID

type Extensions

type Extensions struct {
	Subscription struct {
		Version         string `json:"version"`
		MqttConnections []struct {
			URL    string   `json:"url"`
			Topics []string `json:"topics"`
			Client string   `json:"client"`
		} `json:"mqttConnections"`
		NewSubscriptions map[string]Subscription `json:"newSubscriptions"`
	} `json:"subscription"`
}

Extensions represents AWS AppSync subscription response extensions

func NewExtensions

func NewExtensions(response *graphql.Response) (*Extensions, error)

NewExtensions returns Extensions instance

type GraphQLClient

type GraphQLClient interface {
	Post(header http.Header, request graphql.PostRequest) (*graphql.Response, error)
	PostAsync(header http.Header, request graphql.PostRequest, callback func(*graphql.Response, error)) (context.CancelFunc, error)
}

GraphQLClient is the interface to access GraphQL server.

func NewGraphQLClient

func NewGraphQLClient(client *graphql.Client) GraphQLClient

NewGraphQLClient returns a GraphQLClient interface

type PureWebSocketSubscriber added in v1.2.0

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

PureWebSocketSubscriber has pure WebSocket connections and subscription information.

func NewPureWebSocketSubscriber added in v1.2.0

func NewPureWebSocketSubscriber(realtimeEndpoint string, request graphql.PostRequest,
	onReceive func(response *graphql.Response),
	onConnectionLost func(err error),
	opts ...PureWebSocketSubscriberOption) *PureWebSocketSubscriber

NewPureWebSocketSubscriber returns a PureWebSocketSubscriber instance.

func (*PureWebSocketSubscriber) Abort added in v1.3.0

func (p *PureWebSocketSubscriber) Abort()

Abort ends the subscription forcibly.

func (*PureWebSocketSubscriber) Start added in v1.2.0

func (p *PureWebSocketSubscriber) Start() error

Start starts a new subscription.

func (*PureWebSocketSubscriber) Stop added in v1.2.0

func (p *PureWebSocketSubscriber) Stop()

Stop ends the subscription.

type PureWebSocketSubscriberOption added in v1.2.0

type PureWebSocketSubscriberOption func(*PureWebSocketSubscriber)

PureWebSocketSubscriberOption represents options for an PureWebSocketSubscriber.

func WithAPIKey added in v1.2.0

func WithAPIKey(host, apiKey string) PureWebSocketSubscriberOption

WithAPIKey returns a PureWebSocketSubscriberOption configured with the host for the AWS AppSync GraphQL endpoint and API key

func WithIAM deprecated added in v1.2.0

func WithIAM(signer *sdkv1_v4.Signer, region, host string) PureWebSocketSubscriberOption

WithIAM returns a PureWebSocketSubscriberOption configured with the signature version 4 signer, the region and the host for the AWS AppSync GraphQL endpoint.

Deprecated: for backward compatibility.

func WithIAMV1 added in v1.5.0

func WithIAMV1(signer *sdkv1_v4.Signer, region, url string) PureWebSocketSubscriberOption

WithIAMV1 returns a PureWebSocketSubscriberOption configured with the sdk v1 signature version 4 signer, the region and the url for the AWS AppSync GraphQL endpoint.

func WithIAMV2 added in v1.5.0

func WithIAMV2(signer *sdkv2_v4.Signer, creds aws.Credentials, region, url string) PureWebSocketSubscriberOption

WithIAMV2 returns a PureWebSocketSubscriberOption configured with the sdk v2 signature version 4 signer, the credentials, the region and the url for the AWS AppSync GraphQL endpoint.

func WithOIDC added in v1.2.0

func WithOIDC(host, jwt string) PureWebSocketSubscriberOption

WithOIDC returns a PureWebSocketSubscriberOption configured with the host for the AWS AppSync GraphQL endpoint and JWT Access Token.

type Subscriber

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

Subscriber has MQTT connections and subscription information.

func NewSubscriber

func NewSubscriber(extensions Extensions, callback func(response *graphql.Response), onConnectionLost func(err error)) *Subscriber

NewSubscriber returns a new Subscriber instance.

func (*Subscriber) Start

func (s *Subscriber) Start() error

Start starts a new subscription.

func (*Subscriber) Stop

func (s *Subscriber) Stop()

Stop ends the subscription.

type Subscription added in v1.1.0

type Subscription struct {
	Topic      string      `json:"topic"`
	ExpireTime interface{} `json:"expireTime"`
}

Subscription represents AWS AppSync subscription mqtt topic

Directories

Path Synopsis
internal
test

Jump to

Keyboard shortcuts

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