func Do(p Params) *Result { source := source.NewSource(&source.Source{ Body: []byte(p.RequestString), Name: "GraphQL request", }) AST, err := parser.Parse(parser.ParseParams{Source: source}) if err != nil { return &Result{ Errors: gqlerrors.FormatErrors(err), } } validationResult := ValidateDocument(&p.Schema, AST, nil) if !validationResult.IsValid { return &Result{ Errors: validationResult.Errors, } } return Execute(ExecuteParams{ Schema: p.Schema, Root: p.RootObject, AST: AST, OperationName: p.OperationName, Args: p.VariableValues, Context: p.Context, }) }
func expectInvalidRule(t *testing.T, schema *graphql.Schema, rules []graphql.ValidationRuleFn, queryString string, expectedErrors []gqlerrors.FormattedError) { source := source.NewSource(&source.Source{ Body: queryString, }) AST, err := parser.Parse(parser.ParseParams{Source: source}) if err != nil { t.Fatal(err) } result := graphql.ValidateDocument(schema, AST, rules) if len(result.Errors) != len(expectedErrors) { t.Fatalf("Should have %v errors, got %v", len(expectedErrors), len(result.Errors)) } if result.IsValid != false { t.Fatalf("IsValid should be false, got %v", result.IsValid) } for _, expectedErr := range expectedErrors { found := false for _, err := range result.Errors { if reflect.DeepEqual(expectedErr, err) { found = true break } } if found == false { t.Fatalf("Unexpected result, Diff: %v", Diff(expectedErrors, result.Errors)) } } }
func parse(t *testing.T, query string) *ast.Document { astDoc, err := parser.Parse(parser.ParseParams{ Source: query, Options: parser.ParseOptions{ NoLocation: true, }, }) if err != nil { t.Fatalf("Parse failed: %v", err) } return astDoc }
func TestParse(t *testing.T, query string) *ast.Document { astDoc, err := parser.Parse(parser.ParseParams{ Source: query, Options: parser.ParseOptions{ // include source, for error reporting NoSource: false, }, }) if err != nil { t.Fatalf("Parse failed: %v", err) } return astDoc }
func expectValidRule(t *testing.T, schema *graphql.Schema, rules []graphql.ValidationRuleFn, queryString string) { source := source.NewSource(&source.Source{ Body: queryString, }) AST, err := parser.Parse(parser.ParseParams{Source: source}) if err != nil { t.Fatal(err) } result := graphql.ValidateDocument(schema, AST, rules) if len(result.Errors) > 0 { t.Fatalf("Should validate, got %v", result.Errors) } if result.IsValid != true { t.Fatalf("IsValid should be true, got %v", result.IsValid) } }
func HandleGraphQL(w http.ResponseWriter, r *http.Request) { ctx := getContext(r) startPath := r.URL.Path[:len(r.URL.Path)-9] allow := db.ReadAllowedAt(startPath, ctx.user) if !allow { json.NewEncoder(w).Encode(GraphQLResponse{ Errors: []GraphQLError{GraphQLError{"_read permission for this path needed."}}, }) return } var gql string switch r.Header.Get("Content-Type") { case "application/json": jsonBody := struct { Query string `json:"query"` }{} err := json.NewDecoder(r.Body).Decode(&jsonBody) if err != nil { json.NewEncoder(w).Encode(GraphQLResponse{ Errors: []GraphQLError{GraphQLError{"failed to parse json: " + err.Error()}}, }) return } gql = jsonBody.Query break case "application/x-www-form-urlencoded": r.ParseForm() gql = r.FormValue("query") break // case "application/graphql": default: body, err := ioutil.ReadAll(r.Body) if err != nil { json.NewEncoder(w).Encode(GraphQLResponse{ Errors: []GraphQLError{GraphQLError{"couldn't read request body."}}, }) return } gql = string(body) } doc, err := parser.Parse(parser.ParseParams{ Source: gql, Options: parser.ParseOptions{true, true}, }) if err != nil || len(doc.Definitions) != 1 { message := "your graphql query must describe a 'query' operation." if err != nil { message = err.Error() } json.NewEncoder(w).Encode(GraphQLResponse{ Errors: []GraphQLError{GraphQLError{"failed to parse gql query: " + message}}, }) return } // we're just ignoring Args for now -- maybe we'll find an utility for them in the future topfields := doc.Definitions[0].(*ast.OperationDefinition).SelectionSet.Selections response := cmap.New() godeepAsyncMultiple(topfields, startPath, &response) w.WriteHeader(200) w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(GraphQLResponse{ Data: response.Items(), }) }