Apollo class generation

Capture d’écran 2021-08-12 à 11.48.59

Hello !

I have a question on how I could improve the classes generated by apollo from our schema (cf image).
_13_5 and _6_5 objects are exactly the same.

In my query I first had:

images {
                            _13_5 {
                                alt
                                title
                                files {
                                    width
                                    height
                                    url
                                }
                            }
                            _6_5 {
                                alt
                                title
                                files {
                                    width
                                    height
                                    url
                                }
                            }
                        }

I was happy with it BUT I have a problem with “files”. In our schema both files are of the same type [ImageFile] but in the generated class I have List<File> for _13_5 and List<File1> for _6_5 and it complicates the transformation I do later in the code because I have to duplicate it for File and File1 objects.

I tried to create a fragment:

fragment imageFragment on Image {
    alt
    title
    files {
        width
        height
        url
    }
}
[...]
images {
                            _13_5 {
                                ...imageFragment
                            }
                            _6_5 {
                                ...imageFragment
                            }
                        }

And it did actually merge both objects into one List<File> for _13_5 and _6_5 so I just have one object transformation to implement BUT imageFragment is a subtype of GraphqlFragment and I don’t have apollo depedencies in the module where I do my transformation and I would like not to add it…but I don’t have access to the fragment values

So I’m wondering if I could do something to not create a new fragment but to say to apollo that both files are of the same type ?

Yup, that’s the way to go to reuse field selections :+1:

imageFragment is a subtype of GraphqlFragment and I don’t have apollo depedencies in the module where I do my transformation and I would like not to add it

You can make the code compile by making apollo-runtime an “api” dependency instead of an “implementation”:

dependencies {
  api("com.apollographql.apollo:apollo-runtime:2.5.9")
}

This should have minimal impact on your build. Unless there’s something I’m missing?

Thank you for you quick answer !

Hmmm okay so no way to make apollo use the same type other than using fragment ?
The idea is that we have a Module1 where we have apollo queries and Module2 where we do some mapping on the data we get. And we would like to keep apollo dependencies in just Module1. But using fragment to mutualize types make us add apollo dependency in Module2 too to have access to GraphqlFragment. And we would like to avoid it but if there is no other way…

But using fragment to mutualize types make us add apollo dependency in Module2 too to have access to GraphqlFragment.

Just to clarify: You don’t have to add the dependency explicitely in module 2. You declare it as an “api” dependency in module1 and module2 will have access to it as a transitive dependency.

We could think of options to remove the GraphQlFragment supertype but to be honest, that’s a lot of work and future maintenance work and I’m not sure what the gain would be there.

# Schema
type Image {
  alt
  title
  files: [File] # or FileConnection
}

type File {
  width
  height
  url
}

type Query {
  images(width: Int, height: Int): [Image] # or ImageConnection
}
# Executable
query GetImages {
  thirteenByFive: images(width: 13, height: 5) {
    ...Image
  }
  sixByFive: images(width: 6, height: 5) {
    ...Image
  }
  all: images {
    ...Image
  }
}

fragment Image on Image {
  ...
}