apollographql / apollo-ios

📱  A strongly-typed, caching GraphQL client for iOS, written in Swift.
https://www.apollographql.com/docs/ios/
MIT License
3.89k stars 728 forks source link

Documentation for @import directive. #3373

Closed scottasoutherland closed 6 months ago

scottasoutherland commented 6 months ago

Use case

I'm looking to understand how to use the @import directive added here: https://github.com/apollographql/apollo-ios-dev/pull/245

But it's unclear to me.

What i'm hoping to do is have a one graphQL library import another and re-use the fragments there, but I cannot find any documentation on this or how to use it. We currently are copying files and including multiple copies of the same fragments, and building 3 separate graphql libraries which is unideal. I assume the codegen would need to know about all 3 libraries which are are currently built separately ?

Describe the solution you'd like

No response

scottasoutherland commented 6 months ago

Making matters difficult is when i try to test this out, i run into this crash

GraphQLCompiler/JavaScriptBridge.swift:271: Precondition failed: Expected JavaScript object but found: undefined
calvincestari commented 6 months ago

Hi @scottasoutherland - I agree with should get some specific documentation up for this, I thought we had some already. We'll likely need a new place in the documentation where we can describe custom directives.

That directive is quite easy to use though:

Note that this is not intended to be a way to link schemas though, so any target/module you want to import into the operation file must have been part of the code generation execution that generated the operation in question.

calvincestari commented 6 months ago

Making matters difficult is when i try to test this out, i run into this crash

GraphQLCompiler/JavaScriptBridge.swift:271: Precondition failed: Expected JavaScript object but found: undefined

If you remove @import does the error stop and codegen succeeds?

scottasoutherland commented 6 months ago

Note that this is not intended to be a way to link schemas though, so any target/module you want to import into the operation file must have been part of the code generation execution that generated the operation in question.

So in our case we have 1 schema, and we have 3 different libraries we build that each have their own queries against that schema. We use some fragments in all 3 of them but currently we just copy the files so we end up with LibraryOne.myFragment, LibraryTwo.myFragment, LibraryThree.myFragment which are all effectively the same type but not actually interchangeable. What we'd like to do is import LibraryOne into LibraryTwo & LibraryThree and use LibraryOne.MyFragment everywhere.

If this is not what this is for, can you explain the use case for the directive ? In what case would you need to import a library that has nothing to do with the generated code ?

If you remove @import does the error stop and codegen succeeds?

After messing with it, the problem was not the imports, the problem was the missing fragments that i moved. It appears to be a volume of errors or something because if only one fragment is missing it flags that correctly but when i removed all of the re-used fragments it crashes instead.

calvincestari commented 6 months ago

So in our case we have 1 schema, and we have 3 different libraries we build that each have their own queries against that schema. We use some fragments in all 3 of them but currently we just copy the files so we end up with LibraryOne.myFragment, LibraryTwo.myFragment, LibraryThree.myFragment which are all effectively the same type but not actually interchangeable. What we'd like to do is import LibraryOne into LibraryTwo & LibraryThree and use LibraryOne.MyFragment everywhere.

Yes, this sounds like what it was intended for.

AnthonyMDev commented 6 months ago

There is documentation of that directive here. Perhaps this docs page is too difficult to discover right now?

calvincestari commented 6 months ago

Thanks @AnthonyMDev.

@scottasoutherland I'm going to close this issue now as there doesn't seem to be anything actionable. If you have further issues getting @import to work feel free to comment back here and we'll take a look - thanks.

github-actions[bot] commented 6 months ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo iOS usage and allow us to serve you better.

scottasoutherland commented 6 months ago

Thank you @calvincestari and @AnthonyMDev , i did search the docs for that directive but was unable to find it. Thanks for pointing it out and for the swift replies! Will try it out again today and see if I can make it work.