Skip to content

Commit

Permalink
fix(postgres): fix empty insert case (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebmer authored Sep 21, 2016
1 parent dff9f23 commit f9af3f9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/graphql/mutation/createInsertMutationField.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,18 @@ const resolveInsert = table => {
.filter(([, value]) => value)
)

// Insert the thing making sure we return the newly inserted row.
const { rows: [row] } = await client.queryAsync(
const query = (
new SQLBuilder()
.add(`insert into ${table.getIdentifier()}`)
.add(`(${valueEntries.map(([column]) => `"${column.name}"`).join(', ')})`)
.add('values')
.add(`(${valueEntries.map(constant('$')).join(', ')})`, valueEntries.map(([, value]) => value))
.add(valueEntries.length === 0 ? '' : `(${valueEntries.map(([column]) => `"${column.name}"`).join(', ')})`)
.add(valueEntries.length === 0 ? 'default values' : 'values')
.add(valueEntries.length === 0 ? '' : `(${valueEntries.map(constant('$')).join(', ')})`, valueEntries.map(([, value]) => value))
.add('returning *')
)

// Insert the thing making sure we return the newly inserted row.
const { rows: [row] } = await client.queryAsync(query)

const output = row ? (row[$$rowTable] = table, row) : null

return {
Expand Down
1 change: 1 addition & 0 deletions tests/integration/fixtures/mutation-insert.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mutation Insert {
a: insertThing(input: { note: "a" }) { ...payloadThing }
b: insertThing(input: { rowId: 9, note: "b", clientMutationId: "hello" }) { ...payloadThing }
c: insertRelation(input: { aThingId: 8, bThingId: 9 }) { ...payloadRelation }
d: insertAnythingGoes(input: {}) { anythingGoes { foo bar } }
}

fragment payloadThing on InsertThingPayload {
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/fixtures/mutation-insert.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
}
}
}
},
"d": {
"anythingGoes": {
"foo": null,
"bar": null
}
}
}
}

0 comments on commit f9af3f9

Please sign in to comment.