@@ -25,6 +25,7 @@ export default (function PgBackwardRelationPlugin(
2525 extend,
2626 getTypeByName,
2727 pgGetGqlTypeByTypeIdAndModifier,
28+ gql2pg,
2829 pgIntrospectionResultsByKind : introspectionResultsByKind ,
2930 pgSql : sql ,
3031 getSafeAliasFromResolveInfo,
@@ -105,27 +106,47 @@ export default (function PgBackwardRelationPlugin(
105106 const isUnique = ! ! table . constraints . find (
106107 c =>
107108 ( c . type === "p" || c . type === "u" ) &&
108- c . keyAttributeNums . length == = keys . length &&
109+ c . keyAttributeNums . length < = keys . length &&
109110 c . keyAttributeNums . every ( ( n , i ) => keys [ i ] . num === n )
110111 ) ;
111-
112112 const isDeprecated = isUnique && legacyRelationMode === DEPRECATED ;
113113
114+ const primaryKeyConstraint = table . primaryKeyConstraint ;
115+ const primaryKeys =
116+ primaryKeyConstraint && primaryKeyConstraint . keyAttributes ;
117+ const uncoveredPrimaryKeys =
118+ primaryKeys && keys . every ( attr => primaryKeys . includes ( attr ) )
119+ ? primaryKeys . filter ( attr => ! keys . includes ( attr ) )
120+ : [ ] ;
121+ const parameterKeys = uncoveredPrimaryKeys . map ( key => ( {
122+ ...key ,
123+ sqlIdentifier : sql . identifier ( key . name ) ,
124+ paramName : inflection . column ( key ) , // inflection.argument(key.name, i)
125+ } ) ) ;
126+
114127 const singleRelationFieldName = isUnique
115128 ? inflection . singleRelationByKeysBackwards (
116129 keys ,
117130 table ,
118131 foreignTable ,
119132 constraint
120133 )
134+ : uncoveredPrimaryKeys . length
135+ ? inflection . rowByRelationBackwardsAndUniqueKeys (
136+ uncoveredPrimaryKeys ,
137+ table ,
138+ foreignTable ,
139+ constraint
140+ )
121141 : null ;
122142
123- const primaryKeyConstraint = table . primaryKeyConstraint ;
124- const primaryKeys =
125- primaryKeyConstraint && primaryKeyConstraint . keyAttributes ;
126-
127143 const shouldAddSingleRelation =
128- isUnique && legacyRelationMode !== ONLY ;
144+ ( isUnique && legacyRelationMode !== ONLY ) ||
145+ ( ! isUnique &&
146+ ! ! uncoveredPrimaryKeys . length &&
147+ ! omit ( table , "single" ) &&
148+ ! omit ( primaryKeyConstraint , "single" ) &&
149+ ! omit ( constraint , "single" ) ) ;
129150
130151 const shouldAddManyRelation =
131152 ! isUnique ||
@@ -145,6 +166,7 @@ export default (function PgBackwardRelationPlugin(
145166 ( {
146167 getDataFromParsedResolveInfoFragment,
147168 addDataGenerator,
169+ addArgDataGenerator,
148170 } ) => {
149171 const sqlFrom = sql . identifier ( schema . name , table . name ) ;
150172 addDataGenerator ( parsedResolveInfoFragment => {
@@ -193,12 +215,43 @@ export default (function PgBackwardRelationPlugin(
193215 } ,
194216 } ;
195217 } ) ;
218+ if ( ! isUnique ) {
219+ addArgDataGenerator ( function idArgumentsGenerator ( args ) {
220+ return {
221+ pgQuery ( queryBuilder : QueryBuilder ) {
222+ const sqlTableAlias = queryBuilder . getTableAlias ( ) ;
223+ for ( const key of parameterKeys )
224+ queryBuilder . where (
225+ sql . fragment `${ sqlTableAlias } .${
226+ key . sqlIdentifier
227+ } = ${ gql2pg (
228+ args [ key . paramName ] ,
229+ key . type ,
230+ key . typeModifier
231+ ) } `
232+ ) ;
233+ } ,
234+ } ;
235+ } ) ;
236+ }
196237 return {
197238 description :
198239 constraint . tags . backwardDescription ||
199- `Reads a single \`${ tableTypeName } \` that is related to this \`${ foreignTableTypeName } \`.` ,
240+ `${
241+ isUnique ? "Reads" : "Select"
242+ } a single \`${ tableTypeName } \` that is related to this \`${ foreignTableTypeName } \`.`,
200243 type : gqlTableType ,
201- args : { } ,
244+ args : parameterKeys . reduce ( ( memo , key ) => {
245+ memo [ key . paramName ] = {
246+ type : new GraphQLNonNull (
247+ pgGetGqlTypeByTypeIdAndModifier (
248+ key . typeId ,
249+ key . typeModifier
250+ )
251+ ) ,
252+ } ;
253+ return memo ;
254+ } , { } ) ,
202255 resolve : ( data , _args , resolveContext , resolveInfo ) => {
203256 const safeAlias = getSafeAliasFromResolveInfo (
204257 resolveInfo
0 commit comments