github.com/cayleygraph/cayley@v0.7.7/query/graphql/http.go (about)

     1  package graphql
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"io"
     7  
     8  	"github.com/dennwc/graphql/gqlerrors"
     9  
    10  	"github.com/cayleygraph/cayley/graph"
    11  	"github.com/cayleygraph/cayley/query"
    12  )
    13  
    14  type httpResult struct {
    15  	Data   interface{}                `json:"data"`
    16  	Errors []gqlerrors.FormattedError `json:"errors,omitempty"`
    17  }
    18  
    19  func httpError(w query.ResponseWriter, err error) {
    20  	json.NewEncoder(w).Encode(httpResult{
    21  		Errors: []gqlerrors.FormattedError{{
    22  			Message: err.Error(),
    23  		}},
    24  	})
    25  }
    26  
    27  func httpQuery(ctx context.Context, qs graph.QuadStore, w query.ResponseWriter, r io.Reader) {
    28  	q, err := Parse(r)
    29  	if err != nil {
    30  		httpError(w, err)
    31  		return
    32  	}
    33  	m, err := q.Execute(ctx, qs)
    34  	if err != nil {
    35  		httpError(w, err)
    36  		return
    37  	}
    38  	json.NewEncoder(w).Encode(httpResult{Data: m})
    39  }