1package graphql
2
3import (
4 "context"
5
6 "github.com/graphql-go/graphql/gqlerrors"
7 "github.com/graphql-go/graphql/language/parser"
8 "github.com/graphql-go/graphql/language/source"
9)
10
11type Params struct {
12 // The GraphQL type system to use when validating and executing a query.
13 Schema Schema
14
15 // A GraphQL language formatted string representing the requested operation.
16 RequestString string
17
18 // The value provided as the first argument to resolver functions on the top
19 // level type (e.g. the query object type).
20 RootObject map[string]interface{}
21
22 // A mapping of variable name to runtime value to use for all variables
23 // defined in the requestString.
24 VariableValues map[string]interface{}
25
26 // The name of the operation to use if requestString contains multiple
27 // possible operations. Can be omitted if requestString contains only
28 // one operation.
29 OperationName string
30
31 // Context may be provided to pass application-specific per-request
32 // information to resolve functions.
33 Context context.Context
34}
35
36func Do(p Params) *Result {
37 source := source.NewSource(&source.Source{
38 Body: []byte(p.RequestString),
39 Name: "GraphQL request",
40 })
41 AST, err := parser.Parse(parser.ParseParams{Source: source})
42 if err != nil {
43 return &Result{
44 Errors: gqlerrors.FormatErrors(err),
45 }
46 }
47 validationResult := ValidateDocument(&p.Schema, AST, nil)
48
49 if !validationResult.IsValid {
50 return &Result{
51 Errors: validationResult.Errors,
52 }
53 }
54
55 return Execute(ExecuteParams{
56 Schema: p.Schema,
57 Root: p.RootObject,
58 AST: AST,
59 OperationName: p.OperationName,
60 Args: p.VariableValues,
61 Context: p.Context,
62 })
63}