Skip to content

Commit

Permalink
Update parsing code to use new API
Browse files Browse the repository at this point in the history
  • Loading branch information
mangalaman93 committed Aug 20, 2019
1 parent 767e5be commit e4b1ab7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
25 changes: 14 additions & 11 deletions gql/parser_mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

// ParseMutation parses a block into a mutation. Returns an object with a mutation or
// an upsert block with mutation, otherwise returns nil with an error.
func ParseMutation(mutation string) (mu *api.Mutation, err error) {
func ParseMutation(mutation string) (req *api.Request, err error) {
var lexer lex.Lexer
lexer.Reset(mutation)
lexer.Run(lexIdentifyBlock)
Expand All @@ -39,13 +39,15 @@ func ParseMutation(mutation string) (mu *api.Mutation, err error) {
item := it.Item()
switch item.Typ {
case itemUpsertBlock:
if mu, err = parseUpsertBlock(it); err != nil {
if req, err = parseUpsertBlock(it); err != nil {
return nil, err
}
case itemLeftCurl:
if mu, err = parseMutationBlock(it); err != nil {
mu, err := parseMutationBlock(it)
if err != nil {
return nil, err
}
req = &api.Request{Mutations: []*api.Mutation{mu}}
default:
return nil, it.Errorf("Unexpected token: [%s]", item.Val)
}
Expand All @@ -55,12 +57,12 @@ func ParseMutation(mutation string) (mu *api.Mutation, err error) {
return nil, it.Errorf("Unexpected %s after the end of the block", it.Item().Val)
}

return mu, nil
return req, nil
}

// parseUpsertBlock parses the upsert block
func parseUpsertBlock(it *lex.ItemIterator) (*api.Mutation, error) {
var mu *api.Mutation
func parseUpsertBlock(it *lex.ItemIterator) (*api.Request, error) {
var req *api.Request
var queryText, condText string
var queryFound, condFound bool

Expand All @@ -80,13 +82,13 @@ func parseUpsertBlock(it *lex.ItemIterator) (*api.Mutation, error) {
switch {
// upsert {... ===>}<===
case item.Typ == itemRightCurl:
if mu == nil {
if req == nil {
return nil, it.Errorf("Empty mutation block")
} else if !queryFound {
return nil, it.Errorf("Query op not found in upsert block")
} else {
mu.Query = queryText
return mu, nil
req.Query = queryText
return req, nil
}

// upsert { mutation{...} ===>query<==={...}}
Expand Down Expand Up @@ -124,11 +126,12 @@ func parseUpsertBlock(it *lex.ItemIterator) (*api.Mutation, error) {
}

// upsert @if(...) ===>{<=== ....}
var err error
if mu, err = parseMutationBlock(it); err != nil {
mu, err := parseMutationBlock(it)
if err != nil {
return nil, err
}
mu.Cond = condText
req = &api.Request{Mutations: []*api.Mutation{mu}}

// upsert { mutation{...} ===>fragment<==={...}}
case item.Typ == itemUpsertBlockOp && item.Val == "fragment":
Expand Down
3 changes: 2 additions & 1 deletion gql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4466,8 +4466,9 @@ func TestParseMutation(t *testing.T) {
}
}
`
mu, err := ParseMutation(m)
req, err := ParseMutation(m)
require.NoError(t, err)
mu := req.Mutations[0]
require.NotNil(t, mu)
sets, err := parseNquads(mu.SetNquads)
require.NoError(t, err)
Expand Down

0 comments on commit e4b1ab7

Please sign in to comment.