Add translate API (#72)

This commit is contained in:
Niklas Fondberg
2021-10-18 15:29:44 +02:00
committed by GitHub
parent 2a8212ff12
commit 1f11fd7288
11 changed files with 1327 additions and 5 deletions
+1269
View File
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -1,7 +1,7 @@
{
"name": "graphql-server-pw",
"version": "1.0.0",
"description": "",
"description": "Photowall GraphQL API",
"main": "src/index.ts",
"scripts": {
"exportSQL": "cd src/__test__/db_generator/ && npm install && node index.js && cd ../../../",
@@ -15,6 +15,7 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.31.0",
"@aws-sdk/client-sqs": "^3.36.1",
"@aws-sdk/client-translate": "^3.36.1",
"@aws-sdk/s3-request-presigner": "^3.31.0",
"apollo-datasource": "^3.1.0",
"apollo-datasource-rest": "^3.2.0",
View File
+27
View File
@@ -0,0 +1,27 @@
import {
TranslateClient,
TranslateTextCommand,
} from '@aws-sdk/client-translate';
export type TranslateInput = {
text: string;
// Default to 'auto'
sourceLanguageCode?: string;
targetLanguageCode: string;
};
export async function translateText(input: TranslateInput) {
const translateClient = new TranslateClient({
region: 'eu-west-1',
});
const translateTextCommand = new TranslateTextCommand({
SourceLanguageCode: input.sourceLanguageCode ?? 'auto',
TargetLanguageCode: input.targetLanguageCode,
Text: input.text,
});
return translateClient
.send(translateTextCommand)
.then((resp) => resp.TranslatedText);
}
+1 -1
View File
@@ -8,7 +8,7 @@ import { IdNumberResult, Maybe } from '../types/types';
import { BaseSQLDataSource } from './BaseSQLDataSource';
import { ImageServerApi } from './imageserver-api';
import * as CONFIG from '../config';
import { moveS3File } from '../s3';
import { moveS3File } from '../aws/s3';
const MINUTE = 60;
-1
View File
@@ -4,7 +4,6 @@ import { ProductAPI } from '../datasources/product-api';
const Category = {
async products({ id }, { includeAllSubCategories }, { dataSources, auth }) {
console.log('includeAllSubCategories:', includeAllSubCategories);
checkAccess(['products.read'], auth);
if (includeAllSubCategories === true) {
return (<ProductAPI>(
+1 -1
View File
@@ -1,4 +1,4 @@
import { EXPIRES_DEFAULT_SECS, getSignedPutObjectUrl } from '../s3';
import { EXPIRES_DEFAULT_SECS, getSignedPutObjectUrl } from '../aws/s3';
import {
SignedUploadURL,
SignedUploadURLInput,
+2
View File
@@ -19,6 +19,7 @@ import {
import { DateTimeResolver, DateResolver, JSONResolver } from 'graphql-scalars';
import { interiorsQueryTypeDefs, interiorTypeDefs } from './interiors-resolver';
import { imagesQueryTypeDefs } from './file-upload-resolvers';
import { TextsQueryTypeDefs } from './texts-resolver';
const resolvers = {
Query: {
...orderQueryTypeDefs,
@@ -29,6 +30,7 @@ const resolvers = {
...marketsQueryTypeDefs,
...interiorsQueryTypeDefs,
...imagesQueryTypeDefs,
...TextsQueryTypeDefs,
},
Mutation: {
...productMutationTypeDefs,
+1 -1
View File
@@ -14,7 +14,7 @@ import { MarketLocaleAPI } from '../datasources/market-locale-api';
import { InteriorAPI } from '../datasources/interior-api';
import { InteriorsLambdaAPI } from '../datasources/interiors-lambda-api';
import { IdNumberResult } from '../types/types';
import { moveS3File } from '../s3';
import { moveS3File } from '../aws/s3';
import { DesignerAPI } from '../datasources/designer-api';
import { checkAccess } from '../cognito/access-control';
+10
View File
@@ -0,0 +1,10 @@
import { translateText, TranslateInput } from '../aws/translate';
async function translate(_, { input }, _ctx) {
const value = await translateText(input as TranslateInput);
return { value };
}
export const TextsQueryTypeDefs = {
translate,
};
+14
View File
@@ -38,6 +38,16 @@ type Query {
interiors(filter: InteriorsFilterInput): [Interior]
signedFileUploadURL(config: SignedUploadURLInput): SignedUploadURL
@cacheControl(maxAge: 0)
translate(input: TranslateInput!): StringResult!
}
input TranslateInput {
text: String!
"""
defaults to auto
"""
sourceLanguageCode: String
targetLanguageCode: String!
}
input SignedUploadURLInput {
@@ -449,6 +459,10 @@ input ProductProportionsInput {
marginHeightMin: Int!
}
type StringResult {
value: String!
}
type IdNumberResult {
id: Int!
}