Last active
May 11, 2020 13:55
-
-
Save iliran11/f1d1585995be7e354ece4710e6f64c0d to your computer and use it in GitHub Desktop.
INTL directive - graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { SchemaDirectiveVisitor } = require("graphql-tools"); | |
const { defaultFieldResolver } = require("graphql"); | |
const i18next = require("i18next"); | |
class IntlDirective extends SchemaDirectiveVisitor { | |
visitFieldDefinition(field) { | |
const { resolve = defaultFieldResolver } = field; | |
field.resolve = async (...args) => { | |
const context = args[2]; | |
const info = args[3]; | |
const value = await resolve.apply(this, args); | |
// very basic example to demonstrage | |
if (info.fieldName === "greeting") { | |
return i18next.t("greeting_key", { lng: context.lng }); | |
} | |
return value; | |
}; | |
} | |
} | |
module.exports = IntlDirective; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment