-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add support for directives applied on IDL & Schema #746
Conversation
Wow, this would be excellent, we could probably add a lot of great functionality to graphql-tools with this. |
63bc010
to
3d4235a
Compare
3d4235a
to
29468a0
Compare
cc66527
to
276ed06
Compare
@leebyron @wincent Can you please review this PR? The ability to get applied directives from We use a fork of |
Sorry for the super late review, thanks for the reminder. I really like this, but I think it's too specific and that is making the PR more complicated than it needs to be. A simpler and more general solution would be to refer to the entire AST node associated with each definition when built with |
One argument in favor of just a list of directives instead of the full AST node is when schema is not created based on the IDL, but still may include directive information. For example, if directive information would be exposed via introspection API some day, then the source of the schema would be introspection JSON and it would be beneficial to just have a list of applied directives. |
I think that may mix some concerns - we can address these separately. Right now directives are purely a feature of the GraphQL language and IDL, so a schema not created using IDL definitionally won't contain directives. When building a schema in code, you have the full power of the host programming environment, so directives shouldn't be necessary. Directives are a tool for supplying additional intent to be later interpreted by the host programming environment. As for exposing more information from introspection - that's a bit out of scope for this particular change, and that RFC may go through more changes. I see type metadata and IDL directives as likely two related but not identical concepts, and don't see a need to prematurely link the two for this particular change. |
I see, it's a good point 👍 |
@leebyron In one of our tool we need to get directive values from schema extended with IDL, e.g.:
What would be the corect value for |
Very good point, @IvanGoncharov! What if instead of Also, it could be nice for the GraphQLField definitions to also contain a reference to ast nodes for each field - since otherwise extensions may make finding the field ast for a given field more than a simple lookup |
Alternatively, we could have |
@leebyron - one question I have regarding IDL and directives capture is whether the directive location will be enforced Imagine :
The graphql spec says on validation : https://facebook.github.io/graphql/#sec-Validation.Directives
Its currently specified that this is for One could imagine requiring this to be valid :
That would be very precise. BUT a mistake in my opinion. The use of directives for metadata is really powerful but requiring schema consumers to name where the metadata is valid is TOO PRECISE. The specific graphql engine decides what metadata it understands and requiring people to name is back to the engine seems low value to me. Precise yes but low value. I ask this question because we are debating how to handle this in https://github.com/graphql-java/graphql-java and the lack of spec guidance makes it .... interesting. |
Absolutely it will be enforced. Not doing so invites accidental error.
I'm not following why being too precise is a bad thing, or a low value thing. Not having this precision would allow people to supply directives in a way that was unexpected or unsupported by the underlying tools that will read those directives. At best this requires GraphQL library authors to essentially reproduce this sort of error checking ad-hoc, at worst this would result in GraphQL users suffering from silent errors where a misspelling or misplacement causes them to accidentally break their services. |
@leebyron I have updated this PR according to your suggestion. Also, I added tests. Could you please review it? |
6d3f19b
to
047170a
Compare
@leebyron I improved tests and split this PR into smaller commits so it's easier to review. |
If we add additional fields (e.g. astNode) into values of enum these test will be broken.
047170a
to
e7f385f
Compare
@leebyron Updated PR with requested changes. |
Subtle, but that's representing a different concept - null as in it is known that there is no deprecation. This maps to how deprecationReason is represented in introspection json results as well. But for this internal value, undefined (as in - we don't know if a related AST node exists, rather than we know that there is no AST node) is a reasonable default for a concept like this. |
Great work! Excited to see this as a basis for much better schema validation error messages |
@leebyron Can you please release updated NPM package? |
Can't wait for a release with this feature!!! |
I'm sorry to ask this here – I usually don't do this sort of thing, but how does one actually define a schema-level directive using GraphQL-js? I can't tell from the tests here, as they only seem to show cases using the schema definition language. |
@taion this may not be the answer you're looking for but the next big version of |
@stubailo Thanks! Would the idea there be that those directives would serve as type metadata? I'm a bit confused now on what the resolution is per #746 (comment) on the distinction between type metadata and IDL directives. |
Oh, wait, you can't read the actual directives on a type via introspection anyway, so I guess I'm very badly missing the point here. |
Yeah, that would be a really huge improvement to the value of directives for sure! I think both are nice use cases :] |
Yeah, sorry... I thought that was already how it worked. My bad for not reading the decorator/directive spec carefully enough. |
Before this PR the only way to get directive applied on IDL was working directly with AST.
This PR adds
appliedDirectives
(would be great to come up with a shorter name?) property to everygraphql-js
type that should support it according to: graphql/graphql-spec#90I have also extended
buildASTSchema
to extract applied directives from AST.After the initial review, I'd be happy to add documentation comments and some unit tests.