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

Validation options for schemaDirectives #920

Closed
reconbot opened this issue Aug 3, 2018 · 12 comments
Closed

Validation options for schemaDirectives #920

reconbot opened this issue Aug 3, 2018 · 12 comments
Labels
feature New addition or enhancement to existing solutions

Comments

@reconbot
Copy link
Contributor

reconbot commented Aug 3, 2018

I've noticed when you use schema directives that don't exist (eg, a typo) no error is thrown and it's silently ignored. That's not fun to debug and makes using directives harder to use. I propose we have a validation option that ensures a schemaDirectives key exists for every defined directive and that we don't use unknown directives.

@ghost ghost added the feature New addition or enhancement to existing solutions label Aug 3, 2018
@stubailo
Copy link
Contributor

stubailo commented Aug 3, 2018

That sounds like a great idea to me - I'd even think that should be the default!

@IvanGoncharov
Copy link

@reconbot @stubailo We are working on it for graphql@14.0.0: graphql/graphql-js#1389

@reconbot
Copy link
Contributor Author

reconbot commented Aug 4, 2018 via email

@hwillson
Copy link
Contributor

hwillson commented Sep 7, 2018

We're working on this as part of #945. graphql-tools will be following graphql-js's lead here, so if you attempt to use a directive that doesn't exist, you'll get an error. We're going to be major version bumping graphql-tools on the next release, for this exact reason. Since we're tracking this work in #945, I'll close this off. Thanks!

@hwillson hwillson closed this as completed Sep 7, 2018
@reconbot
Copy link
Contributor Author

reconbot commented Sep 7, 2018 via email

@IvanGoncharov
Copy link

@hwillson As a suggestion. In 14.0.0 we now allow extending incomplete schemas (without query type or any type at all) so here is one way you can provide directive during schema building process:
https://github.com/graphql/graphql-js/blob/3595ea922613f3e13a07185407dd70ed45de7a66/src/utilities/__tests__/extendSchema-test.js#L1291-L1306

We added this feature specifically to allow specifying library directives without messing with AST of client SDL.

@eurostar-fennec-cooper
Copy link

Should this be reopened? It got delegated to #945, but unfortunately graphql@14.0.0 (and in my case 15.3.0) doesn't seem to have included this feature tracked by graphql/graphql-js#1389.

I can still start a graphql server (apollo, in my case) without defining functions for any of my directives.

@yaacovCR
Copy link
Collaborator

Directives have to be defined in SDL in order to be used in the latest versions of GraphQL...

It sounds like you want to make sure that all directive usages have functions that are passed to makeExecutableSchema, but there are lots of other directives that could be used, so perhaps it might be better to add a feature where you check yourself the arguments to makeExecutableSchema before passing a build schema to Apollo server?

@reconbot
Copy link
Contributor Author

What do you mean by "there are a lot of other directives that would be used"?

The feature request here is to add opt in additional validation for custom directives to help ensure they're properly implemented.

@eurostar-fennec-cooper
Copy link

eurostar-fennec-cooper commented Jan 18, 2021

I think, at the end of the day, the issue for me (and probably others) is that I can use directives in my SDL and then refuse to create implementations for them, whether they're never written or accidentally removed etc, but at the moment it seems to fail silently by design.

I don't think directives could be trusted as, for example, auth helpers, since if the implementation is removed for some reason, all your auth checks just disappear. It seems odd that directive in SDL = "wrap the resolver with this function", but that it's acceptable for "this function" to simply...not exist. It would be like using a custom scalar in the SDL and then not providing an implementation for that scalar.

@yaacovCR
Copy link
Collaborator

That line should have read that there are lots of other ways directives could be used. Even with GraphQL tools, you can use directive resolvers, class based schema directives, and the new functional schema directives.

Outside graphql-tools, directives are used in all sorts of ways. And after they are used, there is no hard requirement that they are removed/cleaned up....

Schema directives more generally are just a way to extend SDL syntax without writing a new parser, there is no requirement of an implementation at all. It is not invalid to not implement a directive, basically.

That is not to say that some level of validation of arguments to makeExecutableSchema isn't possible. You could write a custom function that introspects a schema for defined custom directives and throws if you forget to implement it.

I think it is fairly straightforward to just remember to provide implementations just like you need to remember to provide Resolvers. The key is to have tests that would fail if you forget something.

By the way, you can definitely define a custom scalar without an implementation, you will just get a scalar that just gets the default parse and serialize functions, which basically pass everything through.

In terms of directives, you can think of it as there being a default in which The graphql you entities that they annotate are just passed through...

@eurostar-fennec-cooper
Copy link

Great points. Thank you for the info. I think I'm just going to figure out if I can cover them with tests, etc. Otherwise I'll find an alternative solution to annotating the schema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New addition or enhancement to existing solutions
Projects
None yet
Development

No branches or pull requests

6 participants