handler

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 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

handler

CI Go Reference

A Go http.Handler for gql-go/graphql.

This is a community-maintained continuation of graphql-go/handler, created by Hafiz Ismail and maintained by Chris Ramón. It moves together with gql-go/graphql; see that repository for the motivation, the commitments and the roadmap. Its transport items (GraphQL over HTTP, WebSocket and server-sent events, persisted queries) will land here.

Getting started

go get github.com/gql-go/handler
package main

import (
	"net/http"

	"github.com/gql-go/graphql"
	"github.com/gql-go/handler"
)

func main() {
	schema, _ := graphql.NewSchema(graphql.SchemaConfig{ /* ... */ })

	h := handler.New(&handler.Config{
		Schema:   &schema,
		Pretty:   true,
		GraphiQL: true,
	})

	http.Handle("/graphql", h)
	http.ListenAndServe(":8080", nil)
}

To serve GraphQL Playground instead of GraphiQL, set GraphiQL: false and Playground: true.

Migrating from graphql-go/handler

The API is unchanged. Replace both import prefixes:

github.com/graphql-go/handler  ->  github.com/gql-go/handler
github.com/graphql-go/graphql  ->  github.com/gql-go/graphql

Request parameters

The handler accepts:

  • query: the GraphQL document to execute.
  • variables: the values for the operation's variables, as a JSON object.
  • operationName: which operation to run when query contains several named operations.

It looks for them first in the URL query string:

/graphql?query=query+getUser($id:ID){user(id:$id){name}}&variables={"id":"4"}

Otherwise it reads the POST body according to its Content-Type:

  • application/json: a JSON object with the parameters above.
  • application/x-www-form-urlencoded: url-encoded key-value pairs.
  • application/graphql: the body is the query itself.

NewRequestOptions exposes this parsing on its own, for servers that execute the operation themselves.

Contributing

Issues and pull requests are welcome, including ones still open on the original repository. See CONTRIBUTING.md for the process and the gql-go/graphql README for how the project is run.

License

MIT, the same as the original project.

Documentation

Index

Constants

View Source
const (
	ContentTypeJSON           = "application/json"
	ContentTypeGraphQL        = "application/graphql"
	ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Schema           *graphql.Schema
	Pretty           bool
	GraphiQL         bool
	Playground       bool
	PlaygroundConfig *PlaygroundConfig
	RootObjectFn     RootObjectFn
	ResultCallbackFn ResultCallbackFn
	FormatErrorFn    func(err error) gqlerrors.FormattedError
}

func NewConfig

func NewConfig() *Config

type Handler

type Handler struct {
	Schema *graphql.Schema
	// contains filtered or unexported fields
}

func New

func New(p *Config) *Handler

func (*Handler) ContextHandler

func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *http.Request)

ContextHandler provides an entrypoint into executing graphQL queries with a user-provided context.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP provides an entrypoint into executing graphQL queries.

type PlaygroundConfig added in v0.3.0

type PlaygroundConfig struct {
	Endpoint             string
	SubscriptionEndpoint string
}

type RequestOptions

type RequestOptions struct {
	Query         string                 `json:"query" url:"query" schema:"query"`
	Variables     map[string]interface{} `json:"variables" url:"variables" schema:"variables"`
	OperationName string                 `json:"operationName" url:"operationName" schema:"operationName"`
}

func NewRequestOptions

func NewRequestOptions(r *http.Request) *RequestOptions

RequestOptions Parses a http.Request into GraphQL request options struct

type ResultCallbackFn added in v0.2.2

type ResultCallbackFn func(ctx context.Context, params *graphql.Params, result *graphql.Result, responseBody []byte)

type RootObjectFn added in v0.2.2

type RootObjectFn func(ctx context.Context, r *http.Request) map[string]interface{}

RootObjectFn allows a user to generate a RootObject per request

Jump to

Keyboard shortcuts

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