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

Documentation for @import directive. #3373

Closed
scottasoutherland opened this issue Apr 29, 2024 · 9 comments
Closed

Documentation for @import directive. #3373

scottasoutherland opened this issue Apr 29, 2024 · 9 comments
Labels
docs Focuses on documentation changes

Comments

@scottasoutherland
Copy link

Use case

I'm looking to understand how to use the @import directive added here: apollographql/apollo-ios-dev#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 scottasoutherland added the feature New addition or enhancement to existing solutions label Apr 29, 2024
@scottasoutherland
Copy link
Author

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 calvincestari added docs Focuses on documentation changes codegen Issues related to or arising from code generation and removed feature New addition or enhancement to existing solutions codegen Issues related to or arising from code generation labels Apr 30, 2024
@calvincestari
Copy link
Member

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:

  • Add the directive to an operation or fragment, e.g: query ExampleOperation @import(module: "ModuleName") { .. }
  • ModuleName needs to be the name of a defined target/module that is accessible to the code where the operation will be generated.

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
Copy link
Member

calvincestari commented Apr 30, 2024

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
Copy link
Author

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
Copy link
Member

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
Copy link
Contributor

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

@calvincestari
Copy link
Member

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.

Copy link

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Focuses on documentation changes
Projects
None yet
Development

No branches or pull requests

3 participants