Esempio n. 1
0
func Graphql(p Params, resultChannel chan *Result) {
	source := source.NewSource(&source.Source{
		Body: p.RequestString,
		Name: "GraphQL request",
	})
	AST, err := parser.Parse(parser.ParseParams{Source: source})
	if err != nil {
		result := Result{
			Errors: gqlerrors.FormatErrors(err),
		}
		resultChannel <- &result
		return
	}
	validationResult := ValidateDocument(p.Schema, AST)

	if !validationResult.IsValid {
		result := Result{
			Errors: validationResult.Errors,
		}
		resultChannel <- &result
		return
	} else {
		ep := ExecuteParams{
			Schema:        p.Schema,
			Root:          p.RootObject,
			AST:           AST,
			OperationName: p.OperationName,
			Args:          p.VariableValues,
		}
		Execute(ep, resultChannel)
		return
	}
}
Esempio n. 2
0
func TestParseProvidesUsefulErrorsWhenUsingSource(t *testing.T) {
	test := errorMessageTest{
		source.NewSource(&source.Source{Body: "query", Name: "MyQuery.graphql"}),
		`Syntax Error MyQuery.graphql (1:6) Expected Name, found EOF`,
		false,
	}
	testErrorMessage(t, test)
}
Esempio n. 3
0
func Parse(p ParseParams) (*ast.Document, error) {
	var sourceObj *source.Source
	switch p.Source.(type) {
	case *source.Source:
		sourceObj = p.Source.(*source.Source)
	default:
		body, _ := p.Source.(string)
		sourceObj = source.NewSource(&source.Source{Body: body})
	}
	parser, err := makeParser(sourceObj, p.Options)
	if err != nil {
		return nil, err
	}
	doc, err := parseDocument(parser)
	if err != nil {
		return nil, err
	}
	return doc, nil
}
Esempio n. 4
0
// TODO: test and expose parseValue as a public
func parseValue(p ParseParams) (ast.Value, error) {
	var value ast.Value
	var sourceObj *source.Source
	switch p.Source.(type) {
	case *source.Source:
		sourceObj = p.Source.(*source.Source)
	default:
		body, _ := p.Source.(string)
		sourceObj = source.NewSource(&source.Source{Body: body})
	}
	parser, err := makeParser(sourceObj, p.Options)
	if err != nil {
		return value, err
	}
	value, err = parseValueLiteral(parser, false)
	if err != nil {
		return value, err
	}
	return value, nil
}
Esempio n. 5
0
func createSource(body string) *source.Source {
	return source.NewSource(&source.Source{Body: body})
}
Esempio n. 6
0
func TestParseCreatesAst(t *testing.T) {
	body := `{
  node(id: 4) {
    id,
    name
  }
}
`
	source := source.NewSource(&source.Source{Body: body})
	document, err := Parse(
		ParseParams{
			Source: source,
			Options: ParseOptions{
				NoSource: true,
			},
		},
	)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	oDef := ast.OperationDefinition{
		Kind: "OperationDefinition",
		Loc: &ast.Location{
			Start: 0, End: 40,
		},
		Operation:  "query",
		Directives: []*ast.Directive{},
		SelectionSet: &ast.SelectionSet{
			Kind: "SelectionSet",
			Loc: &ast.Location{
				Start: 0, End: 40,
			},
			Selections: []ast.Selection{
				&ast.Field{
					Kind: "Field",
					Loc: &ast.Location{
						Start: 4, End: 38,
					},
					Name: &ast.Name{
						Kind: "Name",
						Loc: &ast.Location{
							Start: 4, End: 8,
						},
						Value: "node",
					},
					Arguments: []*ast.Argument{
						{
							Kind: "Argument",
							Name: &ast.Name{
								Kind: "Name",
								Loc: &ast.Location{
									Start: 9, End: 11,
								},
								Value: "id",
							},
							Value: &ast.IntValue{
								Kind: "IntValue",
								Loc: &ast.Location{
									Start: 13, End: 14,
								},
								Value: "4",
							},
							Loc: &ast.Location{
								Start: 9, End: 14,
							},
						},
					},
					Directives: []*ast.Directive{},
					SelectionSet: &ast.SelectionSet{
						Kind: "SelectionSet",
						Loc: &ast.Location{
							Start: 16, End: 38,
						},
						Selections: []ast.Selection{
							&ast.Field{
								Kind: "Field",
								Loc: &ast.Location{
									Start: 22, End: 24,
								},
								Name: &ast.Name{
									Kind: "Name",
									Loc: &ast.Location{
										Start: 22, End: 24,
									},
									Value: "id",
								},
								Arguments:    []*ast.Argument{},
								Directives:   []*ast.Directive{},
								SelectionSet: nil,
							},
							&ast.Field{
								Kind: "Field",
								Loc: &ast.Location{
									Start: 30, End: 34,
								},
								Name: &ast.Name{
									Kind: "Name",
									Loc: &ast.Location{
										Start: 30, End: 34,
									},
									Value: "name",
								},
								Arguments:    []*ast.Argument{},
								Directives:   []*ast.Directive{},
								SelectionSet: nil,
							},
						},
					},
				},
			},
		},
	}
	expectedDocument := ast.NewDocument(&ast.Document{
		Loc: &ast.Location{
			Start: 0, End: 41,
		},
		Definitions: []ast.Node{&oDef},
	})
	if !reflect.DeepEqual(document, expectedDocument) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expectedDocument, document.Definitions)
	}

}