Skip to content

Commit

Permalink
Add translations to the "incidents" API graphql resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
pdcp1 committed Feb 28, 2025
1 parent 28873be commit d98858e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions site/gatsby-site/server/types/incidents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import { UserType } from "./user";
import { IncidentEmbeddingType, NlpSimilarIncidentType, TsneType } from "./types";
import { ReportType } from "./report";
import { GraphQLDateTime } from "graphql-scalars";
import { Context } from "../interfaces";

const IncidentTranslationsType = new GraphQLObjectType({
name: 'IncidentTranslations',
fields: {
language: { type: GraphQLString },
title: { type: GraphQLString },
description: { type: GraphQLString },
}
});

export const IncidentType = new GraphQLObjectType({
name: 'Incident',
Expand Down Expand Up @@ -51,6 +61,37 @@ export const IncidentType = new GraphQLObjectType({
reports: getListRelationshipConfig(ReportType, GraphQLInt, 'reports', 'report_number', 'reports', 'aiidprod'),
tsne: { type: TsneType },
created_at: { type: GraphQLDateTime },
translations: {
type: new GraphQLList(IncidentTranslationsType),
args: {
languages: { type: new GraphQLNonNull(new GraphQLList(GraphQLString)) }
},
resolve: async (source, args, context: Context) => {
console.log('SAPEEEEEE', source.incident_id, args.languages)
const translationsCollection = context.client.db('translations').collection("incidents");

const translations = await translationsCollection.find({
incident_id: source.incident_id,
language: { $in: args.languages }
}).toArray();

console.log('translations', translations)

return args.languages.map((language: string) => {
const translation = translations.find(t => t.language === language);

return translation ? {
language: language,
title: translation.title || "",
description: translation.description || "",
} : {
language: language,
title: null,
description: null,
};
});
},
},
},
});

Expand Down

0 comments on commit d98858e

Please sign in to comment.