-
-
Notifications
You must be signed in to change notification settings - Fork 131
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
feat(pg) single backward relations with arguments if primary key covered partially #567
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ import debugFactory from "debug"; | |||||
|
||||||
import { Plugin } from "graphile-build"; | ||||||
import { stringTag } from "./PgBasicsPlugin"; | ||||||
import { PgEntityKind } from "./PgIntrospectionPlugin"; | ||||||
import { PgEntityKind, PgConstraint } from "./PgIntrospectionPlugin"; | ||||||
|
||||||
declare module "graphile-build" { | ||||||
interface GraphileBuildOptions { | ||||||
|
@@ -36,6 +36,7 @@ export default (function PgBackwardRelationPlugin( | |||||
extend, | ||||||
getTypeByName, | ||||||
pgGetGqlTypeByTypeIdAndModifier, | ||||||
gql2pg, | ||||||
pgIntrospectionResultsByKind: introspectionResultsByKind, | ||||||
pgSql: sql, | ||||||
getSafeAliasFromResolveInfo, | ||||||
|
@@ -131,27 +132,49 @@ export default (function PgBackwardRelationPlugin( | |||||
const isUnique = !!table.constraints.find( | ||||||
c => | ||||||
(c.type === "p" || c.type === "u") && | ||||||
c.keyAttributeNums.length === keys.length && | ||||||
c.keyAttributeNums.length <= keys.length && | ||||||
c.keyAttributeNums.every((n, i) => keys[i].num === n) | ||||||
); | ||||||
|
||||||
const isDeprecated = isUnique && legacyRelationMode === DEPRECATED; | ||||||
|
||||||
const primaryKeyConstraint = table.primaryKeyConstraint; | ||||||
const primaryKeys = | ||||||
primaryKeyConstraint && primaryKeyConstraint.keyAttributes; | ||||||
const uncoveredPrimaryKeys = | ||||||
primaryKeys && keys.every(attr => primaryKeys.includes(attr)) | ||||||
? primaryKeys.filter(attr => !keys.includes(attr)) | ||||||
: []; | ||||||
const parameterKeys = uncoveredPrimaryKeys.map(key => ({ | ||||||
...key, | ||||||
sqlIdentifier: sql.identifier(key.name), | ||||||
paramName: inflection.column(key), // inflection.argument(key.name, i) | ||||||
})); | ||||||
|
||||||
const singleRelationFieldName = isUnique | ||||||
? inflection.singleRelationByKeysBackwards( | ||||||
keys, | ||||||
table, | ||||||
foreignTable, | ||||||
constraint | ||||||
) | ||||||
: uncoveredPrimaryKeys.length | ||||||
? inflection.rowByRelationBackwardsAndUniqueKeys( | ||||||
uncoveredPrimaryKeys, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
table, | ||||||
foreignTable, | ||||||
constraint, | ||||||
primaryKeyConstraint as PgConstraint // not void | ||||||
) | ||||||
: null; | ||||||
|
||||||
const primaryKeyConstraint = table.primaryKeyConstraint; | ||||||
const primaryKeys = | ||||||
primaryKeyConstraint && primaryKeyConstraint.keyAttributes; | ||||||
|
||||||
const shouldAddSingleRelation = | ||||||
isUnique && legacyRelationMode !== ONLY; | ||||||
(isUnique && legacyRelationMode !== ONLY) || | ||||||
(!isUnique && | ||||||
!!uncoveredPrimaryKeys.length && | ||||||
primaryKeyConstraint && | ||||||
!omit(table, "single") && | ||||||
!omit(primaryKeyConstraint, "single") && | ||||||
!omit(constraint, "single")); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a lot of potential |
||||||
|
||||||
const shouldAddManyRelation = | ||||||
!isUnique || | ||||||
|
@@ -171,6 +194,7 @@ export default (function PgBackwardRelationPlugin( | |||||
({ | ||||||
getDataFromParsedResolveInfoFragment, | ||||||
addDataGenerator, | ||||||
addArgDataGenerator, | ||||||
}) => { | ||||||
const sqlFrom = sql.identifier(schema.name, table.name); | ||||||
addDataGenerator(parsedResolveInfoFragment => { | ||||||
|
@@ -222,12 +246,43 @@ export default (function PgBackwardRelationPlugin( | |||||
}, | ||||||
}; | ||||||
}); | ||||||
if (!isUnique) { | ||||||
addArgDataGenerator(function idArgumentsGenerator(args) { | ||||||
return { | ||||||
pgQuery(queryBuilder): void { | ||||||
const sqlTableAlias = queryBuilder.getTableAlias(); | ||||||
for (const key of parameterKeys) | ||||||
queryBuilder.where( | ||||||
sql.fragment`${sqlTableAlias}.${ | ||||||
key.sqlIdentifier | ||||||
} = ${gql2pg( | ||||||
args[key.paramName], | ||||||
key.type, | ||||||
key.typeModifier | ||||||
)}` | ||||||
); | ||||||
}, | ||||||
}; | ||||||
}); | ||||||
} | ||||||
return { | ||||||
description: | ||||||
stringTag(constraint, "backwardDescription") || | ||||||
`Reads a single \`${tableTypeName}\` that is related to this \`${foreignTableTypeName}\`.`, | ||||||
`${ | ||||||
isUnique ? "Reads" : "Select" | ||||||
} a single \`${tableTypeName}\` that is related to this \`${foreignTableTypeName}\`.`, | ||||||
type: gqlTableType, | ||||||
args: {}, | ||||||
args: parameterKeys.reduce((memo, key) => { | ||||||
const ArgType = pgGetGqlTypeByTypeIdAndModifier( | ||||||
key.typeId, | ||||||
key.typeModifier | ||||||
); | ||||||
if (ArgType) | ||||||
memo[key.paramName] = { | ||||||
type: new GraphQLNonNull(ArgType), | ||||||
}; | ||||||
return memo; | ||||||
}, {}), | ||||||
resolve: (data, _args, _resolveContext, resolveInfo) => { | ||||||
const safeAlias = getSafeAliasFromResolveInfo( | ||||||
resolveInfo | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's less risk of conflicts here if we don't splat this out: