github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/context_gql.go (about)

     1  package gramework
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  // DecodeGQL parses GraphQL request and returns data from it
     8  func (ctx *Context) DecodeGQL() (*GQLRequest, error) {
     9  	r := &GQLRequest{}
    10  
    11  	if string(ctx.Method()) == GET {
    12  		query := ctx.GETParam("query")
    13  		if len(query) == 0 {
    14  			return nil, ErrInvalidGQLRequest
    15  		}
    16  		r.Query = query[0]
    17  
    18  		if operationName := ctx.GETParam("operationName"); len(operationName) != 0 {
    19  			r.OperationName = operationName[0]
    20  		}
    21  
    22  		if variables := ctx.GETParam("variables"); len(variables) != 0 {
    23  			if _, err := ctx.UnJSONBytes([]byte(variables[0]), &r.Variables); err != nil {
    24  				return nil, ErrInvalidGQLRequest
    25  			}
    26  		}
    27  
    28  		return r, nil
    29  	}
    30  
    31  	ctSplitParams := strings.Split(ctx.ContentType(), delimiterCTParams)
    32  
    33  	switch ctSplitParams[0] {
    34  	case jsonCTshort:
    35  		if err := ctx.UnJSON(&r); err != nil {
    36  			return nil, err
    37  		}
    38  	case gqlCT:
    39  		r.Query = string(ctx.PostBody())
    40  	}
    41  
    42  	return r, nil
    43  }