forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (77 loc) · 2.13 KB
/
main.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"errors"
"fmt"
"net/http"
"os"
"time"
"github.com/graphql-go/graphql"
handler "github.com/graphql-go/graphql-go-handler"
"github.com/newrelic/go-agent/v3/integrations/nrgraphqlgo"
"github.com/newrelic/go-agent/v3/newrelic"
)
var schema = func() graphql.Schema {
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"latestPost": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
time.Sleep(time.Second)
return "Hello World!", nil
},
},
"randomNumber": &graphql.Field{
Type: graphql.Int,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
time.Sleep(time.Second)
return 5, nil
},
},
"erroring": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
time.Sleep(time.Second)
return nil, errors.New("oooops")
},
},
},
}),
// 1. Add the nrgraphqlgo.Extension to the schema
Extensions: []graphql.Extension{nrgraphqlgo.Extension{}},
})
if err != nil {
panic(err)
}
return schema
}()
func main() {
// 2. Create the New Relic application
app, err := newrelic.NewApplication(
newrelic.ConfigAppName("Example GraphQL App"),
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY")),
newrelic.ConfigDebugLogger(os.Stdout),
)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
h := handler.New(&handler.Config{
Schema: &schema,
Pretty: true,
GraphiQL: true,
})
// 3. Make sure to instrument your HTTP handler, which will
// create/end transactions, record error codes, and add
// the transactions to the context.
http.Handle(newrelic.WrapHandle(app, "/graphql", h))
// You can test your example query with curl:
// curl -X POST \
// -H "Content-Type: application/json" \
// -d '{"query": "{latestPost, randomNumber}"}' \
// localhost:8080/graphql
http.ListenAndServe(":8080", nil)
}