Skip to content

feat(ObjectType) lookup field implementations from interface, too #108

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 3 commits into from
Mar 1, 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
10 changes: 10 additions & 0 deletions lib/graphql/interface_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ def kind
def possible_types
@possible_types ||= []
end

# @return [GraphQL::Field] The defined field for `field_name`
def get_field(field_name)
fields[field_name]
end

# @return [Array<GraphQL::Field>] All fields on this type
def all_fields
fields.values
end
end
4 changes: 2 additions & 2 deletions lib/graphql/introspection/fields_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
argument :includeDeprecated, GraphQL::BOOLEAN_TYPE, default_value: false
resolve -> (object, arguments, context) {
return nil if !object.kind.fields?
fields = object.fields.values
fields = object.all_fields
if !arguments["includeDeprecated"]
fields = fields.select {|f| !f.deprecation_reason }
end
fields
fields.sort_by { |f| f.name }
}
end
19 changes: 19 additions & 0 deletions lib/graphql/object_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,23 @@ def interfaces=(new_interfaces)
def kind
GraphQL::TypeKinds::OBJECT
end

# @return [GraphQL::Field] The field definition for `field_name` (may be inherited from interfaces)
def get_field(field_name)
fields[field_name] || interface_fields[field_name]
end

# @return [Array<GraphQL::Field>] All fields, including ones inherited from interfaces
def all_fields
interface_fields.merge(self.fields).values
end

private

# Create a {name => defn} hash for fields inherited from interfaces
def interface_fields
interfaces.reduce({}) do |memo, iface|
memo.merge!(iface.fields)
end
end
end
2 changes: 1 addition & 1 deletion lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def execute(*args)
# Resolve field named `field_name` for type `parent_type`.
# Handles dynamic fields `__typename`, `__type` and `__schema`, too
def get_field(parent_type, field_name)
defined_field = parent_type.fields[field_name]
defined_field = parent_type.get_field(field_name)
if defined_field
defined_field
elsif field_name == "__typename"
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/schema/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def print_type(type)
module TypeKindPrinters
module FieldPrinter
def print_fields(type)
type.fields.values.map{ |field| " #{field.name}#{print_args(field)}: #{field.type}" }.join("\n")
type.all_fields.map{ |field| " #{field.name}#{print_args(field)}: #{field.type}" }.join("\n")
end

