Skip to content
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

fix: Respect Federation version in @link directive #222

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions lib/apollo-federation/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,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_preamble) if federation_2?
output
end

Expand All @@ -56,12 +56,12 @@ def query(new_query_object = nil)

private

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

<<~SCHEMA
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.3"#{federation_namespace}, import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v#{federation_version}"#{federation_namespace}, import: ["@inaccessible"])

SCHEMA
end
Expand Down
58 changes: 58 additions & 0 deletions spec/apollo-federation/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,62 @@
expect(schema.federation_2?).to be(true)
end
end

describe '.federation_sdl' do
let(:base_object) do
base_field = Class.new(GraphQL::Schema::Field) do
include ApolloFederation::Field
end

Class.new(GraphQL::Schema::Object) do
include ApolloFederation::Object
field_class base_field
end
end

let(:query_type) do
Class.new(base_object) do
graphql_name 'Query'

field :test, String, null: false
end
end

it 'returns a federation 1 schema by default' do
# can't use a let-defined identifier inside the below class definition?
qt = query_type
schema = Class.new(GraphQL::Schema) do
include ApolloFederation::Schema
query qt
end

expect(schema.federation_sdl).to match_sdl(
<<~GRAPHQL,
type Query {
test: String!
}
GRAPHQL
)
end

it 'returns a schema directive with the given federation version if specified' do
qt = query_type
schema = Class.new(GraphQL::Schema) do
include ApolloFederation::Schema
query qt
federation version: '2.1'
end

expect(schema.federation_sdl).to match_sdl(
<<~GRAPHQL,
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.1", import: ["@inaccessible"])

type Query {
test: String!
}
GRAPHQL
)
end
end
end
46 changes: 23 additions & 23 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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Position @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.3", as: "fed2", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "fed2", import: ["@inaccessible"])

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.3", as: "fed2", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "fed2", import: ["@inaccessible"])

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.3", as: "fed2", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "fed2", import: ["@inaccessible"])

type Position @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.3", as: "fed2", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "fed2", import: ["@inaccessible"])

type Product @fed2__key(fields: "upc") {
upc: String!
Expand Down Expand Up @@ -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.3", as: "fed2", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "fed2", import: ["@inaccessible"])

type Product @fed2__extends @fed2__key(fields: "upc") {
price: Int
Expand Down Expand Up @@ -470,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Book implements Product {
upc: String!
Expand Down Expand Up @@ -533,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Book implements Product @federation__extends @federation__key(fields: "upc") {
upc: String! @federation__external
Expand Down Expand Up @@ -573,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Product @federation__key(fields: "upc") {
upc: String!
Expand Down Expand Up @@ -604,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Product @federation__key(fields: "upc") {
upc: String!
Expand Down Expand Up @@ -632,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Product @federation__key(fields: "upc") @federation__key(fields: "name") {
name: String
Expand Down Expand Up @@ -660,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Product @federation__extends @federation__key(fields: "upc") {
price: Int
Expand Down Expand Up @@ -718,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Position {
x: Int! @federation__shareable
Expand Down Expand Up @@ -754,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Position {
x: Int! @inaccessible
Expand Down Expand Up @@ -786,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Product @federation__extends @federation__key(fields: "id") {
id: ID!
Expand Down Expand Up @@ -822,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Product @federation__extends @federation__key(fields: "upc") {
price: Int
Expand Down Expand Up @@ -857,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Product @federation__extends @federation__key(fields: "upc") {
price: Int @federation__external
Expand Down Expand Up @@ -886,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Product @federation__key(fields: "productId") {
productId: String!
Expand Down Expand Up @@ -914,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Product @federation__extends @federation__key(fields: "product_id") {
options: [String!]! @federation__requires(fields: "my_id")
Expand Down Expand Up @@ -947,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.3", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@inaccessible"])

type Product @federation__extends @federation__key(fields: "upc") {
upc: String! @federation__external
Expand Down