Skip to content

Improve the completions of Mock types when using ApolloTestSupport #3451

Open
@CraigSiemens

Description

@CraigSiemens

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    codegenIssues related to or arising from code generationfeatureNew addition or enhancement to existing solutionslow-prioritytesting

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions