Skip to content
An implementation of GraphQL for Go / Golang
Go
Branch: master
Clone or download
Pull request Compare This branch is even with graphql-go:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
benchutil
examples
gqlerrors
language
testutil
.gitignore
CONTRIBUTING.md
LICENSE
README.md
abstract_test.go
definition.go
definition_test.go
directives.go
directives_test.go
enum_type_test.go
executor.go
executor_resolve_test.go
executor_schema_test.go
executor_test.go
extensions.go
extensions_test.go
go.mod
graphql.go
graphql_bench_test.go
graphql_test.go
introspection.go
introspection_test.go
kitchen-sink.graphql
lists_test.go
located.go
mutations_test.go
nonnull_test.go
quoted_or_list_internal_test.go
race_test.go
rules.go
rules_arguments_of_correct_type_test.go
rules_default_values_of_correct_type_test.go
rules_fields_on_correct_type_test.go
rules_fragments_on_composite_types_test.go
rules_known_argument_names_test.go
rules_known_directives_rule_test.go
rules_known_fragment_names_test.go
rules_known_type_names_test.go
rules_lone_anonymous_operation_rule_test.go
rules_no_fragment_cycles_test.go
rules_no_undefined_variables_test.go
rules_no_unused_fragments_test.go
rules_no_unused_variables_test.go
rules_overlapping_fields_can_be_merged.go
rules_overlapping_fields_can_be_merged_test.go
rules_possible_fragment_spreads_test.go
rules_provided_non_null_arguments_test.go
rules_scalar_leafs_test.go
rules_unique_argument_names_test.go
rules_unique_fragment_names_test.go
rules_unique_input_field_names_test.go
rules_unique_operation_names_test.go
rules_unique_variable_names_test.go
rules_variables_are_input_types_test.go
rules_variables_in_allowed_position_test.go
scalars.go
scalars_parse_test.go
scalars_serialization_test.go
scalars_test.go
schema-kitchen-sink.graphql
schema.go
suggested_list_internal_test.go
type_comparators_internal_test.go
type_info.go
types.go
union_interface_test.go
util.go
util_test.go
validation_test.go
validator.go
validator_test.go
values.go
values_test.go
variables_test.go

README.md

graphql CircleCI GoDoc Coverage Status Join the chat at https://gitter.im/graphql-go/graphql

An implementation of GraphQL in Go. Follows the official reference implementation graphql-js.

Supports: queries, mutations & subscriptions.

Documentation

godoc: https://godoc.org/github.com/graphql-go/graphql

Getting Started

To install the library, run:

go get github.com/graphql-go/graphql

The following is a simple example which defines a schema with a single hello string-type field and a Resolve method which returns the string world. A GraphQL query is performed against this schema with the resulting output printed in JSON format.

package main

import (
	"encoding/json"
	"fmt"
	"log"

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

func main() {
	// Schema
	fields := graphql.Fields{
		"hello": &graphql.Field{
			Type: graphql.String,
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				return "world", nil
			},
		},
	}
	rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
	schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
	schema, err := graphql.NewSchema(schemaConfig)
	if err != nil {
		log.Fatalf("failed to create new schema, error: %v", err)
	}

	// Query
	query := `
		{
			hello
		}
	`
	params := graphql.Params{Schema: schema, RequestString: query}
	r := graphql.Do(params)
	if len(r.Errors) > 0 {
		log.Fatalf("failed to execute graphql operation, errors: %+v", r.Errors)
	}
	rJSON, _ := json.Marshal(r)
	fmt.Printf("%s \n", rJSON) // {“data”:{“hello”:”world”}}
}

For more complex examples, refer to the examples/ directory and graphql_test.go.

Third Party Libraries

Name Author Description
graphql-go-handler Hafiz Ismail Middleware to handle GraphQL queries through HTTP requests.
graphql-relay-go Hafiz Ismail Lib to construct a graphql-go server supporting react-relay.
golang-relay-starter-kit Hafiz Ismail Barebones starting point for a Relay application with Golang GraphQL server.
dataloader Nick Randall DataLoader implementation in Go.

Blog Posts

You can’t perform that action at this time.