Skip to content

How to create Query.Data without getting it through the AppoloClient? #51

@nuke-dash

Description

@nuke-dash

With Apollo Codegen an API.swift file is generate with contains the data structs.
But how can you create a struct without getting it through the AppoloClient as there is only an init which accepts a GraphQLResultReader?
I'm looking to create the data in a unit testing scenario without having to get it through the AppoloClient.
I tried creating a mock AppoloStore or a GraphQLResultReader but that doesn't seem possible as the init or other methods are inaccessible.

Take for example following API.swift file.
How would you create a Channel?

//  This file was automatically generated and should not be edited.

import Apollo

public final class ProofOfConceptQuery: GraphQLQuery {
  public static let operationDefinition =
    "query ProofOfConcept($profileId: ID!) {" +
    "  initialChannelList(profileId: $profileId) {" +
    "    id" +
    "    title" +
    "    channels {" +
    "      id" +
    "      title" +
    "    }" +
    "  }" +
    "}"

  public let profileId: GraphQLID

  public init(profileId: GraphQLID) {
    self.profileId = profileId
  }

  public var variables: GraphQLMap? {
    return ["profileId": profileId]
  }

  public struct Data: GraphQLMappable {
    public let initialChannelList: InitialChannelList?

    public init(reader: GraphQLResultReader) throws {
      initialChannelList = try reader.optionalValue(for: Field(responseName: "initialChannelList", arguments: ["profileId": reader.variables["profileId"]]))
    }

    public struct InitialChannelList: GraphQLMappable {
      public let __typename = "ChannelList"
      public let id: GraphQLID
      public let title: String
      public let channels: [Channel?]

      public init(reader: GraphQLResultReader) throws {
        id = try reader.value(for: Field(responseName: "id"))
        title = try reader.value(for: Field(responseName: "title"))
        channels = try reader.list(for: Field(responseName: "channels"))
      }

      public struct Channel: GraphQLMappable {
        public let __typename = "Channel"
        public let id: GraphQLID
        public let title: String

        public init(reader: GraphQLResultReader) throws {
          id = try reader.value(for: Field(responseName: "id"))
          title = try reader.value(for: Field(responseName: "title"))
        }
      }
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions