File: main.go

package info (click to toggle)
golang-github-graph-gophers-graphql-go 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,452 kB
  • sloc: sh: 373; javascript: 21; makefile: 5
file content (57 lines) | stat: -rw-r--r-- 1,185 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Package main demonstrates a simple web app that uses type-safe enums in a GraphQL resolver.
package main

import (
	"context"
	_ "embed"
	"log"
	"net/http"

	"github.com/graph-gophers/graphql-go"
	"github.com/graph-gophers/graphql-go/relay"
)

//go:embed index.html
var page []byte

//go:embed schema.graphql
var sdl string

type resolver struct {
	state State
}

func (r *resolver) Query() *queryResolver {
	return &queryResolver{state: &r.state}
}

func (r *resolver) Mutation() *mutationResolver {
	return &mutationResolver{state: &r.state}
}

type queryResolver struct {
	state *State
}

func (q *queryResolver) State(ctx context.Context) State {
	return *q.state
}

type mutationResolver struct {
	state *State
}

func (m *mutationResolver) State(ctx context.Context, args *struct{ State State }) State {
	*m.state = args.State
	return *m.state
}

func main() {
	opts := []graphql.SchemaOpt{graphql.UseStringDescriptions()}
	schema := graphql.MustParseSchema(sdl, &resolver{}, opts...)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write(page) })
	http.Handle("/query", &relay.Handler{Schema: schema})

	log.Fatal(http.ListenAndServe(":8080", nil))
}