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

Add schema language directives #273

Merged
merged 1 commit into from
Sep 23, 2016

Conversation

cjoudrey
Copy link
Contributor

@cjoudrey cjoudrey commented Sep 23, 2016

Back in May, graphql/graphql-js#376 added support for directives in GraphQL IDL.

Deprecations were added as directives to the IDL in graphql/graphql-js#384.

Finally, descriptions were added to the IDL in graphql/graphql-js#464.

This is part 1 of a few pull requests I will be submitting that will add all of these features to the gem.

This pull request includes:

  • Add parsing support for directives.
  • Test schema parsing/generation against a copy of graphql-js' schema-kitchen-sink.graphql to ensure we are consistent.
  • Add a new EnumValueDefinition AST node to support directives on enum values. This is a breaking API change as GraphQL::Language::Nodes::EnumTypeDefinition#values used to return an array of strings, now it will return an array of EnumValueDefinition.

👀 @rmosolgo @dylanahsmith

Copy link
Owner

@rmosolgo rmosolgo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Surprise, everything can have directives now! This is awesome, thanks for making it happen. I forgot it would require getting all the way down to the parser 😬

As far as the breaking change to the Nodes API, it's safe to make as far as I know. I haven't heard of any third-party use of the Schema AST.

Just a couple of questions above to make sure I understand the "whys" before adding some code.

@@ -202,7 +202,7 @@ class FragmentDefinition < AbstractNode
def initialize_node(name: nil, type: nil, directives: [], selections: [])
@name = name
@type = type
@directives = directives
@directives = directives || []
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that directives is often falling back to || [] -- what's the case where this is needed? (I ask because I'm only aware of directives_list_opt in the parser, which already provides [] for the empty case.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I did this because I saw we had done it for ObjectTypeDefinition interfaces.

I was under the impression we did this so that if someone intentionally passes nil, it would get turned into []. It does add quite a bit noise, maybe we can just remove the || [].

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I'd say if the tests are green without this, then let's not add them. If "people" pass nil, they get nil :P (fortunately I think "people" is just parser.rb)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 😁

@@ -116,7 +130,7 @@ def generate(node, indent: "")
when Array
"[#{node.map { |v| generate(v) }.join(", ")}]"
when Hash
"{ #{node.map { |k, v| "#{k}: #{generate(v)}" }.join(", ")} }"
"{#{node.map { |k, v| "#{k}: #{generate(v)}" }.join(", ")}}"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I right that this whitespace change was added so that our dump matches the "kitchen sink" example from graphql.js ? It's good by me, I just want to make sure I understand it before adding noise to peoples' diffs :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct. I took for granted that Facebook's "kitchen sink" was formatted correctly. Also: https://github.com/graphql/graphql-js/blob/a725499b155285c2e33647a93393c82689b20b0f/src/language/printer.js#L81

@cjoudrey cjoudrey force-pushed the add-directives-parsing branch from 9c26834 to ef68514 Compare September 23, 2016 13:58
@cjoudrey cjoudrey mentioned this pull request Sep 23, 2016
@rmosolgo
Copy link
Owner

Awesome, thanks for this new feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants