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
}
|