graphql-transform-schema
graphql-transform-schema copied to clipboard
Hiding operations based on the result of a function
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.
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
}
})
}
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
.
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.