Skip to content

Commit

Permalink
Fix return values for bulk mutations and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Oct 3, 2019
1 parent a8ad871 commit 50ba673
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 93 deletions.
72 changes: 51 additions & 21 deletions config/allow.list
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
# http://localhost:8080/

query {
me {
variables {
"update": {
"name": "Wu-Tang",
"description": "No description needed"
},
"product_id": 1
}

mutation {
products(id: $product_id, update: $update) {
id
email
full_name
name
description
}
}

variables {
"update": {
"name": "Hellooooo",
"description": "World",
"data": {
"email": "[email protected]",
"full_name": "Ghostface Killah",
"created_at": "now",
"updated_at": "now"
},
"user": 123
}
}

mutation {
products(update: $update, where: {id: {eq: 134}}) {
user(insert: $data) {
id
}
}

variables {
"data": {
"product_id": 5
}
}

mutation {
products(id: $product_id, delete: true) {
id
name
description
}
}

Expand All @@ -39,20 +57,33 @@ query {
}
}


variables {
"update": {
"name": "Hellooooo",
"description": "World"
},
"user": 123
"data": [
{
"name": "Protect Ya Neck",
"created_at": "now",
"updated_at": "now"
},
{
"name": "Enter the Wu-Tang",
"created_at": "now",
"updated_at": "now"
}
]
}

mutation {
products(update: $update, where: {id: {eq: 134}}) {
products(insert: $data) {
id
name
description
}
}

query {
me {
id
email
full_name
}
}

Expand All @@ -70,5 +101,4 @@ mutation {
name
description
}
}

}
5 changes: 4 additions & 1 deletion psql/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ func (c *compilerContext) renderDelete(qc *qcode.QCode, w *bytes.Buffer, vars Va
return 0, err
}

c.w.WriteString(`DELETE FROM `)
c.w.WriteString(`WITH `)
quoted(c.w, ti.Name)

c.w.WriteString(` AS (DELETE FROM `)
c.w.WriteString(ti.Name)
io.WriteString(c.w, ` WHERE `)

Expand Down
10 changes: 5 additions & 5 deletions psql/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func simpleInsert(t *testing.T) {
}
}`

sql := `WITH "users" AS (WITH "input" AS (SELECT {{data}}::json AS j) INSERT INTO users (full_name, email) SELECT full_name, email FROM input i, json_populate_record(NULL::users, i.j) t RETURNING *) SELECT json_object_agg('user', sel_json_0) FROM (SELECT row_to_json((SELECT "sel_0" FROM (SELECT "users_0"."id" AS "id") AS "sel_0")) AS "sel_json_0" FROM (SELECT "users"."id" FROM "users" LIMIT ('1') :: integer) AS "users_0") AS "done_1337";`
sql := `WITH "users" AS (WITH "input" AS (SELECT {{data}}::json AS j) INSERT INTO users (full_name, email) SELECT full_name, email FROM input i, json_populate_record(NULL::users, i.j) t RETURNING *) SELECT json_object_agg('user', sel_json_0) FROM (SELECT row_to_json((SELECT "sel_0" FROM (SELECT "users_0"."id" AS "id") AS "sel_0")) AS "sel_json_0" FROM (SELECT "users"."id" FROM "users") AS "users_0") AS "done_1337";`

vars := map[string]json.RawMessage{
"data": json.RawMessage(`{"email": "[email protected]", "full_name": "Flo Barton"}`),
Expand All @@ -36,7 +36,7 @@ func singleInsert(t *testing.T) {
}
}`

sql := `WITH "products" AS (WITH "input" AS (SELECT {{insert}}::json AS j) INSERT INTO products (name, description) SELECT name, description FROM input i, json_populate_record(NULL::products, i.j) t RETURNING *) SELECT json_object_agg('product', sel_json_0) FROM (SELECT row_to_json((SELECT "sel_0" FROM (SELECT "products_0"."id" AS "id", "products_0"."name" AS "name") AS "sel_0")) AS "sel_json_0" FROM (SELECT "products"."id", "products"."name" FROM "products" LIMIT ('1') :: integer) AS "products_0") AS "done_1337";`
sql := `WITH "products" AS (WITH "input" AS (SELECT {{insert}}::json AS j) INSERT INTO products (name, description) SELECT name, description FROM input i, json_populate_record(NULL::products, i.j) t RETURNING *) SELECT json_object_agg('product', sel_json_0) FROM (SELECT row_to_json((SELECT "sel_0" FROM (SELECT "products_0"."id" AS "id", "products_0"."name" AS "name") AS "sel_0")) AS "sel_json_0" FROM (SELECT "products"."id", "products"."name" FROM "products") AS "products_0") AS "done_1337";`

