github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/gqlhandler/gqlhandler.go (about) 1 package gqlhandler 2 3 import ( 4 "strings" 5 6 "github.com/gramework/gramework" 7 graphql "github.com/graph-gophers/graphql-go" 8 "github.com/pkg/errors" 9 ) 10 11 var ( 12 // ErrNilSchema returned when New() called with nil argument 13 ErrNilSchema = errors.New("could not initialize gqlhandler state: schema is nil") 14 ) 15 16 // State is the GQL handler state 17 type State struct { 18 NoIntrospection bool 19 schema *graphql.Schema 20 } 21 22 // New returns gql handler state based on given schema 23 func New(schema *graphql.Schema) (*State, error) { 24 if schema == nil { 25 return nil, ErrNilSchema 26 } 27 28 s := &State{ 29 schema: schema, 30 } 31 return s, nil 32 } 33 34 // Handler is the simplest 35 func (s *State) Handler(ctx *gramework.Context) { 36 if s.schema == nil { 37 ctx.Logger.Error("schema is nil") 38 ctx.Err500() 39 return 40 } 41 42 // ok, we have the schema. try to decode request 43 req, err := ctx.DecodeGQL() 44 if err != nil { 45 ctx.Logger.Warn("gql request decoding failed") 46 ctx.Error("Invalid request", 400) 47 return 48 } 49 50 if s.NoIntrospection && strings.HasPrefix(strings.TrimSpace(req.Query), "query IntrospectionQuery") { 51 ctx.Forbidden() 52 return 53 } 54 55 // check if we got invalid content type 56 if req == nil { 57 ctx.Logger.Error("GQL request is nil: invalid content type") 58 ctx.Error("Invalid content type", 400) 59 return 60 } 61 62 ctx.Logger.Info("processing request") 63 if _, err := ctx.Encode(s.schema.Exec(ctx.ToContext(), req.Query, req.OperationName, req.Variables)); err != nil { 64 ctx.SetStatusCode(415) 65 } 66 ctx.Logger.Info("processing request done") 67 }