apollographql / apollographql/apollo-tooling

Support automatic Identifiable protocol generation in Swift Codegen

Open
#1,809 4 comments 1 reaction 0 assignees View on GitHub
:bird: component - swift
Dominant language
TypeScript
Stars
3k
Forks
460
PR merge metrics
No merged PRs in 30d

Description

It would be awesome if the swift codegen had an option to detect and automatically generate the correct protocols to conform to `Identifiable` when a `GraphQLSelectionSet` has an `id` property.

It is currently possible to write code to conform, but it is fragile:
```swift
// assumes that PeopleQuery.Data.AllPerson.Edge.Node has an id property as specified by .graphql
extension PeopleQuery.Data.AllPerson.Edge.Node: Identifiable {
}
```

## Proposed Solution

The proposed solution would be to add an optional flag and generate the extensions in the current codegen, but it may perhaps be easier to traverse the AST of the generated API as a separate step. Advice is welcome if folks have done this before.

## Reasoning
While it is currently possible to support this solution today- it is tedious to rewrite the same code.
```swift
extension PeopleQuery.Data {
var flattenedEdges: [Self.AllPerson.Edge.Node] {
allPeople?.edges?.compactMap { $0?.node } ?? []
}
}

extension PeopleQuery.Data.AllPerson.Edge.Node: Identifiable {
}

extension PlanetsQuery.Data {
var flattenedEdges: [Self.AllPlanet.Edge.Node] {
allPlanets?.edges?.compactMap { $0?.node } ?? []
}
}

extension PlanetsQuery.Data.AllPlanet.Edge.Node: Identifiable {
}
```

Contributor guide

Open the contributing guide

Research direction

Start by locating the Swift codegen entry point and the current generated API output path; the issue suggests either an optional flag or a separate traversal of the generated API AST. Clarify which GraphQLSelectionSet types qualify and what generated Identifiable conformance should look like, then verify the behavior on selections containing an id property.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, swift
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.