Commit 84a48ba9 authored by Alex Kalderimis's avatar Alex Kalderimis

Allow limited scalar inheritance

To smooth over the backwards incompatible nature of the global-ID
changes, this commit introduces a compatibilty layer, which allows
variables of type `ID` to be passed to arguments of type `GlobalID` (or
any specific sub-type).

In order for this to work, the use-site must be careful to coerce the
argument manually. The manual coercion can be removed when the
compatibilty layer is removed following the deprecation cycle.
parent 7d1e1637
# frozen_string_literal: true
module GraphQLExtensions
module ScalarExtensions
# Allow ID to unify with GlobalID Types
def ==(other)
if name == 'ID' && other.is_a?(self.class) &&
other.type_class.ancestors.include?(::Types::GlobalIDType)
return true
end
super
end
end
end
::GraphQL::ScalarType.prepend(GraphQLExtensions::ScalarExtensions)
module Types
class GlobalIDType < BaseScalar
graphql_name 'GlobalID'
......
......@@ -99,8 +99,6 @@ RSpec.describe Types::GlobalIDType do
end
describe 'compatibility' do
# Simplified schema to test compatibility
def query(doc, vars)
GraphQL::Query.new(schema, document: doc, context: {}, variables: vars)
end
......@@ -112,6 +110,7 @@ RSpec.describe Types::GlobalIDType do
all_types = [::GraphQL::ID_TYPE, ::Types::GlobalIDType, ::Types::GlobalIDType[::Project]]
shared_examples 'a working query' do
# Simplified schema to test compatibility
let!(:schema) do
# capture values so they can be closed over
arg_type = argument_type
......@@ -135,10 +134,21 @@ RSpec.describe Types::GlobalIDType do
argument :id, arg_type, required: true
end
# This is needed so that all types are always registered as input types
field :echo, String, null: true do
argument :id, ::GraphQL::ID_TYPE, required: false
argument :gid, ::Types::GlobalIDType, required: false
argument :pid, ::Types::GlobalIDType[::Project], required: false
end
def project_by_id(id:)
gid = ::Types::GlobalIDType[::Project].coerce_isolated_input(id)
gid.model_class.find(gid.model_id)
end
def echo(id: nil, gid: nil, pid: nil)
"id: #{id}, gid: #{gid}, pid: #{pid}"
end
end)
end
end
......@@ -163,16 +173,16 @@ RSpec.describe Types::GlobalIDType do
GRAPHQL
end
let(:argument_type) { ::GraphQL::ID_TYPE }
where(:result_type) { all_types }
where(:result_type, :argument_type) do
all_types.flat_map { |arg_type| all_types.zip([arg_type].cycle) }
end
with_them do
it_behaves_like 'a working query'
end
end
context 'when the argument is declared as GlobalID' do
context 'when the argument is passed as GlobalID' do
let(:document) do
<<-GRAPHQL
query($projectId: GlobalID!) {
......@@ -192,7 +202,7 @@ RSpec.describe Types::GlobalIDType do
end
end
context 'when the argument is declared as ProjectID' do
context 'when the argument is passed as ProjectID' do
let(:document) do
<<-GRAPHQL
query($projectId: ProjectID!) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment