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

懵懂的女人 提交于 2021-01-05 07:43:25

问题


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.

来源:https://stackoverflow.com/questions/65247887/how-to-execute-a-directive-before-a-mutation-is-committed-to-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!