graphiql

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2019 License: MIT Imports: 3 Imported by: 34

README

Golang GraphiQL

GitHub version Build Status codecov Go Report Card CDNJS GoDoc

Golang GraphiQL

An utility wrote on Go for using GraphiQL without need to use NPM.

Try the live demo.

Getting started

To install the library, run

go get -u github.com/friendsofgo/graphiql

To build graphi's CLI you must run

make build-cli

This will generate a binary in the project's bin directory called graphiql. You can then move this binary to anywhere in your PATH.

Use GraphiQL on your own server

If you've a GraphQL server on GO and you want to include the GraphiQL on it, it's as simple as this:

package main

import(
	"net/http"
	"github.com/friendsofgo/graphiql"
)

func main() {
	
	graphiqlHandler, err := graphiql.NewGraphiqlHandler("/graphql")
	if err != nil {
		panic(err)
	}
	
	http.HandleFunc("/graphql", myGraphQLHandler)
	http.Handle("/graphiql", graphiqlHandler)
	http.ListenAndServe(":8080", nil)
}
Use GraphiQL standalone

If you want launch GraphiQL for use on your GraphQL API but you don't want include it in your own project you can launch your own GraphiQL Server:

package main

import(
	"net/http"
	"github.com/friendsofgo/graphiql"
)

func main() {
	graphiqlHandler, err := graphiql.NewGraphiqlHandler("http://localhost:8080/graphql")
	if err != nil {
    		panic(err)
	}
    	
	
	http.Handle("/graphiql", graphiqlHandler)
	http.ListenAndServe(":4040", nil)
}

CLI

Yo can use graphiql as command

$ graphiql --help
usage: graphiql [-version] [OPTIONS]
        graphiql is an standalone server to use Graphiql, based on https://github.com/graphql/graphiql

  -endpoint string
        Endpoint where are hosted your API, eg: http://localhost:8080/graphql (default "http://localhost:8080/graphql")
  -graphiql
        Show the js client of GraphiQL version
  -port string
        Port where will be launched the GraphiQL client (default "4040")
  -version
        Show the graphiql version information

And it'll launch a GraphiQL server

$ graphiql

2018/11/24 17:10:36 GraphiQL run on http://localhost:4040/graphiql...
2018/11/24 17:10:36 GraphQL endpoint http://localhost:8080/graphql
2018/11/24 17:10:36 Use (ctrl+c) to terminate the execution

Docker

The application is also available through Docker, just run:

docker run -it --rm -p 4040:4040 friendsofgo/graphiql

Remember to use the -p flag to expose the container port where the application is listening (4040 by default).

Contribute

Contributions are more than welcome, if you are interested please fork this repo and send your Pull Request.

License

MIT License, see LICENSE

Documentation

Index

Constants

View Source
const (

	// GraphiQLVersion graphiql js library version
	GraphiQLVersion = "0.13.0"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

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

Handler define the struct of graphiql server

func NewGraphiqlHandler

func NewGraphiqlHandler(endpoint string) (*Handler, error)

NewGraphiqlHandler preparing the graphiql client to execute on your own server The endpoint is the url where you have your graphql api hosted

func (*Handler) ServeHTTP

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

ServeHTTP is a handler function for resolve graphiql client on servers

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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