Skip to content

Commit

Permalink
fix(federation): output key directive to entity sdl (#2973)
Browse files Browse the repository at this point in the history
  • Loading branch information
meskill authored Oct 7, 2024
1 parent 459ef4e commit 024d891
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 42 deletions.
24 changes: 0 additions & 24 deletions generated/.tailcallrc.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -833,18 +833,6 @@
"title": "JSON",
"description": "Field whose value conforms to the standard JSON format as specified in RFC 8259 (https://datatracker.ietf.org/doc/html/rfc8259)."
},
"Key": {
"description": "Directive `@key` for Apollo Federation",
"type": "object",
"required": [
"fields"
],
"properties": {
"fields": {
"type": "string"
}
}
},
"KeyValue": {
"type": "object",
"required": [
Expand Down Expand Up @@ -1448,18 +1436,6 @@
},
"uniqueItems": true
},
"key": {
"description": "Apollo federation key directive. skip since it's set automatically by config transformer",
"writeOnly": true,
"anyOf": [
{
"$ref": "#/definitions/Key"
},
{
"type": "null"
}
]
},
"protected": {
"description": "Marks field as protected by auth providers",
"default": null,
Expand Down
8 changes: 1 addition & 7 deletions src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tailcall_typedefs_common::ServiceDocumentBuilder;
use super::directive::Directive;
use super::from_document::from_document;
use super::{
AddField, Alias, Cache, Call, Expr, GraphQL, Grpc, Http, Key, Link, Modify, Omit, Protected,
AddField, Alias, Cache, Call, Expr, GraphQL, Grpc, Http, Link, Modify, Omit, Protected,
Resolver, Server, Telemetry, Upstream, JS,
};
use crate::core::config::npo::QueryPath;
Expand Down Expand Up @@ -119,12 +119,6 @@ pub struct Type {
#[serde(flatten, default, skip_serializing_if = "is_default")]
pub resolver: Option<Resolver>,

///
/// Apollo federation key directive.
/// skip since it's set automatically by config transformer
#[serde(skip_serializing)]
pub key: Option<Key>,

///
/// Any additional directives
#[serde(default, skip_serializing_if = "is_default")]
Expand Down
2 changes: 0 additions & 2 deletions src/core/config/config_module/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ impl Contravariant for Type {
cache: self.cache.merge_right(other.cache),
protected: self.protected.merge_right(other.protected),
resolver: self.resolver.merge_right(other.resolver),
key: self.key.merge_right(other.key),
directives: self.directives.merge_right(other.directives),
})
}
Expand All @@ -155,7 +154,6 @@ impl Covariant for Type {
cache: self.cache.merge_right(other.cache),
protected: self.protected.merge_right(other.protected),
resolver: self.resolver.merge_right(other.resolver),
key: self.key.merge_right(other.key),
directives: self.directives.merge_right(other.directives),
})
}
Expand Down
1 change: 0 additions & 1 deletion src/core/config/from_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ where
cache,
protected,
resolver,
key: None,
directives: unknown_directives,
}
},
Expand Down
1 change: 0 additions & 1 deletion src/core/config/into_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ fn type_directives(type_def: &crate::core::config::Type) -> Vec<Positioned<Const
.and_then(|resolver| resolver.to_directive())
.map(pos),
)
.chain(type_def.key.as_ref().map(|key| pos(key.to_directive())))
.chain(into_directives(&type_def.directives))
.collect::<Vec<_>>()
}
Expand Down
16 changes: 11 additions & 5 deletions src/core/config/transformer/subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::ops::Deref;

use tailcall_macros::MergeRight;

