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

test fragment in requires directive #3978

Merged
merged 25 commits into from
Nov 30, 2023
Merged

test fragment in requires directive #3978

merged 25 commits into from
Nov 30, 2023

Conversation

Geal
Copy link
Contributor

@Geal Geal commented Oct 5, 2023

related to #3972
Fix #3983 (not yet, this only contains tests for now)
Fix #4052

The representation selection code in

match (&fragment.type_condition, &content.get("__typename")) {

should be able to recognize the current type even when __typename is not present if it's a concrete type. This could be achieved by passing the query compiler along the selection code, or using the Schema representation.

The plan right now:

  • I am trying to simplify the selection code a bit, but it will have to be refactored to follow more closely the behaviour for @requires: https://www.apollographql.com/docs/federation/entities-advanced/#contributing-computed-entity-fields
  • I'll pass a ParsedDocument to Variables::new (getting it from context)
  • we need a function that takes an ExecutableDocument and a Path and returns a Field with its type and selection set
  • we pass that selection set to select_object and friends so we know exactly which type is required where

could we work with the schema instead of the query document? (since the schema is already transmitted there).
we may need to work from the query because the path depends on the query(example: there was an alias somewhere).
we would need to work with the schema because the @requires selection are not present in the query.

can we fix the requires selection by post-processing the query plan to add type information then caching the result?


Checklist

Complete the checklist (and note appropriate exceptions) before the PR is marked ready-for-review.

  • Changes are compatible1
  • Documentation2 completed
  • Performance impact assessed and acceptable
  • Tests added and passing3
    • Unit Tests
    • Integration Tests
    • Manual Tests

Exceptions

Note any exceptions here

Notes

Footnotes

  1. It may be appropriate to bring upcoming changes to the attention of other (impacted) groups. Please endeavour to do this before seeking PR approval. The mechanism for doing this will vary considerably, so use your judgement as to how and when to do this.

  2. Configuration is an important part of many changes. Where applicable please try to document configuration examples.

  3. Tick whichever testing boxes are applicable. If you are adding Manual Tests, please document the manual testing (extensively) in the Exceptions.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 5, 2023

@Geal, please consider creating a changeset entry in /.changesets/. These instructions describe the process and tooling.

@router-perf
Copy link

router-perf bot commented Oct 5, 2023

CI performance tests

  • events_big_cap_high_rate - Stress test for events with a lot of users, deduplication enabled and high rate event with a big queue capacity
  • events_without_dedup - Stress test for events with a lot of users and deduplication DISABLED
  • events - Stress test for events with a lot of users and deduplication ENABLED
  • large-request - Stress test with a 1 MB request payload
  • step - Basic stress test that steps up the number of users over time
  • xlarge-request - Stress test with 10 MB request payload
  • reload - Reload test over a long period of time at a constant rate of users
  • no-graphos - Basic stress test, no GraphOS.
  • xxlarge-request - Stress test with 100 MB request payload
  • step-jemalloc-tuning - Clone of the basic stress test for jemalloc tuning
  • const - Basic stress test that runs with a constant number of users

@@ -48,97 +48,171 @@ pub(crate) struct InlineFragment {
pub(crate) selections: Vec<Selection>,
}

pub(crate) fn select_object(
content: &Object,
pub(crate) fn execute_selection_set<'a>(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this replaces our initial implementation with a function that closely follows what is happening in the gateway's executeSelectionSet function

Comment on lines +93 to +115
// if the __typename field was missing but we can infer it, fill it
if let Some(ty) = current_type {
output.insert(
ByteString::from(selection_name.to_owned()),
Value::String(ByteString::from(ty.to_owned())),
);
continue;
}
}
// the behaviour here does not align with the gateway: we should instead assume that
// data is in the correct shape, and return a null (or even no value at all) on
// missing fields. If a field was missing, it should have been nullified,
// and if it was non nullable, the parent object would have been nullified.
// Unfortunately, we don't validate subgraph responses yet
if field_type
.as_ref()
.map(|ty| !ty.is_non_null())
.unwrap_or(false)
{
output.insert(ByteString::from(selection_name.to_owned()), Value::Null);
} else {
return Value::Null;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not entirely confident about this part because it differs from the gateway's implementation. The rationale from the gateway's side is that a missing field in the data we select from should not have happened, and should have been caught by response validation, but I don't see where this validation is happening

Copy link
Contributor

Choose a reason for hiding this comment

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

@trevor-scheer @clenfest is something you could help us figure out? 🙏

Copy link
Contributor

Choose a reason for hiding this comment

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

From memory (a long time ago) I was under the impression that we never validated responses from subgraphs as this was an expensive operation.

Comment on lines +596 to +605
if name.as_str() == TYPENAME {
let input_value = input
.get(field_name.as_str())
.cloned()
.filter(|v| v.is_string())
.unwrap_or_else(|| {
Value::String(ByteString::from(
parent_type.inner_named_type().as_str().to_owned(),
))
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is specifically a check to replace a null __typename with the type we currently know. Maybe in general, we should make sure to replace the typename with the one we know from the query and the API schema

@Geal Geal requested review from a team, SimonSapin, bnjjj and o0Ignition0o October 26, 2023 09:23
@Geal Geal marked this pull request as ready for review October 26, 2023 09:23
}

#[tokio::test]
async fn typename_propagation2() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this test is still failing. I do not know yet if we should fix it (it requires some work to find out the right type) or accept that the implementation does what it can, and won't cover all cases (missing __typename can generally be fixed by adding it to the @requires selection)

Copy link
Contributor

Choose a reason for hiding this comment

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

What shall we do with this? Raise a followup ticket?

Copy link
Contributor

@o0Ignition0o o0Ignition0o left a comment

Choose a reason for hiding this comment

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

Looking good overall, i just got a bit nitpicky trying to understand things.

The failing test i d love us to investigate a bit (looks like a breaking change?)

@@ -131,6 +131,7 @@ impl Variables {
request: &Arc<http::Request<Request>>,
schema: &Schema,
input_rewrites: &Option<Vec<rewrites::DataRewrite>>,
//document: &ParsedDocument,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
//document: &ParsedDocument,

let mut output = Object::with_capacity(selections.len());
for selection in selections {
println!(
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
println!(
tracing::trace!(

Comment on lines +93 to +115
// if the __typename field was missing but we can infer it, fill it
if let Some(ty) = current_type {
output.insert(
ByteString::from(selection_name.to_owned()),
Value::String(ByteString::from(ty.to_owned())),
);
continue;
}
}
// the behaviour here does not align with the gateway: we should instead assume that
// data is in the correct shape, and return a null (or even no value at all) on
// missing fields. If a field was missing, it should have been nullified,
// and if it was non nullable, the parent object would have been nullified.
// Unfortunately, we don't validate subgraph responses yet
if field_type
.as_ref()
.map(|ty| !ty.is_non_null())
.unwrap_or(false)
{
output.insert(ByteString::from(selection_name.to_owned()), Value::Null);
} else {
return Value::Null;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@trevor-scheer @clenfest is something you could help us figure out? 🙏

Comment on lines 157 to 160
println!(
"is_object_of_type({condition})={b} for {}",
serde_json::to_string(&content).unwrap(),
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
println!(
"is_object_of_type({condition})={b} for {}",
serde_json::to_string(&content).unwrap(),
);
tracing::trace!(
"is_object_of_type({condition})={b} for {}",
serde_json::to_string(&content).unwrap(),
);

Comment on lines 181 to 184
println!(
"execute_selection_set: output is now: {}",
serde_json::to_string(&output).unwrap(),
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
println!(
"execute_selection_set: output is now: {}",
serde_json::to_string(&output).unwrap(),
);
tracing::trace!(
"execute_selection_set: output is now: {}",
serde_json::to_string(&output).unwrap(),
);

Comment on lines +210 to +215
if conditional_type.is_interface() || conditional_type.is_union() {
return (object_type.is_object() || object_type.is_interface())
&& schema.is_subtype(condition, typename);
}

false
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if conditional_type.is_interface() || conditional_type.is_union() {
return (object_type.is_object() || object_type.is_interface())
&& schema.is_subtype(condition, typename);
}
false
(conditional_type.is_interface() || conditional_type.is_union()).then(|| (object_type.is_object() || object_type.is_interface())
&& schema.is_subtype(condition, typename)).unwrap_or(false)

not tested, i just wanted to be nitpicky 😂

Comment on lines 249 to 255
/*match (value, selections) {
(Value::Object(content), requires) => {
select_object(content, requires, schema).transpose()
select_object(content, requires, schema) //.transpose()
}
(_, _) => Some(Err(FetchError::ExecutionInvalidContent {
(_, _) => Err(FetchError::ExecutionInvalidContent {
reason: "not an object".to_string(),
})),
}),*/
Copy link
Contributor

Choose a reason for hiding this comment

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

uh did we remove a test? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, the select_object was just replaced with execute_selection_set

))
.collect::<Vec<_>>(),
));
println!("select res: {res:?}");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
println!("select res: {res:?}");

.await
.unwrap();

println!("\n\n==================QUERYBOOK2=============\n\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
println!("\n\n==================QUERYBOOK2=============\n\n");

}

#[tokio::test]
async fn typename_propagation2() {
Copy link
Contributor

Choose a reason for hiding this comment

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

What shall we do with this? Raise a followup ticket?

@Geal
Copy link
Contributor Author

Geal commented Nov 30, 2023

What shall we do with this? Raise a followup ticket?

This is a case that this PR can't fix. We will need to revisit this when we get deeper integration with the compiler and the new planner

@Geal Geal merged commit 4a592f4 into dev Nov 30, 2023
12 checks passed
@Geal Geal deleted the geal/fragment-in-requires branch November 30, 2023 16:06
@Geal Geal mentioned this pull request Aug 20, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants