示例#1
0
func TestSuccessfulParses(t *testing.T) {
	for i, in := range shouldParse {
		//d, err := parser.Parse("parser_test.go", []byte(in), parser.Debug(true))
		d, err := parser.Parse("parser_test.go", []byte(in))
		if err != nil {
			t.Errorf("case %d: %v\n%s", i+1, err, in)
		}
		_ = d
		//fmt.Println(in, "\n\n")
		//j, _ := json.MarshalIndent(d, "", " ")
		//fmt.Println(string(j), "\n\n\n\n")
	}
}
示例#2
0
文件: parser.go 项目: samuel/graphql
// ParseOperation attempts to parse a graphql.Operation from a byte slice.
func ParseOperation(query []byte) (*graphql.Operation, error) {
	result, err := parser.Parse("", query)
	if err != nil {
		return nil, ErrMalformedOperation{err}
	}
	doc, ok := result.(graphql.Document)
	if !ok {
		return nil, ErrMalformedOperation{err}
	}
	switch len(doc.Operations) {
	case 1:
		return &doc.Operations[0], nil
	case 0:
		return nil, ErrMalformedOperation{fmt.Errorf("no operations")}
	default:
		return nil, ErrMultipleOperations
	}
}