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

Hiding operations based on the result of a function #9

Open
aloof-ruf opened this issue Nov 24, 2017 · 3 comments
Open

Hiding operations based on the result of a function #9

aloof-ruf opened this issue Nov 24, 2017 · 3 comments

Comments

@aloof-ruf
Copy link

aloof-ruf commented Nov 24, 2017

As mentioned in the schema transformation documentation, one can hide operations in a transformSchema call by setting boolean values against desired queries/mutations like so:

// Example below is from the documentation

const schema = ... // this is the GraphQLSchema instance implementing the SDL from above
const transformedSchema = transformSchema(schema, {
  Query: {
    'allUsers': false   // hide `allUsers` field on `Query` type
  },
  Mutation: {
    'deleteUser': false // hide `deleteUser` field on `Mutation` type
  }
})

However, there may be situations where you'd want to conditionally hide one query/mutation or another. This functionality could be provided by functions, but I'm still on the fence how, especially when functions are used to modify arguments before they hit a resolve (so how could you tell a "hiding" function from a "modifying" function?). I still feel this could be a good feature.

@kbrandwijk
Copy link

kbrandwijk commented Nov 24, 2017

I think the schema transformation should be a static and stateless operation. Other patterns are possible for adding conditional logic. I don't think that should be part of the defined transform, but should be dealt with on a higher level (conditional transformSchema, not conditional transform inside it).

if (req.user.role != 'ADMIN') {
   finalSchema = transformSchema(schema, {
      Mutation: {
          'deleteUser': false
      }
   })
}

@aloof-ruf
Copy link
Author

aloof-ruf commented Nov 25, 2017

That's a good point @kbrandwijk. You're basically saying you can do that logic somewhere else (either by surrounding it in a conditional, or running a function that returns true/false ) then put that in the object. I'm starting to side with you. The only reason I can see against that is if you wanted to something slick with the arguments (ie, you pass a "conditional function" that get's the field map, field name as arguments). For example:

transformSchema(schema, {
  Mutation: {
    'deleteUser': (fieldMap, fieldName) => { ...perform some magic with args passed... },
  }
});

As I said before, I'm starting to side with you. The only way something like this would be useful is if there are significant changes/processing done to the rules you pass. Otherwise, you can just do as you say: process/inspect the rules beforehand and come up with the result you need and stick it into transformSchema.

@kbrandwijk
Copy link

Can you share a concrete example of the kind of arguments that you want to process in a function like that? Because I can't envision it right now.

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

No branches or pull requests

2 participants