Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mechanism to modify outgoing http request #23

Merged
merged 1 commit into from
Dec 15, 2021

Conversation

dbarrosop
Copy link

@dbarrosop dbarrosop commented Dec 14, 2021

The purpose of this PR is to allow users of this library to modify the outgoing request. It is useful as it allows you to set authentication headers without having to write a custom http client (ref. shurcooL#28 (comment))

The mechanism implemented for this has two purposes:

  1. It maintains backwards compatibility
  2. It allows reusing the same client amongst slightly different "configurations". This allows you to have one single http client in multitenant applications with different authentication headers.

You can see a silly proof of concept below to illustrate how different requests can tweak the header slightly different while reusing the same TCP socket for performance reasons:

package main

import (
	"context"
	"fmt"
	"net/http"
	"sync"

	"github.com/hasura/go-graphql-client"
)

func setAuthHeader(secret string) func(req *http.Request) {
	return func(req *http.Request) {
		req.Header.Add("x-hasura-admin-secret", secret)
	}
}

func main() {
	client := graphql.NewClient("http://localhost:8080/v1/graphql", nil)

	wg := &sync.WaitGroup{}
	wg.Add(100)

	for i := 0; i < 100; i++ {
		go func(i int) {
			defer wg.Done()

			if i%2 == 0 {
				client = client.WithRequestModifier(setAuthHeader("hello123"))
			} else {
				client = client.WithRequestModifier(setAuthHeader("hello124"))
			}

			var query struct {
				StorageBucketsByPK struct {
					ID graphql.String `graphql:"id"`
				} `graphql:"storage_buckets_by_pk(id: $id)"`
			}

			variables := map[string]interface{}{
				"id": graphql.String("default"),
			}

			err := client.Query(context.Background(), &query, variables)
			if err != nil {
				fmt.Println(err.Error())
			}
			fmt.Println(query.StorageBucketsByPK.ID)
		}(i)
	}

	wg.Wait()
}

@hgiasac hgiasac merged commit 8c8fa4d into hasura:master Dec 15, 2021
@advdv
Copy link

advdv commented Oct 20, 2022

It would be nice to have this on the *SubscriptionClient as well. That way we can easily add authorization header on WebSocket connections as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants