Skip to content

Add field mixin to type configurations ? #97

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions lib/graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def self.parse_with_parslet(string)
require 'graphql/enum_type'
require 'graphql/input_object_type'
require 'graphql/interface_type'
require 'graphql/mixin'
require 'graphql/list_type'
require 'graphql/non_null_type'
require 'graphql/union_type'
Expand Down
4 changes: 4 additions & 0 deletions lib/graphql/definition_helpers/defined_by_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def types
GraphQL::DefinitionHelpers::TypeDefiner.instance
end

def mixin(mixin)
@fields = mixin.fields.merge(@fields)
end

def field(name, type = nil, desc = nil, field: nil, property: nil, &block)
if block_given?
field = GraphQL::Field.define(&block)
Expand Down
7 changes: 7 additions & 0 deletions lib/graphql/mixin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module GraphQL
class Mixin
include GraphQL::DefinitionHelpers::DefinedByConfig
defined_by_config :fields
attr_accessor :fields
end
end
6 changes: 5 additions & 1 deletion spec/graphql/introspection/type_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
|}
let(:result) { DummySchema.execute(query_string, context: {}, variables: {"cheeseId" => 2}) }
let(:cheese_fields) {[
{"name"=>"cents", "isDeprecated" => false, "type" => { "name" => "Non-Null", "ofType" => { "name" => "Int"}}},
{"name"=>"currency", "isDeprecated" => false, "type" => { "name" => "Non-Null", "ofType" => { "name" => "String"}}},
{"name"=>"id", "isDeprecated" => false, "type" => { "name" => "Non-Null", "ofType" => { "name" => "Int"}}},
{"name"=>"flavor", "isDeprecated" => false, "type" => { "name" => "Non-Null", "ofType" => { "name" => "String"}}},
{"name"=>"origin", "isDeprecated" => false, "type" => { "name" => "Non-Null", "ofType" => { "name" => "String"}}},
Expand All @@ -37,7 +39,9 @@
{"name"=>"AnimalProduct"}
],
"fields"=>[
{"type"=>{"name"=>"Non-Null", "ofType"=>{"name"=>"ID"}}},
{"type"=>{"name"=>"Non-Null","ofType"=>{"name"=>"Int"}}},
{"type"=>{"name"=>"Non-Null","ofType"=>{"name"=>"String"}}},
{"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"}}},
Expand Down
8 changes: 7 additions & 1 deletion spec/support/dairy_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ class NoSuchDairyError < StandardError; end
value("YAK", "Animal with long hair", deprecation_reason: "Out of fashion")
end

HasPrice = GraphQL::Mixin.define do
field :cents, !types.Int
field :currency, !types.String
end

CheeseType = GraphQL::ObjectType.define do
name "Cheese"
description "Cultured dairy product"
interfaces [EdibleInterface, AnimalProductInterface]

mixin HasPrice
# Can have (name, type, desc)
field :id, !types.Int, "Unique identifier"
field :flavor, !types.String, "Kind of Cheese"
Expand Down Expand Up @@ -62,6 +67,7 @@ class NoSuchDairyError < StandardError; end
name 'Milk'
description "Dairy beverage"
interfaces [EdibleInterface, AnimalProductInterface]
mixin HasPrice
field :id, !types.ID
field :source, DairyAnimalEnum, "Animal which produced this milk"
field :origin, !types.String, "Place the milk comes from"
Expand Down