Add language endpoint to developer APIs (#41)
* Added languages endpoint and its tests
This commit is contained in:
@@ -1,21 +1,31 @@
|
||||
import { ApolloServer, gql, IResolvers } from "apollo-server-micro";
|
||||
import { ApolloServer, gql, IResolvers, ApolloError, UserInputError } from "apollo-server-micro";
|
||||
import { NextApiHandler } from "next";
|
||||
import NextCors from "nextjs-cors";
|
||||
import { googleScrape, textToSpeechScrape } from "@utils/translate";
|
||||
import { retrieveFromType, getName } from "@utils/language";
|
||||
|
||||
export const typeDefs = gql`
|
||||
enum LangType {
|
||||
SOURCE,
|
||||
TARGET
|
||||
}
|
||||
type Query {
|
||||
translation(source: String="auto" target: String="en" query: String!): Translation!
|
||||
audio(lang: String! query: String!): Entry!
|
||||
languages(type: LangType): [Language]!
|
||||
}
|
||||
type Translation {
|
||||
source: Entry!
|
||||
target: Entry!
|
||||
}
|
||||
type Entry {
|
||||
lang: String!
|
||||
text: String
|
||||
audio: [Int]
|
||||
lang: Language!
|
||||
text: String!
|
||||
audio: [Int]!
|
||||
}
|
||||
type Language {
|
||||
code: String!
|
||||
name: String!
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -25,35 +35,65 @@ export const resolvers: IResolvers = {
|
||||
const { source, target, query } = args;
|
||||
return {
|
||||
source: {
|
||||
lang: source,
|
||||
lang: {
|
||||
code: source
|
||||
},
|
||||
text: query
|
||||
},
|
||||
target: {
|
||||
lang: target
|
||||
lang: {
|
||||
code: target
|
||||
}
|
||||
}
|
||||
};
|
||||
},
|
||||
audio(_, args) {
|
||||
const { lang, query } = args;
|
||||
return {
|
||||
lang: args.lang,
|
||||
text: args.query
|
||||
lang: {
|
||||
code: lang
|
||||
},
|
||||
text: query
|
||||
};
|
||||
},
|
||||
languages(_, args) {
|
||||
const { type } = args;
|
||||
const langEntries = retrieveFromType(type?.toLocaleLowerCase());
|
||||
return langEntries.map(([code, name]) => ({ code, name }));
|
||||
}
|
||||
},
|
||||
Translation: {
|
||||
async target(parent) {
|
||||
const { source, target } = parent;
|
||||
const { translationRes } = await googleScrape(source.lang, target.lang, source.text);
|
||||
const textScrape = await googleScrape(source.lang.code, target.lang.code, source.text);
|
||||
|
||||
if ("errorMsg" in textScrape)
|
||||
throw new ApolloError(textScrape.errorMsg);
|
||||
return {
|
||||
lang: target.lang,
|
||||
text: translationRes
|
||||
text: textScrape.translationRes
|
||||
};
|
||||
}
|
||||
},
|
||||
Entry: {
|
||||
async audio(parent) {
|
||||
const { lang, text } = parent;
|
||||
return await textToSpeechScrape(lang, text);
|
||||
const audio = await textToSpeechScrape(lang.code, text);
|
||||
if (!audio)
|
||||
throw new ApolloError("An error occurred while retrieving the audio");
|
||||
return audio;
|
||||
}
|
||||
},
|
||||
Language: {
|
||||
name(parent) {
|
||||
const { code, name } = parent;
|
||||
if (name)
|
||||
return name;
|
||||
|
||||
const newName = getName(code);
|
||||
if (!newName)
|
||||
throw new UserInputError("Invalid language code");
|
||||
return newName;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user