Skip to content

Instantly share code, notes, and snippets.

@bkonkle
Created August 20, 2019 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bkonkle/bc45e6c543e3746026ee756739015fd4 to your computer and use it in GitHub Desktop.
Save bkonkle/bc45e6c543e3746026ee756739015fd4 to your computer and use it in GitHub Desktop.
Ensure an '$id' for every Mutation in PostGraphile
export const ensureMutationId: Plugin = builder => {
builder.hook('GraphQLObjectType:fields:field', (field, build, context) => {
const {pgSql: sql} = build
const {
scope: {
isPgUpdateMutationField,
isPgCreateMutationField,
isPgDeleteMutationField,
},
} = context
if (
!isPgUpdateMutationField &&
!isPgCreateMutationField &&
!isPgDeleteMutationField
) {
return field
}
context.addArgDataGenerator((_parsedResolveInfoFragment: any) => ({
pgQuery: (queryBuilder: QueryBuilder) => {
queryBuilder.select(
sql.fragment`${queryBuilder.getTableAlias()}.id`,
'$id'
)
},
}))
return field
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment