Skip to content

Return All Inserted Rows in Many Create #15

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/PostGraphileManyCreatePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,22 @@ const PostGraphileManyCreatePlugin: T.Plugin = (
const newPayloadHookSpec = {
name: `mn${inflection.createPayloadType(table)}`,
description: `The output of our many create \`${tableTypeName}\` mutation.`,
fields: ({ fieldWithHooks }) => {
fields: ({ fieldWithHooks }: {fieldWithHooks: any}) => {
const tableName = inflection.tableFieldName(table);
const tableNamePlural = `${tableName}s`
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjie This might be of interest? It seems Postgraphile does a lot of "pluralizing", is there a function I can use besides my naive strategy of appending an s? Thanks!

Also if you look below, does type: new GraphQLList(tableType) make sense?

return {
clientMutationId: {
description:
'The exact same `clientMutationId` that was provided in the mutation input, unchanged and unused. May be used by a client to track mutations.',
type: GraphQLString
},
[tableName]: pgField(
[tableNamePlural]: pgField(
build,
fieldWithHooks,
tableName,
tableNamePlural,
{
description: `The \`${tableTypeName}\` that was created by this mutation.`,
type: tableType
description: `The \`${tableTypeName}\`s that was created by this mutation.`,
type: new GraphQLList(tableType)
},
{
isPgCreatePayloadResultField: true,
Expand Down Expand Up @@ -192,7 +193,7 @@ const PostGraphileManyCreatePlugin: T.Plugin = (
function newFieldWithHooks (): T.FieldWithHooksFunction {
return fieldWithHooks(
fieldName,
context => {
(context:any) => {
context.table = table;
context.relevantAttributes = table.attributes.filter(
attr =>
Expand All @@ -216,7 +217,7 @@ const PostGraphileManyCreatePlugin: T.Plugin = (
);
}

async function resolver (_data, args, resolveContext, resolveInfo) {
async function resolver (_data:any, args:any, resolveContext:any, resolveInfo:any) {
const { input } = args;
const {
table,
Expand Down Expand Up @@ -260,7 +261,7 @@ const PostGraphileManyCreatePlugin: T.Plugin = (
inputData.forEach((dataObj, i) => {
relevantAttributes.forEach((attr: T.PgAttribute) => {
const fieldName = inflection.column(attr);
const dataValue = dataObj[fieldName];
const dataValue = (dataObj as any)[fieldName];
// On the first run, store the attribute values
if (i === 0) {
sqlColumns.push(sql.identifier(attr.name));
Expand Down Expand Up @@ -290,18 +291,17 @@ const PostGraphileManyCreatePlugin: T.Plugin = (
)})`
: sql.fragment`default values`
} returning *`;
let row;
let rows;
try {
await pgClient.query('SAVEPOINT graphql_mutation');
const rows = await viaTemporaryTable(
rows = await viaTemporaryTable(
pgClient,
sql.identifier(table.namespace.name, table.name),
mutationQuery,
insertedRowAlias,
query
);

row = rows[0];
await pgClient.query('RELEASE SAVEPOINT graphql_mutation');
} catch (e) {
await pgClient.query('ROLLBACK TO SAVEPOINT graphql_mutation');
Expand All @@ -310,7 +310,7 @@ const PostGraphileManyCreatePlugin: T.Plugin = (

return {
clientMutationId: input.clientMutationId,
data: row
data: rows
};
}

Expand Down