def print_args(field)
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/schema/type_reducer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def self.find_all(types)
def find_types(type, type_hash)
type_hash[type.name] = type
if type.kind.fields?
type.fields.each do |name, field|
type.all_fields.each do |field|
reduce_type(field.type, type_hash)
field.arguments.each do |name, argument|
reduce_type(argument.type, type_hash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def validate_field(errors, ast_field, parent_type, parent)
return GraphQL::Language::Visitor::SKIP
end

field = parent_type.fields[ast_field.name]
field = parent_type.get_field(ast_field.name)
if field.nil?
errors << message("Field '#{ast_field.name}' doesn't exist on type '#{parent_type.name}'", parent)
return GraphQL::Language::Visitor::SKIP
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ https://medium.com/@gauravtiwari/graphql-and-relay-on-rails-first-relay-powered-
- Add a custom dump for Relay (it expects default value strings to be double-quoted)
- Make variable validation provide a specific, useful message
- Add docs for shared behaviors & DRY code
- Optimize the pure-Ruby parser (hand-write, RACC?!)
- Big ideas:
- Revamp the fixture Schema to be more useful (better names, more extensible)
- __Subscriptions__
Expand Down
12 changes: 6 additions & 6 deletions spec/graphql/introspection/schema_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
"queryType"=>{
"fields"=>[
{"name"=>"cheese"},
{"name"=>"milk"},
{"name"=>"dairy"},
{"name"=>"fromSource"},
{"name"=>"favoriteEdible"},
{"name"=>"cow"},
{"name"=>"searchDairy"},
{"name"=>"dairy"},
{"name"=>"error"},
{"name"=>"executionError"},
{"name"=>"maybeNull"}
{"name"=>"favoriteEdible"},
{"name"=>"fromSource"},
{"name"=>"maybeNull"},
{"name"=>"milk"},
{"name"=>"searchDairy"},
]
},
"mutationType"=> {
Expand Down
12 changes: 6 additions & 6 deletions spec/graphql/introspection/type_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
|}
let(:result) { DummySchema.execute(query_string, context: {}, variables: {"cheeseId" => 2}) }
let(:cheese_fields) {[
{"name"=>"id", "isDeprecated" => false, "type" => { "name" => "Non-Null", "ofType" => { "name" => "Int"}}},
{"name"=>"flavor", "isDeprecated" => false, "type" => { "name" => "Non-Null", "ofType" => { "name" => "String"}}},
{"name"=>"id", "isDeprecated" => false, "type" => { "name" => "Non-Null", "ofType" => { "name" => "Int"}}},
{"name"=>"origin", "isDeprecated" => false, "type" => { "name" => "Non-Null", "ofType" => { "name" => "String"}}},
{"name"=>"source", "isDeprecated" => false, "type" => { "name" => "Non-Null", "ofType" => { "name" => "DairyAnimal"}}},
{"name"=>"similarCheese", "isDeprecated"=>false, "type"=>{"name"=>"Cheese", "ofType"=>nil}},
{"name"=>"source", "isDeprecated" => false, "type" => { "name" => "Non-Null", "ofType" => { "name" => "DairyAnimal"}}},
]}

let(:dairy_animals) {[
Expand All @@ -37,11 +37,11 @@
{"name"=>"AnimalProduct"}
],
"fields"=>[
{"type"=>{"name"=>"Non-Null", "ofType"=>{"name"=>"ID"}}},
{"type"=>{"name"=>"DairyAnimal", "ofType"=>nil}},
{"type"=>{"name"=>"Non-Null", "ofType"=>{"name"=>"String"}}},
{"type"=>{"name"=>"Non-Null", "ofType"=>{"name"=>"Float"}}},
{"type"=>{"name"=>"List", "ofType"=>{"name"=>"String"}}},
{"type"=>{"name"=>"Non-Null", "ofType"=>{"name"=>"ID"}}},
{"type"=>{"name"=>"Non-Null", "ofType"=>{"name"=>"String"}}},
{"type"=>{"name"=>"DairyAnimal", "ofType"=>nil}},
]
},
"dairyAnimal"=>{
Expand Down Expand Up @@ -75,7 +75,7 @@
|}
let(:deprecated_fields) { {"name"=>"fatContent", "isDeprecated"=>true, "type"=>{"name"=>"Non-Null", "ofType"=>{"name"=>"Float"}}} }
it 'can expose deprecated fields' do
new_cheese_fields = cheese_fields + [deprecated_fields]
new_cheese_fields = [deprecated_fields] + cheese_fields
expected = { "data" => {
"cheeseType" => {
"name"=> "Cheese",
Expand Down
15 changes: 11 additions & 4 deletions spec/graphql/object_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,25 @@
assert_equal([EdibleInterface, AnimalProductInterface], type.interfaces)
end

describe '.fields ' do
describe '#get_field ' do
it 'exposes fields' do
field = type.fields["id"]
field = type.get_field("id")
assert_equal(GraphQL::TypeKinds::NON_NULL, field.type.kind)
assert_equal(GraphQL::TypeKinds::SCALAR, field.type.of_type.kind)
end

it 'exposes defined field property' do
field_without_prop = CheeseType.fields['flavor']
field_with_prop = CheeseType.fields['fatContent']
field_without_prop = CheeseType.get_field('flavor')
field_with_prop = CheeseType.get_field('fatContent')
assert_equal(field_without_prop.property, nil)
assert_equal(field_with_prop.property, :fat_content)
end

it "looks up from interfaces" do
field_from_self = CheeseType.get_field('fatContent')
field_from_iface = MilkType.get_field('fatContent')
assert_equal(field_from_self.property, :fat_content)
assert_equal(field_from_iface.property, nil)
end
end
end
4 changes: 2 additions & 2 deletions spec/graphql/schema/type_reducer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
reducer = GraphQL::Schema::TypeReducer.new(CheeseType, {})
expected = {
"Cheese" => CheeseType,
"Int" => GraphQL::INT_TYPE,
"Float" => GraphQL::FLOAT_TYPE,
"String" => GraphQL::STRING_TYPE,
"DairyAnimal" => DairyAnimalEnum,
"Float" => GraphQL::FLOAT_TYPE,
"Int" => GraphQL::INT_TYPE,
"Edible" => EdibleInterface,
"Milk" => MilkType,
"ID" => GraphQL::ID_TYPE,
Expand Down
3 changes: 1 addition & 2 deletions spec/support/dairy_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class NoSuchDairyError < StandardError; end
EdibleInterface = GraphQL::InterfaceType.define do
name "Edible"
description "Something you can eat, yum"
field :fatContent, !types.Float, "Percentage which is fat", property: :bogus_property
field :fatContent, !types.Float, "Percentage which is fat"
field :origin, !types.String, "Place the edible comes from"
end

Expand Down Expand Up @@ -65,7 +65,6 @@ class NoSuchDairyError < StandardError; end
field :id, !types.ID
field :source, DairyAnimalEnum, "Animal which produced this milk"
field :origin, !types.String, "Place the milk comes from"
field :fatContent, !types.Float, "Percentage which is milkfat"
field :flavors, types[types.String], "Chocolate, Strawberry, etc" do
argument :limit, types.Int
resolve -> (milk, args, ctx) {
Expand Down