Description
Describe the bug
It's not possible to insert an object along with its related object(s) in a single mutation. But that's exactly the beauty of GraphQL!
To Reproduce
Consider the following simple extract of a schema (Query
, update
, delete
and all other types omitted for simplicity), where basically we have a One-To-Many relationship between Post
and PostImage
tables:
type Post {
id: UUID!
content: String!
createdAt: Datetime!
postImageCollection(
first: Int
last: Int
before: Cursor
after: Cursor
filter:PostImageFilter
orderBy: [PostImageOrderBy!]
): PostImageConnection
}
type PostImage {
id: UUID!
postId: UUID!
imageUrl: String!
createdAt: Datetime!
post: Post
}
type PostImageConnection {
edges: [PostImageEdge!]!
pageInfo: PageInfo!
}
type PostImageEdge {
cursor: String!
node: PostImage!
}
input PostInsertInput {
id: UUID
content: String!
createdAt: Datetime
}
type PostInsertResponse {
affectedCount: Int!
records: [Post!]!
}
input PostImageInsertInput {
id: UUID
imageUrl: String!
postId: UUID!
createdAt: Datetime
}
type PostImageInsertResponse {
affectedCount: Int!
records: [PostImage!]!
}
type Mutation {
insertIntoPostCollection(objects: [PostInsertInput!]!): PostInsertResponse
insertIntoPostImageCollection(objects: [PostImageInsertInput!]!): PostImageInsertResponse
}
Since I usually create a PostImage
row every time I create a Post
row, I need to execute two separate mutations to achieve this.
First:
mutation CreatePost {
insertIntoPostCollection(objects: {...}) {
affectedCount
records {
id
}
}
}
And after getting the result from CreatePost
, I can execute:
mutation CreatePostImage {
insertIntoPostImageCollection(objects: {
imageUrl: "whatever",
postId: "" # here goes the Post id from previous mutation result
}) {
affectedCount
records {
id
}
}
}
Expected behavior
Expose nested objects in mutation inputs, so that I can directly create a Post
along with a PostImage
with just one mutation:
mutation CreatePost {
insertIntoPostCollection(objects: {
content: "blabla",
postImageCollection: {
objects: {
imageUrl: "whatever"
}
}
}) {
affectedCount
records {
id
content
postImageCollection(...) {
imageUrl
postId # this is automatically assigned by the mutation itself
}
}
}
See Hasura docs on this topic for reference.
Versions:
- PostgreSQL: any (this issue does not relate to Postgres itself)
- pg_graphql commit ref: b1cc167