Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert parent-child records in a single mutation #331

Closed
cmrajan opened this issue Feb 26, 2022 · 7 comments
Closed

Insert parent-child records in a single mutation #331

cmrajan opened this issue Feb 26, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@cmrajan
Copy link
Contributor

cmrajan commented Feb 26, 2022

What version of GraphJin are you using? graphjin version

v0.17.16

Have you tried reproducing the issue with the latest release?

Yes

Steps to reproduce the issue (config used to run GraphJin).

Pass the following mutation query

 mutation {
    order (insert: $data) {
    id 
    order_date 
    order_items {
    id 
    order_id
    item_name
    qty
    }
    }
} 

and variable as

  { 
 "data" : {
"order_date" : "2022-02-22",
"order_items":[ {"item_name":"itm1", "qty":22},{"item_name":"itm2", "qty":10}]
 }
}

Expected behaviour and actual result.

Expected to insert data and return

{
    "order":
    {
        "id": 1001,
        "order_date": "2022-02-22",
        "order_items":
        [{"id": 1,"order_id": 1001,"item_name": "itm1","qty": 22},
         {"id": 2,"order_id": 1001,"item_name": "itm2","qty": 10}
        ]
    }
}

But returns

"ERROR: cannot call populate_composite on an array (SQLSTATE 22023)"

This is useful to insert 1-many relation parent-child records and seems to be supported from https://github.com/dosco/graphjin/wiki/Guide-to-GraphQL#nested-insert or I may be mistaken.
Thanks for this excellent library and help.

@cmrajan cmrajan added the bug Something isn't working label Feb 26, 2022
@cmrajan
Copy link
Contributor Author

cmrajan commented Feb 26, 2022

possibly related to #326

@dosco
Copy link
Owner

dosco commented May 9, 2022

See if this latest release fixes it 6a020d2

@jniclas
Copy link

jniclas commented May 10, 2022

Hey, I updated to 0.17.29 and it is not working. In my case I have also one parent object with a field with multiple child objects, that are not inserted. But also no error is thrown.

        options := []*Option{
		&Option{
			Text: "option 1",
		},
		&Option{
			Text: "option 2",
		},
	}
	meeting := &Meeting{
		ID:          uuid.New(),
		Options:     options,
	}

and after an insert, only the meeting is in the database but no options

        gql := `mutation {
		meetings(insert: $data) {
		}
	}`
	variablesMap := map[string]interface{}{
		"data": meeting,
	}
	variablesJson, err := json.Marshal(variablesMap)
	if err != nil {
		panic(err)
	}
	_, gqlerr := gj.GraphQL(context.Background(), gql, variablesJson, nil)
	if gqlerr != nil {
		panic(gqlerr)
	}

(postgres schema is

CREATE TABLE meetings
(
    id           UUID NOT NULL PRIMARY KEY
);

CREATE TABLE options
(
    id          UUID NOT NULL PRIMARY KEY,
    meeting_id  UUID NOT NULL REFERENCES meetings (id) ON DELETE CASCADE,
    option_text TEXT,
);

)

@jniclas
Copy link

jniclas commented May 11, 2022

Should not be the mutation in the test TestCompileInsert/nestedInsertOneToMany in github.com/dosco/graphjin/core/internal/psql like

vars := map[string]json.RawMessage{
		"data": json.RawMessage(`{
			"email": "[email protected]",
			"full_name": "The Dude",
			"created_at": "now",
			"updated_at": "now",
			"products": 
			[
			  {
			  "name": "Pear",
			  "price": 1.70,
			  "created_at": "now",
			  "updated_at": "now"
			  },
			  {
			  "name": "Apple",
			  "price": 1.25,
			  "created_at": "now",
			  "updated_at": "now"
			  }]
		}`),
	}

and not

vars = map[string]json.RawMessage{
			"data": json.RawMessage(`{
			"email": "[email protected]",
			"full_name": "The Dude",
			"created_at": "now",
			"updated_at": "now",
			"products":
			{
				"name": "Apple",
				"price": 1.25,
				"created_at": "now",
				"updated_at": "now"
			}
		}`),
		}

like currently?

If I run the test, change the data like above and output c.w at the bottom of CompileQuery core/internal/psql/query.go:93, the insert for products disappears, but no error is thrown.

@dosco
Copy link
Owner

dosco commented May 11, 2022

Thanks for this detailed report. I'll take another look at this.

@dosco
Copy link
Owner

dosco commented May 15, 2022

Added a new test Example_insertIntoTableBulkInsertIntoRelatedTable to cover this.

@jniclas
Copy link

jniclas commented May 16, 2022

Hey, I tested 0.17.30 and it works great 👍🏻
Thank you for the quick fix!
from my side we can close the ticket

@dosco dosco closed this as completed May 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants