Created
August 20, 2019 20:41
-
-
Save bkonkle/bc45e6c543e3746026ee756739015fd4 to your computer and use it in GitHub Desktop.
Ensure an '$id' for every Mutation in PostGraphile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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