Mock artifacts generated for deleted interface type

Hello–I’m new to the forums so forgive me if this isn’t the best place or if it has already been answered.

Question:
I was recently cleaning up an unused interface type in my client code. However, after removing everything from my query and re-running codegen, I notice that there is a still a mock file generated for this type.

Is this a bug with the apollo iOS client, or does the type still exist for other purposes?

Example code

interface Character {
  id: ID!
}

type Human implements Character {
  id: ID!
  totalCredits: Int
}

type Droid implements Character {
  id: ID!
  primaryFunction: String
}

Before:

hero {
    ... on Droid {
        ...droidFields
    }
    ... on Human {
        ...humanFields
    } 
}

After:

hero {
    ... on Droid {
        ...droidFields
    }
}

Expected behavior

  • No human related artifacts left in my generated client code

Observed behavior
A mock type is generated like so


public class Human: MockObject {
  public static let objectType: ApolloAPI.Object = BTGraphQL.Objects.Human
  public static let _mockFields = MockFields()
  public typealias MockValueCollectionType = Array<Mock<Human>>

  public struct MockFields {
    @Field<BTGraphQL.ID>("id") public var id
  }
}

Hi @tt-pkaminski - is it possible this is an old file from a previous run of codegen? Codegen won’t automatically remove old files unless you configure it to do so, see pruneGeneratedFiles.

Thanks for the help. I did a grep for that config in our repo, and I do see that it is set to true

let options: ApolloCodegenConfiguration.OutputOptions = .init(
                additionalInflectionRules: [],
                deprecatedEnumCases: .exclude,
                schemaDocumentation: .include,
                operationDocumentFormat: [.definition, .operationId],
                warningsOnDeprecatedUsage: .exclude,
                pruneGeneratedFiles: true,
                markOperationDefinitionsAsFinal: true
)

The interesting thing that made me ask this question was, after running codegen, the mock was updated to remove all other properties except for the id property.

Our interface defines id as the only required property.

OK, if the mock file was updated then we can assume it’s being regenerated. Are you certain it’s not being used anywhere else? The schema generated objects should have a list of implementedInterfaces - does it appear in any of those?

Ah yep, I do see it show up

public extension BTGraphQL.Objects {
  static let MyCustomSection = ApolloAPI.Object(
    typename: "MyCustomSection",
    implementedInterfaces: [BTGraphQL.Interfaces.Section.self]
  )
}

Is this generated on the client because the server still defines this type in the schema, even though it is deprecated?

It doesn’t matter that it’s in the schema but rather that it’s still a possible return type. So as long as a response object can return a type like that it’ll need to be generated. I don’t believe deprecated would make a difference in this case because deprecated simply means “do not use” but not “guaranteed to never use”.

I see, so just to solidify my own understanding:

  • this type will be generated on the client as it is still possible for the server to return this particular section type.
  • In order for it to be fully removed from our generated code, we’d need to delete MyCustomSection as a type from our schema

Thanks again for following up on this.

  • this type will be generated on the client as it is still possible for the server to return this particular section type.

That’s correct.

  • In order for it to be fully removed from our generated code, we’d need to delete MyCustomSection as a type from our schema

Doesn’t need to be deleted but your return types would need to stop implementing that interface, then it would no longer be generated.