juniper icon indicating copy to clipboard operation
juniper copied to clipboard

Directives support?

Open pyrossh opened this issue 6 years ago • 4 comments

Authentication and other checks can be included in the generated schema using directives. Is it possible to do that juniper right now?

I'm trying to add auth checks for certain mutations using directives but seems impossible in juniper. Something like this, https://gist.github.com/LawJolla/132b99179b978cf76eadfd7a6d742b22#file-scratch-graphql Only other alternative I see is using macros. isAuthenticate!() and hasRole!(role: “MANAGER”) within the mutations.

pyrossh avatar Mar 12 '18 08:03 pyrossh

Hey I want this too. It looks like the query parser supports directives, but I don't see a way to add new directives of your own or to handle resolution. I'll give it a go at taking this on. I'll dig into what this might look like and post back here so I don't go too far off the reservation before agreeing on a design.

rokob avatar Nov 27 '18 19:11 rokob

So the "official" js library doesn't support directives other than the two baked in ones, skip and include, and won't be any time soon, c.f. https://github.com/graphql/graphql-js/issues/41 https://github.com/graphql/graphql-js/issues/446

Thus adding support here would be deviating from the standard a bit and therefore is maybe why this hasn't been added yet.

You can create directives in your schema via something like root_node.schema.add_directive https://github.com/graphql-rust/juniper/blob/master/juniper/src/schema/model.rs#L190

The AST has the notion of directives which is how skip and include work for a query. Adding support for extra field level directives seems to be pretty straight forward, it would just be generalizing skip/include to call into a directive resolver.

However, there are other notions of directive that I have seen, for example https://www.apollographql.com/docs/graphql-tools/schema-directives.html, which are much more complicated. For instance, they have an @upper directive which uppercases strings:

type Query {
  hello: String @upper
}

This would require resolving the field, then calling into some other resolver defined for the directive. Again that specific case isn't too hard. But this quickly gets out of control.

Solving this with an abstraction like the SchemaDirectiveVisitor from Apollo seems like the right solution if this is a feature you'd actually want to add to this library. As this library doesn't really work with the SDL I am not sure this is actually a desired feature here. It probably makes more sense to just write resolvers that handle the necessary logic. If this library was able to parse a SDL document to generate types and hook into resolvers then this might be a worthwhile feature, but as it stands it is kinda antithetical to how this library views type definitions.

rokob avatar Dec 05 '18 00:12 rokob

I didn't know that it wasn't implemented in the official graphql-js library. I had used apollo and thought they were using something present in graphql. Then I think its better not to make juniper complicated like apollo. Let another framework or library handle it. Its best to stick to the spec.

pyrossh avatar Dec 05 '18 05:12 pyrossh

Directives are quite important, especially for building stuff relating to authorisation, scopes & permissions. How can we do this?

AlbertMarashi avatar Jan 17 '23 02:01 AlbertMarashi