Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: apollo-federation/src/sources/connect/models/validation.rs
expression: "format!(\"{:?}\", errors)"
input_file: apollo-federation/src/sources/connect/models/test_data/invalid_entity_arg_on_field.graphql
---
[Message { code: EntityNotOnRootQuery, message: "`@connect(entity:)` on `User.bestFriend` is invalid. Entities can only be declared on root `Query` fields.", locations: [Location { line: 15, column: 61 }..Location { line: 15, column: 73 }] }]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: apollo-federation/src/sources/connect/models/validation.rs
expression: "format!(\"{:?}\", errors)"
input_file: apollo-federation/src/sources/connect/models/test_data/valid_entity_arg_on_field.graphql
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
extend schema
@link(
url: "https://specs.apollo.dev/connect/v0.1"
import: ["@connect"]
)

type Query {
user: User
@connect(http: {GET: "http://127.0.0.1:8000/resources"}, entity: true)
}

type User {
id: ID!
name: String!
bestFriend: User
@connect(http: {GET: "http://127.0.0.1:8000/resources"}, entity: true)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extend schema
@link(
url: "https://specs.apollo.dev/connect/v0.1"
import: ["@connect"]
)

type Query {
user: User
@connect(http: {GET: "http://127.0.0.1:8000/resources"}, entity: true)
}

type User {
id: ID!
name: String!
}
33 changes: 33 additions & 0 deletions apollo-federation/src/sources/connect/models/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use itertools::Itertools;
use url::Url;

use crate::link::Link;
use crate::sources::connect::spec::schema::CONNECT_ENTITY_ARGUMENT_NAME;
use crate::sources::connect::spec::schema::CONNECT_HTTP_ARGUMENT_DELETE_METHOD_NAME;
use crate::sources::connect::spec::schema::CONNECT_HTTP_ARGUMENT_GET_METHOD_NAME;
use crate::sources::connect::spec::schema::CONNECT_HTTP_ARGUMENT_NAME;
Expand Down Expand Up @@ -369,6 +370,28 @@ fn validate_field(
),
)
});

if let Some(entity_arg) = connect_directive
.arguments
.iter()
.find(|arg| arg.name == CONNECT_ENTITY_ARGUMENT_NAME)
{
if entity_arg
.value
.to_bool()
.is_some_and(|entity_arg_value| entity_arg_value)
&& category != ObjectCategory::Query
{
errors.push(Message {
code: Code::EntityNotOnRootQuery,
message: format!("{coordinate} is invalid. Entities can only be declared on root `Query` fields.", coordinate = connect_directive_entity_argument_coordinate(connect_directive_name, object_name, &field.name)),
locations: Location::from_node(entity_arg.location(), source_map)
.into_iter()
.collect(),
})
}
}

if let Some(source_name) = connect_directive
.arguments
.iter()
Expand Down Expand Up @@ -500,6 +523,14 @@ fn connect_directive_name_coordinate(
format!("`@{connect_directive_name}({CONNECT_SOURCE_ARGUMENT_NAME}: {source})` on `{object}.{field}`")
}

fn connect_directive_entity_argument_coordinate(
connect_directive_entity_argument: &Name,
object: &Name,
field: &Name,
) -> String {
format!("`@{connect_directive_entity_argument}({CONNECT_ENTITY_ARGUMENT_NAME}:)` on `{object}.{field}`")
}

fn connect_directive_http_coordinate(
connect_directive_name: &Name,
object: &Name,
Expand Down Expand Up @@ -730,6 +761,8 @@ pub enum Code {
MultipleHttpMethods,
/// The `@connect` directive is missing an HTTP method.
MissingHttpMethod,
// The `entity` argument should only be used on the root `Query` field
EntityNotOnRootQuery,
}

impl Code {
Expand Down