Hey folks, I’ve got a nexus question (since I’ve f...
# orm-help
n
Hey folks, I’ve got a nexus question (since I’ve finally been convinced that code-first makes sense for large projects). With
nexus
, where should I put business logic that I want to run when fetching/saving types? i.e. not just field resolvers, but whole types
e.g. when saving a Tag, I want it to check user auth, not just in my
updateTag
mutation, but in any mutation where Tags might be saved (e.g. through nested/related updates:
updateArticle
with
{ tags: { create: { some tag data }}
)
right now (with apollo-server 2 and SDL type defs) I’ve got each of my resolvers/<type>.js exporting
get
and
patch
functions, which are called in mutations, queries, and other types’
get
and
patch
functions
e.g.
updateArticle
calls
Article.patch()
which calls
Tag.patch()
(checking user auth for tags) and then the returned Article object is sent to
photon.articles.update()
I know this isn’t an unheard of pattern, but what would be the
nexus
-y way to do similar stuff? middleware? stuff in the
definition()
method?
can I do this?
Copy code
const MyNode = objectType({
  definition(t) {
    t.string('foo', () => 'result')
  },
  resolve(o) {
    // business logic
    return photon.mynodes.findOne(o.id)
  }
}
I see that
t.field()
accepts an
authorize
property, but that would mean I’d have to do all auth and full type resolvers at the edges
which is doable but feels real gross, since Tag is what cares about auth/resolving, not Article.tags (and Image.tags, and any other edge to tags that I don’t want to accidentally forget to protect and thus expose a glaring security hole in my api)