apollographql / apollo-ios

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

How @import(module:) works? #3346

Closed arnauddorgans closed 7 months ago

arnauddorgans commented 7 months ago

Question

I saw in the latest release we can import modules from the definition line, but I need help understanding the purpose of being able to.

query SearchUsers($after: String, $username: String!) @import(module: "ProfileAPI") {
    searchSellersByUsername(first: 20, after: $after, username: $username) {
        edges {
            node {
                ...GQLUser
            }
        }
        totalCount
        pageInfo {
            endCursor
            hasNextPage
        }
    }
}

I have a fragment in ProfileAPI module named GQLUser but Apollo refuses to generate the file because Unknown fragment "GQLUser" Is it supposed to work?

BobaFetters commented 7 months ago

@arnauddorgans Based on what you posted I am assuming the GQLUser fragment (and ProfileAPI) are part of a separate schema from your SearchUsers query? The @import directive is used for handling Swift module imports based on how you have your code generation configured to output your files, not including items from a separate GraphQL schema into another.

The general use case is say you have your schema schema types (such as your fragments) generated into target/package A, while you may have various sets of operations that you have being generated into different modules B, C, and D, respectively. If you are using a fragment from A in an operation in package B, the generated code wouldn't compile without importing package A into the generated operation file in package B. Using this @import directive allows you to do this at the schema level and have it handled by code generation instead of having to do it manually.

github-actions[bot] commented 7 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.