Apollo-ios: How to use a non id field for identifiable?

Created on 29 Nov 2020  路  8Comments  路  Source: apollographql/apollo-ios

In my graphql server the query result objects have id's with the object type in them for example

agent_id instead of id for an agent object

When using it in a list in SwiftUI like

List(agentData, id: \.agent_id) { agent in
    NavigationLink(destination: AgentDetailView(agentData: agent)) {
        AgentListItemView(agent: agent)
    }
}

The agent_id for the identifier shows an error of

Value of type 'GetAgentsQuery.Data.Agent' has no member 'agent_id'

Where as in the API.swift file has

"""
    query GetAgents {
        agents {
            __typename
            agent_id
            user_id
            company
            description
            name
            email
            phone
            mobile
            comments
            meta
            active
            created_at
         }
    }
"""

Im not sure why it doesn't recognise it, so any ideas is appreciated

question

All 8 comments

"""
    query GetAgents {
      agents {
        __typename
        id
        agent_id
        user_id
        company
        description
        name
        email
        phone
        mobile
        comments
        meta
        active
        created_at
      }
    }
    """

I've even added the id field to the graphql server and apollo client in ios and still says it doesn't conform to identifiable, what am I missing?

Hi, so Identifiable is a Swift stdlib protocol that, right now, is not automatically conformed to by any Apollo code.

You will need to make a file separate from the generated API.swift (which gets constantly overwritten by automated codegen) where you will need to add Identifiable conformance to the nested Agent type. I think based on that query it should be (assuming agent_id is a String):

extension GetAgentsQuery.Data.Agent: Identifiable {
   associatedType ID: String

  var id: String {
    return agent_id
  }
}

A few things to read up on for more general info about extensions and Identifiable:

Thank you for your help. I created the swift file and named it the same as the graphql file (GetAgents.swift) and placed in:

import Apollo
import Foundation

extension GetAgentsQuery.Data.Agent: Identifiable {
    associatedType ID: String

    var id: String {
        return agent_id
    }
}

But i got nothing but errors I dont know what to do with, sorry

Showing Recent Issues
graphql/getAgents.swift:12:5: Expected 'func' keyword in instance method declaration
graphql/getAgents.swift:12:20: Found an unexpected second identifier in function declaration; is there an accidental break?
graphql/getAgents.swift:12:22: Expected '(' in argument list of function declaration
graphql/getAgents.swift:12:22: Expected '->' after function parameter tuple
graphql/getAgents.swift:12:5: Expected '{' in body of function declaration
graphql/getAgents.swift:14:9: Invalid redeclaration of 'id'
graphql/getAgents.swift:15:16: Cannot find 'agent_id' in scope

while some seem self ex[lantitory it just ends in more errors. Is the fact that the API.swift compiles earlier than the rest of the project? I've no idea...

Thanks for any of your help

It should not be related to the order of compilation, no.

One of the problems is mine: associatedtype is for the declaration of the protocol rather than the conformance, so that needs to be changed to typealias ID = String. I've got no idea where the rest of those errors are coming from, but it's certainly possible that changing to typealias would at least help.

The errors on line 12 don't make any sense though - it'd help to see what else is in that file. since it doesn't seem to be 15 lines long.

@Dreamystify Were you ever able to figure this out?

I was after I changed agent_id to agentId it worked

extension AgentsQuery.Data.Agent: Identifiable {
    public typealias ID = String

    public var id: String {
        return agentId
    }
}

woo! Mind if we close this out?

Absolutely, thanks for all your help

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ashiemke picture ashiemke  路  5Comments

uericw picture uericw  路  4Comments

StanislavCekunov picture StanislavCekunov  路  3Comments

jeromeDms picture jeromeDms  路  5Comments

hiteshborse12 picture hiteshborse12  路  4Comments