File: log_test.go

package info (click to toggle)
golang-github-graph-gophers-graphql-go 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,516 kB
  • sloc: sh: 373; javascript: 21; makefile: 5
file content (42 lines) | stat: -rw-r--r-- 948 bytes parent folder | download
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
package log_test

import (
	"context"
	"fmt"

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

func ExampleLoggerFunc() {
	logfn := log.LoggerFunc(func(ctx context.Context, err interface{}) {
		// Here you can handle the panic, e.g., log it or send it to an error tracking service.
		fmt.Printf("graphql: panic occurred: %v", err)
	})

	opts := []graphql.SchemaOpt{
		graphql.Logger(logfn),
		graphql.UseFieldResolvers(),
	}

	schemadef := `
		type Query {
			hello: String!
		}
	`
	resolver := &struct {
		Hello func() string
	}{
		Hello: func() string {
			// Simulate a panic
			panic("something went wrong")
		},
	}

	schema := graphql.MustParseSchema(schemadef, resolver, opts...)
	// Now, when you execute a query that causes a panic, it will be logged using the provided LoggerFunc.
	schema.Exec(context.Background(), "{ hello }", "", nil)

	// Output:
	// graphql: panic occurred: something went wrong
}