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

Fix Missing Referenced Fragments in Query Document #2647

Merged
merged 7 commits into from
Oct 13, 2020

Conversation

tylerbwong
Copy link
Contributor

Fixes #2644.

@apollo-cla
Copy link

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

Copy link
Contributor

@martinbonnin martinbonnin left a comment

Choose a reason for hiding this comment

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

Nice catch, Thanks a lot!

@tylerbwong
Copy link
Contributor Author

tylerbwong commented Oct 10, 2020

@martinbonnin We'll have to hold off on merging this. The current patch doesn't walk every child fragment on operation.fragments. Should be working as intended in b7dcb05.

@martinbonnin
Copy link
Contributor

Thanks for digging into this!

Looking at this code made me realise two things:

  1. looks like we're going to have the same issue with inline fragments, which are not even in the Operation IR at the moment
  2. We should add validation on the root fragments so that querying a fragment on another type than the root (schema.queryType) fails:
query PlanetRootQueryBug {
  #This should fail with 
  # Fragment `planetFragment` can't be spread here as result can never be of type Root
  ...planetFragment
}

fragment planetFragment on Planet {
  name
  diameter
  rotationPeriod
  orbitalPeriod
}

Let me know if you want to look into this as part of this PR or I can look into it in a follow up PR.

@tylerbwong
Copy link
Contributor Author

I'll look into both issues as part of this PR!

@@ -188,6 +195,21 @@ class IRBuilder(private val schema: IntrospectionSchema,
return usedTypes
}

private fun List<FragmentRef>.referencedRootFragmentNames(operationType: String, fragments: List<Fragment>, filePath: String): Set<String> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Mostly nitpicking at this point but it feels a bit "off" that we need a special case for the root fragmentsRefs compared to field.fragmentRefs. What about moving the schema.rootTypeForOperationType(operationType) to the call site and re-using List<FragmentRef>.findFragments ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah nice catch. A lot of code here ended up being mostly duplicated from List<FragmentRef>.findFragments and I didn't even realize. Will refactor! I think I'll keep this function here though since we still need to do some extra lookups for all the nested fragments inside the root fragment.


fragment
}
}

private fun Fragment.validateTypeCondition(fragmentRef: FragmentRef, typeCondition: String, filePath: String) {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@tylerbwong
Copy link
Contributor Author

tylerbwong commented Oct 12, 2020

So after looking into supporting root inline fragments, I was able to add inlineFragments to the Operation IR and even get the Kotlin codegen to work properly for that use-case, but I wasn't able to figure out the Java codegen. If it's alright I think maybe we can get these changes merged in for root fragment support and I can open a separate issue/PR for supporting root inline fragments as a follow-up?

@martinbonnin
Copy link
Contributor

If it's alright I think maybe we can get these changes merged in for root fragment support and I can open a separate issue/PR for supporting root inline fragments as a follow-up?

That sounds good 👍 , thanks again !

Re: inline fragment, I didn't realise this would require big changes to the codegen. That's an area that's changing considerably in version 3.0.0 so we might want to implement that in the dev-3.x branch directly. I'll leave it up to you whether to:

  1. fix in main first and merge to dev-3.x later (but codegen will be most likely quite different, meaning a bunch of conflicts, I can help with merging if needed)
  2. or fix directly in dev-3.x and backport in main if required (maybe it won't be required).

In all cases, feel free to ignore the java codegen for the time being as it's currently removed from 3.0.0 (see #2616)

@martinbonnin martinbonnin merged commit 4cb0c47 into apollographql:main Oct 13, 2020
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.

Missing Fragment Definitions in QUERY_DOCUMENT for Root Query Fragment
3 participants