-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
73 lines (62 loc) · 2.07 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import express from 'express';
import {ApolloServer} from 'apollo-server-express';
import {localizeSchema, localizedContext, GLocString} from 'smartloc/graphql';
import { setDefaultLocale, addLocale, loc } from 'smartloc';
import { GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLNonNull } from 'graphql';
import GJson from 'graphql-type-json';
const app = express();
const port = 3000;
// lets say our developpers use latin to write original strings in code
setDefaultLocale('la');
// add translations for actual languages...
addLocale('fr-FR', {
hello: 'Bonjour tout le monde !',
arg: 'Vous êtes ici: {0}',
});
addLocale('en-US', {
hello: 'Hello world !',
arg: 'You are here: {0}',
});
// build your schema
// [self promotion] checkout my other library if you with to add some dependency injection in resolvers https://www.npmjs.com/package/inversify-graphql
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Root',
fields: () => ({
hello: {
type: GLocString,
description: 'A greeting string that is translatable',
resolve: () => loc('hello')`Salve mundi !`,
},
location: {
type: GJson,
description: 'Your location, as a JSON translatable object',
args: {
myArg: { type: new GraphQLNonNull(GraphQLString), }
},
resolve: (_, {myArg}) => ({
location: loc('arg')`Hic es ${myArg}`,
}),
},
})
})
});
// create our appolo endpoint
const apollo = new ApolloServer({
schema: localizeSchema(schema),
context: localizedContext(async http => {
// build your own context here as usual
return {};
})
});
apollo.applyMiddleware({
app,
path: '/graphql',
});
// start listening
app.listen(port, () => console.log(`Localized graphql listening on port ${port}!
Try visiting http://localhost:3000/graphql and run the query:
{
hello
location(myArg: "somewhere")
}`))