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

Directives support? #156

Open
pyrossh opened this issue Mar 12, 2018 · 4 comments
Open

Directives support? #156

pyrossh opened this issue Mar 12, 2018 · 4 comments
Labels
enhancement Improvement of existing features or bugfix help wanted

Comments

@pyrossh
Copy link

pyrossh commented Mar 12, 2018

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 pyrossh changed the title Directive support? Directives support? Mar 12, 2018
@LegNeato LegNeato added enhancement Improvement of existing features or bugfix help wanted labels May 23, 2018
@rokob
Copy link

rokob commented Nov 27, 2018

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
Copy link

rokob commented Dec 5, 2018

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. graphql/graphql-js#41 graphql/graphql-js#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.

@pyrossh
Copy link
Author

pyrossh commented Dec 5, 2018

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.

@AlbertMarashi
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement of existing features or bugfix help wanted
Projects
None yet
Development

No branches or pull requests

4 participants