Skip to content

Instantly share code, notes, and snippets.

@iliran11
Last active May 11, 2020 13:55
Show Gist options
  • Save iliran11/f1d1585995be7e354ece4710e6f64c0d to your computer and use it in GitHub Desktop.
Save iliran11/f1d1585995be7e354ece4710e6f64c0d to your computer and use it in GitHub Desktop.
INTL directive - graphql
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