The graphql package does not handle authentication directly. Instead, you should provide an http.Client that performs authentication when creating a new client.
For simple header injection, use the WithRequestModifier method. For standard OAuth2 flows, it is recommended to use the golang.org/x/oauth2 package to create an http.Client and pass it to graphql.NewClient.
// Using WithRequestModifier for custom headers
client := graphql.NewClient(endpoint, http.DefaultClient).
WithRequestModifier(func(r *http.Request) {
r.Header.Set("Authorization", "random-token")
})
// Using OAuth2
import "golang.org/x/oauth2"
func main() {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GRAPHQL_TOKEN")},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := graphql.NewClient("https://example.com/graphql", httpClient)
}