graphqlhttp

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package graphqlhttp provides functions for serving GraphQL over HTTP as described in https://graphql.org/learn/serving-over-http/.

Example
package main

import (
	"log"
	"net/http"

	"zombiezen.com/go/graphql-server/graphql"
	"zombiezen.com/go/graphql-server/graphqlhttp"
)

// Query is the GraphQL object read from the server.
type Query struct {
	Greeting string
}

func main() {
	// Set up the server.
	schema, err := graphql.ParseSchema(`
		type Query {
			greeting: String!
		}
	`, nil)
	if err != nil {
		log.Fatal(err)
	}
	queryObject := &Query{Greeting: "Hello, World!"}
	server, err := graphql.NewServer(schema, queryObject, nil)
	if err != nil {
		log.Fatal(err)
	}

	// Serve over HTTP using NewHandler.
	http.Handle("/graphql", graphqlhttp.NewHandler(server))
	http.ListenAndServe(":8080", nil)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(schema *graphql.Schema, r *http.Request) (graphql.Request, error)

Parse parses a GraphQL HTTP request. If an error is returned, StatusCode will return the proper HTTP status code to use. Parse may return a validated query, but it may not always do so.

Request methods may be GET, HEAD, or POST. If the method is not one of these, then an error is returned that will make StatusCode return http.StatusMethodNotAllowed.

func StatusCode

func StatusCode(err error) int

StatusCode returns the HTTP status code an error indicates.

func WriteResponse

func WriteResponse(w http.ResponseWriter, response graphql.Response)

WriteResponse writes a GraphQL result as an HTTP response.

Types

type Handler

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

Handler serves GraphQL HTTP requests by executing them on its server.

func NewHandler

func NewHandler(server *graphql.Server) *Handler

NewHandler returns a new handler that sends requests to the given server.

func (*Handler) ServeHTTP

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

ServeHTTP executes a GraphQL request.

Jump to

Keyboard shortcuts

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