Skip to content

feat: support custom resolvers #674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 5, 2020

Conversation

craigspaeth
Copy link
Contributor

@craigspaeth craigspaeth commented May 25, 2020

This is an attempt at addressing some issues people are raising for the desire to hook into the CRUD operations. It solves this with middleware that can be used like so...

t.crud.createOneUser({
  middleware: async (root, args, ctx, info, next) => {
    // Something before
    const res = await next()
    // Something after
    return res
  }
})

I'm very open to an alternative implementation like before and after or whatever you all recommend. Whatever shape it takes hooking into the CRUD lifecycle is a feature I'd love to see in this plugin.

This code works for me but two weird things happened when I tried to write a test and clean this up...

  1. I got a segfault trying to run tests
/bin/sh: line 1: 89527 Segmentation fault: 11  ts-node tests/__ensure-engine
error Command failed with exit code 139.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
  1. A bunch of formatting noise got caught in my PR even though I turned off my linter/prettier. Maybe this is because of a git hook or something I'm not seeing?

Let me know what I can do to work past this and get this into a mergeable state.

Update:

Closes #684, #541, #550

@craigspaeth craigspaeth changed the title [WIP] Support CRUD middleware feat: Support CRUD middleware May 25, 2020
@Weakky Weakky force-pushed the cs/support-crud-middleware branch from 4b64359 to ef44718 Compare June 5, 2020 14:43
@Weakky Weakky changed the title feat: Support CRUD middleware feat: support custom resolver Jun 5, 2020
@Weakky
Copy link
Member

Weakky commented Jun 5, 2020

Hey @craigspaeth,

We've taken the liberty to update your PR and get it into a mergeable state. We've also made some modification to your original proposal:

  • middleware has been renamed to resolve. The feature is not a middleware per se as it completely replaces the original resolver and optionally wraps it. There's no stack involved. It also makes the feature integrate seamlessly with the Nexus Schema resolve API
  • We've made it available on t.model as well
  • We've renamed next to originalResolve
  • We've updated the typegen so that we get nice type-safety on the resolve method for the root, args, ctx params, but also on the same parameters expected by the originalResolver

Usage

queryType({
  definition(t) {
    t.crud.users({
      async resolve(root, args, ctx, info, originalResolve) {
        console.log('before')
        const res = await originalResolve(root, args, ctx, info)
        console.log('after')
        return res 
      }
    })
  }
})

@Weakky Weakky linked an issue Jun 5, 2020 that may be closed by this pull request
@Weakky Weakky changed the title feat: support custom resolver feat: support custom resolvers Jun 5, 2020
Copy link
Contributor

@jasonkuhrt jasonkuhrt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@Weakky
Copy link
Member

Weakky commented Jun 5, 2020

@craigspaeth Thanks a lot for leading that effort, merging now 🚀

@Weakky Weakky merged commit 9569bd6 into graphql-nexus:master Jun 5, 2020
@craigspaeth
Copy link
Contributor Author

craigspaeth commented Jun 8, 2020

Wow—amazing, thank you so much @Weakky and team! ❤️ 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add hooks for CRUD nexus prisma fields Prisma2 CRUD: "before" and "after" hooks
3 participants