Add insert mutation with bulk insert

This commit is contained in:
Vikram Rangnekar
2019-09-05 00:09:56 -04:00
parent 5b9105ff0c
commit c0a21e448f
30 changed files with 1080 additions and 265 deletions

View File

@ -100,19 +100,7 @@ var lexPool = sync.Pool{
}
func Parse(gql []byte) (*Operation, error) {
return parseSelectionSet(nil, gql)
}
func ParseQuery(gql []byte) (*Operation, error) {
op := opPool.Get().(*Operation)
op.Reset()
op.Type = opQuery
op.Name = ""
op.Fields = op.fieldsA[:0]
op.Args = op.argsA[:0]
return parseSelectionSet(op, gql)
return parseSelectionSet(gql)
}
func ParseArgValue(argVal string) (*Node, error) {
@ -134,7 +122,7 @@ func ParseArgValue(argVal string) (*Node, error) {
return op, err
}
func parseSelectionSet(op *Operation, gql []byte) (*Operation, error) {
func parseSelectionSet(gql []byte) (*Operation, error) {
var err error
if len(gql) == 0 {
@ -154,14 +142,28 @@ func parseSelectionSet(op *Operation, gql []byte) (*Operation, error) {
items: l.items,
}
if op == nil {
op, err = p.parseOp()
} else {
if p.peek(itemObjOpen) {
p.ignore()
}
var op *Operation
if p.peek(itemObjOpen) {
p.ignore()
}
if p.peek(itemName) {
op = opPool.Get().(*Operation)
op.Reset()
op.Type = opQuery
op.Name = ""
op.Fields = op.fieldsA[:0]
op.Args = op.argsA[:0]
op.Fields, err = p.parseFields(op.Fields)
} else {
op, err = p.parseOp()
if err != nil {
return nil, err
}
}
lexPool.Put(l)

View File

@ -45,10 +45,10 @@ func compareOp(op1, op2 Operation) error {
}
*/
func TestCompile(t *testing.T) {
func TestCompile1(t *testing.T) {
qcompile, _ := NewCompiler(Config{})
_, err := qcompile.CompileQuery([]byte(`
_, err := qcompile.Compile([]byte(`
product(id: 15) {
id
name
@ -59,9 +59,39 @@ func TestCompile(t *testing.T) {
}
}
func TestCompile2(t *testing.T) {
qcompile, _ := NewCompiler(Config{})
_, err := qcompile.Compile([]byte(`
query { product(id: 15) {
id
name
} }`))
if err != nil {
t.Fatal(err)
}
}
func TestCompile3(t *testing.T) {
qcompile, _ := NewCompiler(Config{})
_, err := qcompile.Compile([]byte(`
mutation {
product(id: 15, name: "Test") {
id
name
}
}`))
if err != nil {
t.Fatal(err)
}
}
func TestInvalidCompile1(t *testing.T) {
qcompile, _ := NewCompiler(Config{})
_, err := qcompile.CompileQuery([]byte(`#`))
_, err := qcompile.Compile([]byte(`#`))
if err == nil {
t.Fatal(errors.New("expecting an error"))
@ -70,7 +100,7 @@ func TestInvalidCompile1(t *testing.T) {
func TestInvalidCompile2(t *testing.T) {
qcompile, _ := NewCompiler(Config{})
_, err := qcompile.CompileQuery([]byte(`{u(where:{not:0})}`))
_, err := qcompile.Compile([]byte(`{u(where:{not:0})}`))
if err == nil {
t.Fatal(errors.New("expecting an error"))
@ -79,7 +109,7 @@ func TestInvalidCompile2(t *testing.T) {
func TestEmptyCompile(t *testing.T) {
qcompile, _ := NewCompiler(Config{})
_, err := qcompile.CompileQuery([]byte(``))
_, err := qcompile.Compile([]byte(``))
if err == nil {
t.Fatal(errors.New("expecting an error"))
@ -114,7 +144,7 @@ func BenchmarkQCompile(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
_, err := qcompile.CompileQuery(gql)
_, err := qcompile.Compile(gql)
if err != nil {
b.Fatal(err)
@ -130,7 +160,7 @@ func BenchmarkQCompileP(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := qcompile.CompileQuery(gql)
_, err := qcompile.Compile(gql)
if err != nil {
b.Fatal(err)

View File

@ -10,15 +10,17 @@ import (
"github.com/gobuffalo/flect"
)
type QType int
const (
maxSelectors = 30
QTQuery QType = iota + 1
QTMutation
)
type QCode struct {
Query *Query
}
type Query struct {
Type QType
Selects []Select
}
@ -149,6 +151,11 @@ type Compiler struct {
ka bool
}
var opMap = map[parserType]QType{
opQuery: QTQuery,
opMutate: QTMutation,
}
var expPool = sync.Pool{
New: func() interface{} { return new(Exp) },
}
@ -196,44 +203,23 @@ func (com *Compiler) Compile(query []byte) (*QCode, error) {
return nil, err
}
switch op.Type {
case opQuery:
qc.Query, err = com.compileQuery(op)
case opMutate:
case opSub:
default:
err = fmt.Errorf("Unknown operation type %d", op.Type)
}
qc.Selects, err = com.compileQuery(op)
if err != nil {
return nil, err
}
if t, ok := opMap[op.Type]; ok {
qc.Type = t
} else {
return nil, fmt.Errorf("Unknown operation type %d", op.Type)
}
opPool.Put(op)
return &qc, nil
}
func (com *Compiler) CompileQuery(query []byte) (*QCode, error) {
var err error
op, err := ParseQuery(query)
if err != nil {
return nil, err
}
qc := &QCode{}
qc.Query, err = com.compileQuery(op)
opPool.Put(op)
if err != nil {
return nil, err
}
return qc, nil
}
func (com *Compiler) compileQuery(op *Operation) (*Query, error) {
func (com *Compiler) compileQuery(op *Operation) ([]Select, error) {
id := int32(0)
parentID := int32(0)
@ -344,7 +330,7 @@ func (com *Compiler) compileQuery(op *Operation) (*Query, error) {
return nil, errors.New("invalid query")
}
return &Query{selects[:id]}, nil
return selects[:id], nil
}
func (com *Compiler) compileArgs(sel *Select, args []Arg) error {
@ -661,14 +647,6 @@ func (com *Compiler) compileArgOffset(sel *Select, arg *Arg) error {
return nil
}
func compileMutate() (*Query, error) {
return nil, nil
}
func compileSub() (*Query, error) {
return nil, nil
}
func newExp(st *util.Stack, node *Node, usePool bool) (*Exp, error) {
name := node.Name
if name[0] == '_' {