use crate::core::config::directive::to_directive;
use crate::core::config::{
self, ApolloFederation, Arg, Call, Config, Field, GraphQL, Grpc, Http, Key, KeyValue, Resolver,
Union,
Expand Down Expand Up @@ -45,13 +46,18 @@ impl Transform for Subgraph {
if let Some(resolver) = &ty.resolver {
resolver_by_type.insert(type_name.clone(), resolver.clone());

KeysExtractor::extract_keys(resolver).map(|fields| {
fields.map(|fields| {
ty.key = Some(Key { fields });
})
KeysExtractor::extract_keys(resolver).and_then(|fields| match fields {
Some(fields) => {
let key = Key { fields };

to_directive(key.to_directive()).map(|directive| {
ty.directives.push(directive);
})
}
None => Valid::succeed(()),
})
} else {
Valid::succeed(None)
Valid::succeed(())
}
.trace(type_name)
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ expression: response
"body": {
"data": {
"_service": {
"sdl": "schema {\n query: Query\n}\n\nscalar _Any\n\nunion _Entity = Post | User\n\ntype Post {\n id: Int!\n title: String!\n}\n\ntype Query {\n _entities(representations: [_Any!]!): [_Entity]!\n _service: _Service!\n user(id: Int!): User\n}\n\ntype User {\n id: Int!\n name: String!\n}\n\ntype _Service {\n sdl: String\n}\nextend schema @link(\n\turl: \"https://specs.apollo.dev/federation/v2.3\",\n\timport: [\"@key\", \"@tag\", \"@shareable\", \"@inaccessible\", \"@override\", \"@external\", \"@provides\", \"@requires\", \"@composeDirective\", \"@interfaceObject\"]\n)\n"
"sdl": "schema {\n query: Query\n}\n\nscalar _Any\n\nunion _Entity = Post | User\n\ntype Post @key(fields: \"id\") {\n id: Int!\n title: String!\n}\n\ntype Query {\n _entities(representations: [_Any!]!): [_Entity]!\n _service: _Service!\n user(id: Int!): User\n}\n\ntype User @key(fields: \"id\") {\n id: Int!\n name: String!\n}\n\ntype _Service {\n sdl: String\n}\nextend schema @link(\n\turl: \"https://specs.apollo.dev/federation/v2.3\",\n\timport: [\"@key\", \"@tag\", \"@shareable\", \"@inaccessible\", \"@override\", \"@external\", \"@provides\", \"@requires\", \"@composeDirective\", \"@interfaceObject\"]\n)\n"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/core/snapshots/apollo-federation-entities.md_1.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ expression: response
"body": {
"data": {
"_service": {
"sdl": "schema {\n query: Query\n}\n\nscalar _Any\n\nunion _Entity = Post | User\n\ntype Post {\n id: Int!\n title: String!\n}\n\ntype Query {\n _entities(representations: [_Any!]!): [_Entity]!\n _service: _Service!\n user(id: Int!): User\n}\n\ntype User {\n id: Int!\n name: String!\n}\n\ntype _Service {\n sdl: String\n}\nextend schema @link(\n\turl: \"https://specs.apollo.dev/federation/v2.3\",\n\timport: [\"@key\", \"@tag\", \"@shareable\", \"@inaccessible\", \"@override\", \"@external\", \"@provides\", \"@requires\", \"@composeDirective\", \"@interfaceObject\"]\n)\n"
"sdl": "schema {\n query: Query\n}\n\nscalar _Any\n\nunion _Entity = Post | User\n\ntype Post @key(fields: \"id\") {\n id: Int!\n title: String!\n}\n\ntype Query {\n _entities(representations: [_Any!]!): [_Entity]!\n _service: _Service!\n user(id: Int!): User\n}\n\ntype User @key(fields: \"id\") {\n id: Int!\n name: String!\n}\n\ntype _Service {\n sdl: String\n}\nextend schema @link(\n\turl: \"https://specs.apollo.dev/federation/v2.3\",\n\timport: [\"@key\", \"@tag\", \"@shareable\", \"@inaccessible\", \"@override\", \"@external\", \"@provides\", \"@requires\", \"@composeDirective\", \"@interfaceObject\"]\n)\n"
}
}
}
Expand Down

1 comment on commit 024d891

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 7.40ms 3.24ms 40.01ms 72.53%
Req/Sec 3.43k 406.17 3.92k 94.08%

409732 requests in 30.02s, 784.01MB read

Requests/sec: 13647.21

Transfer/sec: 26.11MB

Please sign in to comment.