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

Improve the completions of Mock types when using ApolloTestSupport #3451

Open
CraigSiemens opened this issue Sep 30, 2024 · 2 comments
Open
Labels
codegen Issues related to or arising from code generation feature New addition or enhancement to existing solutions low-priority

Comments

@CraigSiemens
Copy link
Contributor

Use case

We're working on updating our tests to use the generated ApolloTestSupport.Mock initializers. We've noticed that it's fairly difficult to create the Mocks and response types.

  1. When typing .from(...), autocomplete shows every Mock init that's accessible.
  2. If a .from(...) is passed an incorrect Mock type, it fails at runtime.
    • The runtime error message doesn't help determine the correct type.
    ApolloTestSupport/TestMock.swift:131: Fatal error: 'try!' expression unexpectedly raised an error: Apollo.GraphQLExecutionError(path: refreshToken, underlying: ApolloAPI.JSONDecodingError.wrongType)
    
  3. Since the type name is not always the same as the field name, the only reliable way we've found to determine the type of Mock to create it by looking at the selection set's __parentType property in the generated code.

Describe the solution you'd like

Ideally there'd be more type information in the MockObject and selection sets. The the compiler could tell whether .from(...) is being passed a mock that is valid for the selection set and resolve the difficulties from above.

  1. Autocomplete would only show the mock inits that are valid for the type .from(...) is being called on.
  2. It would replace a runtime error with a compile time error, which is always a better developer experience.
  3. This would be less necessary if the autocomplete didn't show invalid inits.
@CraigSiemens CraigSiemens added the feature New addition or enhancement to existing solutions label Sep 30, 2024
@CraigSiemens
Copy link
Contributor Author

Currently the schema objects are not generated as types, but as a static let. Unfortunately that doesn't add any type information that can be used to help the compiler.

extension GraphQL.Objects {
  static let Mutation = ApolloAPI.Object(
    typename: "Mutation",
    implementedInterfaces: []
  )
}

Possible Solution

No matter what, it'd require changes to the generated code to add more type information. However, I don't think it'd need large structural changes, just adding some additional types and type aliases.

Schema Objects

Instead, if it generated a type for the object that contained the schema object instance, that would keep the object instance while adding a type as well.

extension Objects {
  enum Mutation {
    static let object = ApolloAPI.Object(
      typename: "Mutation",
      implementedInterfaces: []
    )
  }
}

Operation SelectionSet

SelectionSet could contain an associated type for ParentType that could be used in the generated types like the following.

typealias ParentType = GraphQL.Objects.Mutation
static var __parentType: any ApolloAPI.ParentType { ParentType.object }

MockObject

MockObject could contain an associated type for ObjectType that could be used in the generated types like the following.

typealias ObjectType = GraphQL.Objects.Mutation
static var objectType: ApolloAPI.Object { ObjectType.object }

.from(...)

It could have it's clause changed to include the following.

where O : ApolloTestSupport.MockObject, O.ObjectType == Self.ParentType

Other

__parentType and objectType could possibly be removed from the generated code and added as a default implementation in a protocol extension.

@BobaFetters
Copy link
Member

This is something we can look into to see how we might be able to support something like this, right now this wouldn't be at the top of our priority list, I will add this to our backlog for us to take a look at.

@BobaFetters BobaFetters added codegen Issues related to or arising from code generation low-priority labels Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codegen Issues related to or arising from code generation feature New addition or enhancement to existing solutions low-priority
Projects
None yet
Development

No branches or pull requests

2 participants