@@ -36,6 +36,7 @@ export default (function PgBackwardRelationPlugin(
3636 extend,
3737 getTypeByName,
3838 pgGetGqlTypeByTypeIdAndModifier,
39+ gql2pg,
3940 pgIntrospectionResultsByKind : introspectionResultsByKind ,
4041 pgSql : sql ,
4142 getSafeAliasFromResolveInfo,
@@ -131,27 +132,48 @@ export default (function PgBackwardRelationPlugin(
131132 const isUnique = ! ! table . constraints . find (
132133 c =>
133134 ( c . type === "p" || c . type === "u" ) &&
134- c . keyAttributeNums . length == = keys . length &&
135+ c . keyAttributeNums . length < = keys . length &&
135136 c . keyAttributeNums . every ( ( n , i ) => keys [ i ] . num === n )
136137 ) ;
137-
138138 const isDeprecated = isUnique && legacyRelationMode === DEPRECATED ;
139139
140+ const primaryKeyConstraint = table . primaryKeyConstraint ;
141+ const primaryKeys =
142+ primaryKeyConstraint && primaryKeyConstraint . keyAttributes ;
143+ const uncoveredPrimaryKeys =
144+ primaryKeys && keys . every ( attr => primaryKeys . includes ( attr ) )
145+ ? primaryKeys . filter ( attr => ! keys . includes ( attr ) )
146+ : [ ] ;
147+ const parameterKeys = uncoveredPrimaryKeys . map ( key => ( {
148+ ...key ,
149+ sqlIdentifier : sql . identifier ( key . name ) ,
150+ paramName : inflection . column ( key ) , // inflection.argument(key.name, i)
151+ } ) ) ;
152+
140153 const singleRelationFieldName = isUnique
141154 ? inflection . singleRelationByKeysBackwards (
142155 keys ,
143156 table ,
144157 foreignTable ,
145158 constraint
146159 )
160+ : uncoveredPrimaryKeys . length
161+ ? inflection . rowByRelationBackwardsAndUniqueKeys (
162+ uncoveredPrimaryKeys ,
163+ table ,
164+ foreignTable ,
165+ constraint
166+ )
147167 : null ;
148168
149- const primaryKeyConstraint = table . primaryKeyConstraint ;
150- const primaryKeys =
151- primaryKeyConstraint && primaryKeyConstraint . keyAttributes ;
152-
153169 const shouldAddSingleRelation =
154- isUnique && legacyRelationMode !== ONLY ;
170+ ( isUnique && legacyRelationMode !== ONLY ) ||
171+ ( ! isUnique &&
172+ ! ! uncoveredPrimaryKeys . length &&
173+ primaryKeyConstraint &&
174+ ! omit ( table , "single" ) &&
175+ ! omit ( primaryKeyConstraint , "single" ) &&
176+ ! omit ( constraint , "single" ) ) ;
155177
156178 const shouldAddManyRelation =
157179 ! isUnique ||
@@ -171,6 +193,7 @@ export default (function PgBackwardRelationPlugin(
171193 ( {
172194 getDataFromParsedResolveInfoFragment,
173195 addDataGenerator,
196+ addArgDataGenerator,
174197 } ) => {
175198 const sqlFrom = sql . identifier ( schema . name , table . name ) ;
176199 addDataGenerator ( parsedResolveInfoFragment => {
@@ -222,12 +245,43 @@ export default (function PgBackwardRelationPlugin(
222245 } ,
223246 } ;
224247 } ) ;
248+ if ( ! isUnique ) {
249+ addArgDataGenerator ( function idArgumentsGenerator ( args ) {
250+ return {
251+ pgQuery ( queryBuilder ) : void {
252+ const sqlTableAlias = queryBuilder . getTableAlias ( ) ;
253+ for ( const key of parameterKeys )
254+ queryBuilder . where (
255+ sql . fragment `${ sqlTableAlias } .${
256+ key . sqlIdentifier
257+ } = ${ gql2pg (
258+ args [ key . paramName ] ,
259+ key . type ,
260+ key . typeModifier
261+ ) } `
262+ ) ;
263+ } ,
264+ } ;
265+ } ) ;
266+ }
225267 return {
226268 description :
227269 stringTag ( constraint , "backwardDescription" ) ||
228- `Reads a single \`${ tableTypeName } \` that is related to this \`${ foreignTableTypeName } \`.` ,
270+ `${
271+ isUnique ? "Reads" : "Select"
272+ } a single \`${ tableTypeName } \` that is related to this \`${ foreignTableTypeName } \`.`,
229273 type : gqlTableType ,
230- args : { } ,
274+ args : parameterKeys . reduce ( ( memo , key ) => {
275+ const ArgType = pgGetGqlTypeByTypeIdAndModifier (
276+ key . typeId ,
277+ key . typeModifier
278+ ) ;
279+ if ( ArgType )
280+ memo [ key . paramName ] = {
281+ type : new GraphQLNonNull ( ArgType ) ,
282+ } ;
283+ return memo ;
284+ } , { } ) ,
231285 resolve : ( data , _args , resolveContext , resolveInfo ) => {
232286 const safeAlias = getSafeAliasFromResolveInfo (
233287 resolveInfo
0 commit comments