How to execute a directive before a mutation is committed to the database?

221 views Asked by At

I'm trying to add the internationalization feature to my database. It would be quite difficult to maintain a schema graphql if I needed to duplicate all translatable fields

type Quest {
    name @i18n  # simple
} 

type Quest {
    nameEn
    nameJa
    nameFr
    ...  # omg
}

The problem I'm facing is that field directives are only executed during a query (or, once a mutation has finished, to return the result).

I do not see how I could do something similar with resolvers. For any possible mutation, I want to check if any field has the @i18n directive, and execute it (so that it fetches my user's language locale from the context and modify the field with the locale as suffix.

In other words, I would like to make a directive to modify the key of a field, not its value.

How would you accomplish that ?

My question was purposefully made general, but if there's a platform specific solution, I'm using the GRANDstack with Neo4j as db and neo4j-graphql-js with Apollo server at the API level.

EDIT: Asked another question for a different way to solve my problem by modifying the schema at build time.

0

There are 0 answers