Skip to content

Commit

Permalink
feat: add support for the @interfaceObject directive (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
moonflare authored Feb 21, 2023
1 parent fc541ee commit c7b987d
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 32 deletions.
4 changes: 4 additions & 0 deletions lib/apollo-federation/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def inaccessible
add_directive(name: 'inaccessible')
end

def interface_object
add_directive(name: 'interfaceObject')
end

def key(fields:, camelize: true)
add_directive(
name: 'key',
Expand Down
14 changes: 6 additions & 8 deletions lib/apollo-federation/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ def self.included(klass)
end

module CommonMethods
FEDERATION_2_PREFIX = <<~SCHEMA
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
SCHEMA
DEFAULT_LINK_NAMESPACE = 'federation'.freeze

def federation(version: '1.0', link: {})
@federation_version = version
@link = { as: 'federation' }.merge(link)
@link = { as: DEFAULT_LINK_NAMESPACE }.merge(link)
end

def federation_version
Expand All @@ -35,7 +31,7 @@ def federation_sdl(context: nil)
document_from_schema = FederatedDocumentFromSchemaDefinition.new(self, context: context)

output = GraphQL::Language::Printer.new.print(document_from_schema.document)
output.prepend(FEDERATION_2_PREFIX) if federation_2?
output.prepend(federation_2_prefix) if federation_2?
output
end

Expand All @@ -61,9 +57,11 @@ def query(new_query_object = nil)
private

def federation_2_prefix
federation_namespace = ", as: \"#{link_namespace}\"" if link_namespace != DEFAULT_LINK_NAMESPACE

<<~SCHEMA
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "#{link_namespace}")
@link(url: "https://specs.apollo.dev/federation/v2.3"#{federation_namespace})
SCHEMA
end
Expand Down
27 changes: 27 additions & 0 deletions spec/apollo-federation/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@

expect(schema.federation_version).to eq('2.0')
end

it 'returns the specified version when set' do
schema = Class.new(GraphQL::Schema) do
include ApolloFederation::Schema
federation version: '2.3'
end

expect(schema.federation_version).to eq('2.3')
end
end

describe '.federation_2?' do
Expand Down Expand Up @@ -61,6 +70,15 @@
expect(schema.federation_2?).to be(true)
end

it 'returns true when the version is a float greater than 2.0' do
schema = Class.new(GraphQL::Schema) do
include ApolloFederation::Schema
federation version: 2.3
end

expect(schema.federation_2?).to be(true)
end

it 'returns true when the version is a string greater than 2.0' do
schema = Class.new(GraphQL::Schema) do
include ApolloFederation::Schema
Expand All @@ -69,5 +87,14 @@

expect(schema.federation_2?).to be(true)
end

it 'returns true when the version is a string greater than 2.0' do
schema = Class.new(GraphQL::Schema) do
include ApolloFederation::Schema
federation version: '2.3'
end

expect(schema.federation_2?).to be(true)
end
end
end
100 changes: 76 additions & 24 deletions spec/apollo-federation/service_field_v2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Product {
upc: String!
Expand Down Expand Up @@ -144,7 +144,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Product @federation__extends {
upc: String!
Expand Down Expand Up @@ -180,7 +180,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Position @federation__shareable {
x: Int!
Expand Down Expand Up @@ -217,7 +217,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Position @federation__inaccessible {
x: Int!
Expand Down Expand Up @@ -254,7 +254,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3", as: "fed2")
type Product @fed2__extends {
upc: String!
Expand Down Expand Up @@ -290,7 +290,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3", as: "fed2")
type Position @fed2__shareable {
x: Int!
Expand Down Expand Up @@ -327,7 +327,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3", as: "fed2")
type Position @fed2__inaccessible {
x: Int!
Expand Down Expand Up @@ -363,7 +363,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3", as: "fed2")
type Product @fed2__key(fields: "upc") {
upc: String!
Expand All @@ -376,7 +376,7 @@ def execute_sdl(schema)
)
end

it 'returns valid SDL for @external directives' do
it 'returns valid SDL for @external directives with custom namespace' do
product = Class.new(base_object) do
graphql_name 'Product'
extend_type
Expand All @@ -394,7 +394,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3", as: "fed2")
type Product @fed2__extends @fed2__key(fields: "upc") {
price: Int
Expand All @@ -403,6 +403,32 @@ def execute_sdl(schema)
GRAPHQL
)
end

it 'returns valid SDL for @interfaceObject directives with custom namespace' do
product = Class.new(base_object) do
graphql_name 'Product'
interface_object
key fields: :id

field :id, 'ID', null: false
end

schema = Class.new(base_schema) do
orphan_types product
federation version: '2.3', link: { as: 'fed2' }
end

expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.3", as: "fed2")
type Product @fed2__interfaceObject @fed2__key(fields: "id") {
id: ID!
}
GRAPHQL
)
end
end

it 'returns valid SDL for inaccessible interface types' do
Expand Down Expand Up @@ -444,7 +470,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Book implements Product {
upc: String!
Expand Down Expand Up @@ -507,7 +533,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Book implements Product @federation__extends @federation__key(fields: "upc") {
upc: String! @federation__external
Expand Down Expand Up @@ -547,7 +573,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Product @federation__key(fields: "upc") {
upc: String!
Expand Down Expand Up @@ -578,7 +604,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Product @federation__key(fields: "upc") {
upc: String!
Expand Down Expand Up @@ -606,7 +632,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Product @federation__key(fields: "upc") @federation__key(fields: "name") {
name: String
Expand Down Expand Up @@ -634,7 +660,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Product @federation__extends @federation__key(fields: "upc") {
price: Int
Expand All @@ -644,6 +670,32 @@ def execute_sdl(schema)
)
end

it 'returns valid SDL for @interfaceObject directives' do
product = Class.new(base_object) do
graphql_name 'Product'
interface_object
key fields: :id

field :id, 'ID', null: false
end

schema = Class.new(base_schema) do
orphan_types product
federation version: '2.3'
end

expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Product @federation__interfaceObject @federation__key(fields: "id") {
id: ID!
}
GRAPHQL
)
end

it 'returns valid SDL for @shareable directives' do
position = Class.new(base_object) do
graphql_name 'Position'
Expand All @@ -666,7 +718,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Position {
x: Int! @federation__shareable
Expand Down Expand Up @@ -702,7 +754,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Position {
x: Int! @federation__inaccessible
Expand Down Expand Up @@ -734,7 +786,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Product @federation__extends @federation__key(fields: "id") {
id: ID!
Expand Down Expand Up @@ -770,7 +822,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Product @federation__extends @federation__key(fields: "upc") {
price: Int
Expand Down Expand Up @@ -805,7 +857,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Product @federation__extends @federation__key(fields: "upc") {
price: Int @federation__external
Expand Down Expand Up @@ -834,7 +886,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Product @federation__key(fields: "productId") {
productId: String!
Expand Down Expand Up @@ -862,7 +914,7 @@ def execute_sdl(schema)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Product @federation__extends @federation__key(fields: "product_id") {
options: [String!]! @federation__requires(fields: "my_id")
Expand Down Expand Up @@ -895,7 +947,7 @@ def self.visible?(context)
expect(execute_sdl(schema)).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0")
@link(url: "https://specs.apollo.dev/federation/v2.3")
type Product @federation__extends @federation__key(fields: "upc") {
upc: String! @federation__external
Expand Down

0 comments on commit c7b987d

Please sign in to comment.