Skip to content

feature: Support inline fragments without a type condition #123

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

Merged
merged 1 commit into from
Mar 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/graphql/language/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ class Parser < Parslet::Parser
directives.maybe.as(:optional_directives).as(:directives)
}
rule(:spread) { str("...") }
rule(:type_condition) { str("on ") >> name.as(:optional_string_content) }
# TODO: `on` bug, see spec
rule(:inline_fragment) {
spread.as(:fragment_spread_keyword) >> space? >>
str("on ") >> name.as(:inline_fragment_type) >> space? >>
type_condition.maybe.as(:inline_fragment_type) >> space? >>
directives.maybe.as(:optional_directives).as(:directives) >> space? >>
selections.as(:selections)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/language/transform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def self.optional_sequence(name)
inline_fragment_type: simple(:n),
directives: sequence(:d),
selections: sequence(:s),
) { CREATE_NODE[:InlineFragment, type: n.to_s, directives: d, selections: s, position_source: kw]}
) { CREATE_NODE[:InlineFragment, type: n, directives: d, selections: s, position_source: kw]}

# Operation Definition
rule(
Expand Down
1 change: 1 addition & 0 deletions lib/graphql/query/serial_execution/selection_resolution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def flatten_fragment(ast_fragment)
end

def fragment_type_can_apply?(ast_fragment)
return true unless ast_fragment.type
child_type = execution_context.get_type(ast_fragment.type)
resolved_type = GraphQL::Query::TypeResolver.new(target, child_type, type).type
!resolved_type.nil?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ def validate(context)

context.visitor[GraphQL::Language::Nodes::InlineFragment] << -> (node, parent) {
fragment_parent = context.object_types[-2]
fragment_child = context.object_types.last
validate_fragment_in_scope(fragment_parent, fragment_child, node, context)
if fragment_child = context.object_types.last
validate_fragment_in_scope(fragment_parent, fragment_child, node, context)
end
}

spreads_to_validate = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def validate(context)
private

def validate_type_exists(node, context)
return unless node.type
type = context.schema.types.fetch(node.type, nil)
if type.nil?
context.errors << message("No such type #{node.type}, so it can't be a fragment condition", node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def validate(context)

def validate_type_is_composite(node, context)
type_name = node.type
return unless type_name
type_def = context.schema.types[type_name]
if type_def.nil? || !type_def.kind.composite?
context.errors << message("Invalid fragment on type #{type_name} (must be Union, Interface or Object)", node)
Expand Down
6 changes: 5 additions & 1 deletion lib/graphql/static_validation/type_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ def self.get_strategy_for_node_class(node_class)

class FragmentWithTypeStrategy
def push(stack, node)
object_type = stack.schema.types.fetch(node.type, nil)
object_type = if node.type
stack.schema.types.fetch(node.type, nil)
else
stack.object_types.last
end
if !object_type.nil?
object_type = object_type.unwrap
end
Expand Down
3 changes: 3 additions & 0 deletions spec/graphql/directive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
... on Cheese @skip(if: false) { dontSkipInlineId: id }
... on Cheese @include(if: true) { includeInlineId: id }
... on Cheese @include(if: false) { dontIncludeInlineId: id }
... @skip(if: true) { skipNoType: id }
... @skip(if: false) { dontSkipNoType: id }
}
}
fragment includeFlavorField on Cheese { includeFlavor: flavor }
Expand All @@ -66,6 +68,7 @@
"includeFlavor" => "Brie",
"dontSkipInlineId" => 1,
"includeInlineId" => 1,
"dontSkipNoType" => 1,
},
}}
assert_equal(expected, result)
Expand Down
1 change: 1 addition & 0 deletions spec/graphql/language/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
name,
... on Species { color },
... family # background info, of course
... @skip(if: true) { inlineFragSkip },
}
}
subscription watchStuff {
Expand Down
5 changes: 5 additions & 0 deletions spec/graphql/language/transform_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,9 @@ def get_result(query_string, parse: nil, debug: false)
res = get_result("{quoted: \"\\\" \\\\ \\/ \\b \\f \\n \\r \\t\"}", parse: :value_input_object)
assert_equal("\" \\ / \b \f \n \r \t", res.arguments[0].value)
end

it 'transforms inline fragment optional type condition' do
res = get_result("{ ... @skip(if: true) { skippedField }, ... on Pet { isHousebroken } }")
assert_equal([nil, "Pet"], res.definitions.first.selections.map(&:type))
end
end