vars := map[string]json.RawMessage{
"insert": json.RawMessage(` { "name": "my_name", "woo": { "hoo": "goo" }, "description": "my_desc" }`),
Expand All @@ -60,7 +60,7 @@ func bulkInsert(t *testing.T) {
}
}`

sql := `WITH "products" AS (WITH "input" AS (SELECT {{insert}}::json AS j) INSERT INTO products (name, description) SELECT name, description FROM input i, json_populate_recordset(NULL::products, i.j) t RETURNING *) SELECT json_object_agg('product', sel_json_0) FROM (SELECT row_to_json((SELECT "sel_0" FROM (SELECT "products_0"."id" AS "id", "products_0"."name" AS "name") AS "sel_0")) AS "sel_json_0" FROM (SELECT "products"."id", "products"."name" FROM "products" LIMIT ('1') :: integer) AS "products_0") AS "done_1337";`
sql := `WITH "products" AS (WITH "input" AS (SELECT {{insert}}::json AS j) INSERT INTO products (name, description) SELECT name, description FROM input i, json_populate_recordset(NULL::products, i.j) t RETURNING *) SELECT json_object_agg('product', sel_json_0) FROM (SELECT row_to_json((SELECT "sel_0" FROM (SELECT "products_0"."id" AS "id", "products_0"."name" AS "name") AS "sel_0")) AS "sel_json_0" FROM (SELECT "products"."id", "products"."name" FROM "products") AS "products_0") AS "done_1337";`

vars := map[string]json.RawMessage{
"insert": json.RawMessage(` [{ "name": "my_name", "woo": { "hoo": "goo" }, "description": "my_desc" }]`),
Expand All @@ -84,7 +84,7 @@ func singleUpdate(t *testing.T) {
}
}`

sql := `WITH "products" AS (WITH "input" AS (SELECT {{update}}::json AS j) UPDATE products SET (name, description) = (SELECT name, description FROM input i, json_populate_record(NULL::products, i.j) t) WHERE (("products"."price") > 0) AND (("products"."price") < 8) AND (("products"."id") = 1) AND (("products"."id") = 15) RETURNING *) SELECT json_object_agg('product', sel_json_0) FROM (SELECT row_to_json((SELECT "sel_0" FROM (SELECT "products_0"."id" AS "id", "products_0"."name" AS "name") AS "sel_0")) AS "sel_json_0" FROM (SELECT "products"."id", "products"."name" FROM "products" LIMIT ('1') :: integer) AS "products_0") AS "done_1337";`
sql := `WITH "products" AS (WITH "input" AS (SELECT {{update}}::json AS j) UPDATE products SET (name, description) = (SELECT name, description FROM input i, json_populate_record(NULL::products, i.j) t) WHERE (("products"."price") > 0) AND (("products"."price") < 8) AND (("products"."id") = 1) AND (("products"."id") = 15) RETURNING *) SELECT json_object_agg('product', sel_json_0) FROM (SELECT row_to_json((SELECT "sel_0" FROM (SELECT "products_0"."id" AS "id", "products_0"."name" AS "name") AS "sel_0")) AS "sel_json_0" FROM (SELECT "products"."id", "products"."name" FROM "products") AS "products_0") AS "done_1337";`

vars := map[string]json.RawMessage{
"update": json.RawMessage(` { "name": "my_name", "woo": { "hoo": "goo" }, "description": "my_desc" }`),
Expand All @@ -108,7 +108,7 @@ func delete(t *testing.T) {
}
}`

sql := `DELETE FROM products WHERE (("products"."price") > 0) AND (("products"."price") < 8) AND (("products"."id") = 1) RETURNING *) SELECT json_object_agg('product', sel_json_0) FROM (SELECT row_to_json((SELECT "sel_0" FROM (SELECT "products_0"."id" AS "id", "products_0"."name" AS "name") AS "sel_0")) AS "sel_json_0" FROM (SELECT "products"."id", "products"."name" FROM "products" LIMIT ('1') :: integer) AS "products_0") AS "done_1337";`
sql := `WITH "products" AS (DELETE FROM products WHERE (("products"."price") > 0) AND (("products"."price") < 8) AND (("products"."id") = 1) RETURNING *) SELECT json_object_agg('product', sel_json_0) FROM (SELECT row_to_json((SELECT "sel_0" FROM (SELECT "products_0"."id" AS "id", "products_0"."name" AS "name") AS "sel_0")) AS "sel_json_0" FROM (SELECT "products"."id", "products"."name" FROM "products") AS "products_0") AS "done_1337";`

vars := map[string]json.RawMessage{
"update": json.RawMessage(` { "name": "my_name", "woo": { "hoo": "goo" }, "description": "my_desc" }`),
Expand Down
20 changes: 11 additions & 9 deletions psql/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,17 +570,19 @@ func (c *compilerContext) renderBaseSelect(sel *qcode.Select, ti *DBTableInfo,
}
}

if len(sel.Paging.Limit) != 0 {
//fmt.Fprintf(w, ` LIMIT ('%s') :: integer`, c.sel.Paging.Limit)
c.w.WriteString(` LIMIT ('`)
c.w.WriteString(sel.Paging.Limit)
c.w.WriteString(`') :: integer`)
if sel.Action == 0 {
if len(sel.Paging.Limit) != 0 {
//fmt.Fprintf(w, ` LIMIT ('%s') :: integer`, c.sel.Paging.Limit)
c.w.WriteString(` LIMIT ('`)
c.w.WriteString(sel.Paging.Limit)
c.w.WriteString(`') :: integer`)

} else if ti.Singular {
c.w.WriteString(` LIMIT ('1') :: integer`)
} else if ti.Singular {
c.w.WriteString(` LIMIT ('1') :: integer`)

} else {
c.w.WriteString(` LIMIT ('20') :: integer`)
} else {
c.w.WriteString(` LIMIT ('20') :: integer`)
}
}

if len(sel.Paging.Offset) != 0 {
Expand Down
57 changes: 0 additions & 57 deletions test.yml

This file was deleted.

0 comments on commit 50ba673

Please sign in to comment.