Skip to content

Commit 280f809

Browse files
committed
fix: graphql localized relationship bugs
1 parent 035f6c6 commit 280f809

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

src/graphql/schema/buildObjectType.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ function buildObjectType(name: string, fields: Field[], parentName: string, base
101101
extensions: { complexity: 20 },
102102
async resolve(parent, args, context) {
103103
const value = parent[field.name];
104-
const locale = args.locale || context.locale;
105-
const fallbackLocale = args.fallbackLocale || context.fallbackLocale;
104+
const locale = args.locale || context.req.locale;
105+
const fallbackLocale = args.fallbackLocale || context.req.fallbackLocale;
106106

107107
let id = value;
108108

@@ -229,12 +229,6 @@ function buildObjectType(name: string, fields: Field[], parentName: string, base
229229

230230
const types = relationTo.map((relation) => this.collections[relation].graphQL.type);
231231

232-
let resolveType = function resolveType(data) {
233-
return this.collections[data.collection].graphQL.type.name;
234-
};
235-
236-
resolveType = resolveType.bind(this);
237-
238232
type = new GraphQLObjectType({
239233
name: `${relationshipName}_Relationship`,
240234
fields: {
@@ -245,7 +239,9 @@ function buildObjectType(name: string, fields: Field[], parentName: string, base
245239
type: new GraphQLUnionType({
246240
name: relationshipName,
247241
types,
248-
resolveType,
242+
async resolveType(data, { req: { payload } }) {
243+
return payload.collections[data.collection].graphQL.type.name;
244+
},
249245
}),
250246
},
251247
},
@@ -294,24 +290,25 @@ function buildObjectType(name: string, fields: Field[], parentName: string, base
294290
extensions: { complexity: 10 },
295291
async resolve(parent, args, context) {
296292
const value = parent[field.name];
297-
const locale = args.locale || context.locale;
298-
const fallbackLocale = args.fallbackLocale || context.fallbackLocale;
293+
const locale = args.locale || context.req.locale;
294+
const fallbackLocale = args.fallbackLocale || context.req.fallbackLocale;
299295
let relatedCollectionSlug = field.relationTo;
300296

301297
if (hasManyValues) {
302298
const results = [];
303299
const resultPromises = [];
304300

305-
const createPopulationPromise = async (relatedDoc) => {
301+
const createPopulationPromise = async (relatedDoc, i) => {
306302
let id = relatedDoc;
303+
let collectionSlug = field.relationTo;
307304

308305
if (isRelatedToManyCollections) {
309-
relatedCollectionSlug = relatedDoc.relationTo;
306+
collectionSlug = relatedDoc.relationTo;
310307
id = relatedDoc.value;
311308
}
312309

313310
const result = await find({
314-
collection: collections[relatedCollectionSlug as string],
311+
collection: collections[collectionSlug as string],
315312
where: {
316313
...(args.where || {}),
317314
_id: {
@@ -329,22 +326,22 @@ function buildObjectType(name: string, fields: Field[], parentName: string, base
329326

330327
if (result.docs.length === 1) {
331328
if (isRelatedToManyCollections) {
332-
results.push({
333-
relationTo: relatedCollectionSlug,
329+
results[i] = {
330+
relationTo: collectionSlug,
334331
value: {
335332
...result.docs[0],
336-
collection: relatedCollectionSlug,
333+
collection: collectionSlug,
337334
},
338-
});
335+
};
339336
} else {
340-
results.push(result.docs[0]);
337+
[results[i]] = result.docs;
341338
}
342339
}
343340
};
344341

345342
if (value) {
346-
value.forEach((relatedDoc) => {
347-
resultPromises.push(createPopulationPromise(relatedDoc));
343+
value.forEach((relatedDoc, i) => {
344+
resultPromises.push(createPopulationPromise(relatedDoc, i));
348345
});
349346
}
350347

0 commit comments

Comments
 (0)