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

do not perform nested fetches if the parent one returned null #1332

Merged
merged 13 commits into from
Aug 1, 2022

Conversation

Ty3uK
Copy link
Contributor

@Ty3uK Ty3uK commented Jun 30, 2022

Related to https://github.com/apollographql/federation/blob/main/gateway-js/src/executeQueryPlan.ts#L179

If I have two subgraphs (simplified):

type Mutation {
	mutationA: Mutation
}
type Mutation {
	mutationB: Boolean
}

And run mutation like this:

mutation {
	mutationA {
		mutationB
	}
}

Then, mutationB will be called even if mutationA returns null

@apollo-cla
Copy link

@Ty3uK: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@netlify
Copy link

netlify bot commented Jun 30, 2022

Deploy Preview for apollo-router-docs canceled.

Built without sensitive environment variables

Name Link
🔨 Latest commit c228ce9
🔍 Latest deploy log https://app.netlify.com/sites/apollo-router-docs/deploys/62be2495d1cc85000841f13d

@Geal Geal self-requested a review June 30, 2022 15:38
@@ -167,6 +167,22 @@ impl PlanNode {
let mut value;
let mut errors;

if !current_dir.is_empty() {
// Check if parent data has field requested by Flatten
match parent_value.pointer(&current_dir.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.

it's not the right place to do that. The check should be performed at variables aggregation in https://github.com/apollographql/router/blob/main/apollo-router/src/query_planner/mod.rs#L492-L508
as you can see there's already a check to avoid a subquery if the list of variables is empty, this needs an additional check that the variables are not 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.

Thanks for feedback!
Do I need to perform this check inside match?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, the problem is that with my example, variables are empty object ({}) in both cases

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Geal, fixed, hope I understood you right. 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry 😓 I was not very clear, I think it has to be done inside Variables::new. If you want I can take a closer look at it and add some tests

Copy link
Contributor Author

@Ty3uK Ty3uK Jul 1, 2022

Choose a reason for hiding this comment

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

@Geal,

I have free time, so I can do it by myself 🙂

One question: do I need to add this check inside this else branch?

And another question - do I need to extend existing tests or create a separate one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Geal, check latest commit please 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks, looking into it

@Ty3uK Ty3uK force-pushed the bugfix/flatten-null branch from d900e38 to c70b006 Compare June 30, 2022 21:41
@Geal
Copy link
Contributor

Geal commented Jul 4, 2022

so, we need a bit more context to evaluate this. The code looks ok but I don't understand the use case enough to decide. Correct me if I'm wrong: this is used to "batch" mutations and do them in a specific order, right? Could you tell more about the kind of use case that warrants this design?

Could you add a test to validate this code? There are examples to create a router and subgraph services in https://github.com/apollographql/router/blob/main/apollo-router/src/query_planner/mod.rs#L687

Thank you for taking the time to patch this :)

@Ty3uK
Copy link
Contributor Author

Ty3uK commented Jul 4, 2022

@Geal, let's look into some production code from my work 🙂

Schema
type Captcha_Captcha {
    token: String!
    image: String!
}

input Captcha_CaptchaCreateInput {
    width: Int!
    height: Int!
}

type Captcha_CaptchaCreateError implements Error @shareable {
    message: String!
}

type Captcha_CaptchaCreateLimitError implements Error @shareable {
    message: String!
}

input Captcha_CaptchaVerifyInput {
    token: String!
    text: String!
}

type Captcha_CaptchaVerificationInputError implements Error @shareable {
    message: String!
}

type Captcha_CaptchaVerificationError implements Error @shareable {
    message: String!
}

type Captcha_CaptchaVerificationLimitError implements Error @shareable {
    message: String!
}

union Captcha_CaptchaCreateResult =
    | Captcha_Captcha
    | Captcha_CaptchaCreateError
    | Captcha_CaptchaCreateLimitError

type Captcha_CaptchaVerifyResult {
    error: Error
    mutation: Mutation
}

type Captcha_CaptchaMutations {
    create(input: Captcha_CaptchaCreateInput): Captcha_CaptchaCreateResult!
    verify(input: Captcha_CaptchaVerifyInput!): Captcha_CaptchaVerifyResult!
}

extend type Mutation {
    captcha: Captcha_CaptchaMutations
}

With this schema, I can verify captcha and do next mutation in one request. As I know, there is no limitation for doing such thing 🙂 And, as I say in first message, @apollo/graphql doesn't have this problem.

Question about testing: do I need to change existing schema.graphql or create a separate one?

@Geal
Copy link
Contributor

Geal commented Jul 4, 2022

if you can make the test schema smaller it would be nice, but otherwise that could work

@Ty3uK
Copy link
Contributor Author

Ty3uK commented Jul 6, 2022

@Geal, I'm so sorry, I don't have enough experience in Rust to write tests 😕
I pushed schema and query plan, can I ask you to write the test?

@Geal
Copy link
Contributor

Geal commented Jul 15, 2022

just FYI, I have not forgotten about your PR, I had to handle other features this week, I'll get back to this one next week to write the test

Geoffroy Couprie added 5 commits July 25, 2022 15:02
so we won't need to convert the path to string then reparse it
they are small enough to get inlined
@Geal Geal requested review from o0Ignition0o, BrynCooke and Geal July 25, 2022 14:38
@Geal Geal changed the title Fix execution of flatten when fetch in sequence returns null do not perform nested fetches if the parent one returned null Jul 25, 2022
# Conflicts:
#	apollo-router/src/query_planner/mod.rs
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.

This looks great!

@Ty3uK
Copy link
Contributor Author

Ty3uK commented Aug 1, 2022

@Geal, @BrynCooke, @o0Ignition0o
Thank you so much!
Can you tell me what I need to do next? 🙂

@o0Ignition0o
Copy link
Contributor

@Ty3uK There seems to be a merge conflict, i thinks you want to update the main branch and solve conflicts, do you know how to do this or would you like us to pair on that ? :)

@Ty3uK
Copy link
Contributor Author

Ty3uK commented Aug 1, 2022

@o0Ignition0o, thanks, I'll do it 🙂

@Ty3uK
Copy link
Contributor Author

Ty3uK commented Aug 1, 2022

@o0Ignition0o, I merged main into my branch without conflicts (as I can see) 🙂

@o0Ignition0o
Copy link
Contributor

Awesome!

Let's run the Continuous Integration checks, it looks like you are getting super close to landing your first contribution to the router! 🏅

@o0Ignition0o
Copy link
Contributor

Tests are passing on my machine, let's land this!

@o0Ignition0o o0Ignition0o enabled auto-merge (squash) August 1, 2022 11:12
@o0Ignition0o o0Ignition0o merged commit 79a2080 into apollographql:main Aug 1, 2022
@garypen garypen added this to the v0.14.0 milestone Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants