Skip to content

Commit

Permalink
fix: graphql localized relationship bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikrut committed Apr 19, 2021
1 parent 035f6c6 commit 280f809
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions src/graphql/schema/buildObjectType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ function buildObjectType(name: string, fields: Field[], parentName: string, base
extensions: { complexity: 20 },
async resolve(parent, args, context) {
const value = parent[field.name];
const locale = args.locale || context.locale;
const fallbackLocale = args.fallbackLocale || context.fallbackLocale;
const locale = args.locale || context.req.locale;
const fallbackLocale = args.fallbackLocale || context.req.fallbackLocale;

let id = value;

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

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

let resolveType = function resolveType(data) {
return this.collections[data.collection].graphQL.type.name;
};

resolveType = resolveType.bind(this);

type = new GraphQLObjectType({
name: `${relationshipName}_Relationship`,
fields: {
Expand All @@ -245,7 +239,9 @@ function buildObjectType(name: string, fields: Field[], parentName: string, base
type: new GraphQLUnionType({
name: relationshipName,
types,
resolveType,
async resolveType(data, { req: { payload } }) {
return payload.collections[data.collection].graphQL.type.name;
},
}),
},
},
Expand Down Expand Up @@ -294,24 +290,25 @@ function buildObjectType(name: string, fields: Field[], parentName: string, base
extensions: { complexity: 10 },
async resolve(parent, args, context) {
const value = parent[field.name];
const locale = args.locale || context.locale;
const fallbackLocale = args.fallbackLocale || context.fallbackLocale;
const locale = args.locale || context.req.locale;
const fallbackLocale = args.fallbackLocale || context.req.fallbackLocale;
let relatedCollectionSlug = field.relationTo;

if (hasManyValues) {
const results = [];
const resultPromises = [];

const createPopulationPromise = async (relatedDoc) => {
const createPopulationPromise = async (relatedDoc, i) => {
let id = relatedDoc;
let collectionSlug = field.relationTo;

if (isRelatedToManyCollections) {
relatedCollectionSlug = relatedDoc.relationTo;
collectionSlug = relatedDoc.relationTo;
id = relatedDoc.value;
}

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

if (result.docs.length === 1) {
if (isRelatedToManyCollections) {
results.push({
relationTo: relatedCollectionSlug,
results[i] = {
relationTo: collectionSlug,
value: {
...result.docs[0],
collection: relatedCollectionSlug,
collection: collectionSlug,
},
});
};
} else {
results.push(result.docs[0]);
[results[i]] = result.docs;
}
}
};

if (value) {
value.forEach((relatedDoc) => {
resultPromises.push(createPopulationPromise(relatedDoc));
value.forEach((relatedDoc, i) => {
resultPromises.push(createPopulationPromise(relatedDoc, i));
});
}

Expand Down

0 comments on commit 280f809

Please sign in to comment.