Skip to content
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

Schema level mutation hooks #114

Open
Secretmapper opened this issue Apr 18, 2016 · 0 comments
Open

Schema level mutation hooks #114

Secretmapper opened this issue Apr 18, 2016 · 0 comments

Comments

@Secretmapper
Copy link

Secretmapper commented Apr 18, 2016

From the docs, resolve hooks can be added per field in the schema.

For mutations, we define it through a top level hook object we pass to getSchema.

Is there a way to define mutation hooks inside the schema? Like so:

const UserSchema = new mongoose.Schema({
  name: {
    type: String,
  },
  hooks: {
    mutation: {
     addUser: {
       pre: (next, root, args, context) => {
          authorize(request)
          next()
        }
      }
    }
  }
});

I know this isn't possible since hooks will be seen as a type, but it's the gist of the idea

I've digged around in the source and something like this doesn't seem to be possible. What seems to be possible is creating a sort of root reducer for the mutations (like redux) and making it resolve the mutation based on the context's fieldName. So something like:

pre: (next, args, context) => {
  switch(context.fieldName) {
    case 'addUser':
      authorize(request)
      return next()
  }
}

I'd like to know if this is the canonical way to do this, and it'd be great to have examples in the docs for this use case.

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

No branches or pull requests

1 participant