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 send field to the service if it cannot process it #864

Merged
merged 1 commit into from
Jan 10, 2022

Conversation

konalegi
Copy link

@konalegi konalegi commented Jul 8, 2021

Problem descrion

When the resolved field type is shared interface and this interface is implemented in different services by types that aren't shared query planner would create queries that cause No such type ... errors.

Example

Service A:

type Query {
  myField: MyInterface
}

type MyInterface {
  name: String
}

type MyTypeA implements MyInterface {
  name: String
}

type MyTypeB implements MyInterface {
  name: String
}

Service B:

interface MyInterface {
 name: String
}

type MyTypeC implements MyInterface {
  name: String
}

The query:

query {
  myField {
    ...MyIntFragment
  }
}

fragment MyIntFragment on MyInterface {
  name
}

result in such query to service A:

query {
  myField {
    ...on MyTypeA { name }
    ...on MyTypeB { name }
    ...on MyTypeC { name }
  }
}

which cause an error

Unknown type "MyTypeC".

@apollo-cla
Copy link

@konalegi: 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/

@dchambers
Copy link
Contributor

@konalegi thanks for providing this PR. Any chance you can sign the Apollo CLA for it?

@konalegi
Copy link
Author

@dchambers done

Copy link
Contributor

@pcmanus pcmanus left a comment

Choose a reason for hiding this comment

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

Thanks for the patch.

As pointed in a comment, the patch creates a problem with value types, but this can be fixed easily enough. It's unfortunate that no test catch that, so I think we'd need to add at least 2 tests: one for the fix here, and one that show the value-type case is not broken. But with the fix and tests, I think this could be committed.

query-planner-js/src/buildQueryPlan.ts Show resolved Hide resolved
const { fieldDef } = fields[0];
const returnType = getNamedType(fieldDef.type);

if (isCompositeType(returnType)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious as to why you added this exclusion? I don't mind keeping it as it limits the cases changed by this fix and is thus somewhat more conservative, but given the goal is to exclude fields from services that don't the field at all, I'm not sure to see why the field return type would influence the behaviour.

Mostly wondering if you were thinking of a case I don't have in mind.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, that was the intention, to make changes as little as possible, because I'm not very familiar with the query planner.

return true;
}

const owningService = context.getOwningService(runtimeParentType, fieldDef);
Copy link
Contributor

Choose a reason for hiding this comment

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

We actually need to handle the case where owningService === undefined (and return true). Otherwise this will create an issue for value types as they don't have an owningService and would always be skipped, which we don't want.

An example of this would be if a subgraph has:

type Query {
  itf: Itf
}

interface Itf {
  f: String
}

type T1 implements Itf @key(fields: "f") {
  f: String
}

type T2 implements Itf {
  f: String
}

With the patch, if you query:

{
  itf {
    f
  }
}

then the query plan will include T1 but not T2 due to this problem.

@konalegi
Copy link
Author

konalegi commented Dec 15, 2021

Thanks for your review @pcmanus, I've tried to address all your comments. Could you please take a look again? Thanks!

@pcmanus pcmanus merged commit 345d5f0 into apollographql:version-0.x Jan 10, 2022
@pcmanus
Copy link
Contributor

pcmanus commented Jan 10, 2022

Slowly for the slow turn-around. The last changes look good to me, thanks!

@konalegi
Copy link
Author

@pcmanus thanks for your help with the PR :) Do you know when it's going to be released?

@dchambers
Copy link
Contributor

dchambers commented Jan 10, 2022

Great work getting a fix for this out there gents!

@pcmanus
Copy link
Contributor

pcmanus commented Jan 11, 2022

Do you know when it's going to be released?

I believe we're planning to do the next release relatively soon (tracked at #1369). I don't have an exact date but I'd say "within a few days".

@trevor-scheer
Copy link
Member

I'll be handling the release for this likely by the end of the week. It's actually currently available via the gateway alpha (you'll also need to install the query-planner alpha to force its usage)
npm i @apollo/gateway@alpha @apollo/query-planner@alpha

Would someone please share a changelog entry here so I can add it before release? And is it fair to say this is just a patch (non-breaking bug fix)?

@pcmanus
Copy link
Contributor

pcmanus commented Jan 11, 2022

Would someone please share a changelog entry here

I already took the liberty to ninja-push one (acf3ffe) on the version-0.x branch. But yeah, it's a non-breaking bug fix.

@konalegi
Copy link
Author

I'm not sure if the changelog is still required, but I'll provide it, "Not owning types are not added into a query on interface unpacking" not sure if it's very accurate though.

@trevor-scheer
Copy link
Member

This fix is released in the latest versions of federation packages. Thanks again for providing the fix!

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.

5 participants