예제 #1
0
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,
	})
}
예제 #2
0
func expectInvalidRule(t *testing.T, schema *graphql.Schema, rules []graphql.ValidationRuleFn, queryString string, expectedErrors []gqlerrors.FormattedError) {
	source := source.NewSource(&source.Source{
		Body: []byte(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))
		}
	}

}
예제 #3
0
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
}
예제 #4
0
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
}
예제 #5
0
func expectValid(t *testing.T, schema *graphql.Schema, queryString string) {
	source := source.NewSource(&source.Source{
		Body: []byte(queryString),
		Name: "GraphQL request",
	})
	AST, err := parser.Parse(parser.ParseParams{Source: source})
	if err != nil {
		t.Fatalf("Unexpected error: %v", err)
	}
	validationResult := graphql.ValidateDocument(schema, AST, nil)

	if !validationResult.IsValid || len(validationResult.Errors) > 0 {
		t.Fatalf("Unexpected error: %v", validationResult.Errors)
	}

}
예제 #6
0
func expectValidRule(t *testing.T, schema *graphql.Schema, rules []graphql.ValidationRuleFn, queryString string) {
	source := source.NewSource(&source.Source{
		Body: []byte(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)
	}

}