Skip to content

Commit

Permalink
FragmentDefinition rendering & unit test fixes (#2127)
Browse files Browse the repository at this point in the history
* FragmentDefinition rendering & unit test fixes

* Render Selection Set - Minor fixes (#2128)

* Fix entity field accessor to point to correct generated selection set

* Add test for merged fragment accessors

* Render Merged Field and TypeCase selection sets

* Change Glob to use OrderedSet to remove randomness is referencedType ordering

* Fixed doubled operation type name on end of generated operation with type already appended.

* Fix glob tests
  • Loading branch information
AnthonyMDev authored Feb 2, 2022
1 parent ff490d4 commit 389774d
Show file tree
Hide file tree
Showing 22 changed files with 758 additions and 267 deletions.
4 changes: 0 additions & 4 deletions Apollo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@
DE223C3327221144004A0148 /* IRMatchers.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE223C2A2721FAD6004A0148 /* IRMatchers.swift */; };
DE2739112769AEBA00B886EF /* SelectionSetTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE2739102769AEBA00B886EF /* SelectionSetTemplate.swift */; };
DE296539279B3B8200BF9B49 /* SelectionSetTemplateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE296536279B3B8200BF9B49 /* SelectionSetTemplateTests.swift */; };
DE29653A279B3B8200BF9B49 /* SelectionSetTemplate_RenderFragment_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE296537279B3B8200BF9B49 /* SelectionSetTemplate_RenderFragment_Tests.swift */; };
DE29653B279B3B8200BF9B49 /* SelectionSetTemplate_RenderOperation_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE296538279B3B8200BF9B49 /* SelectionSetTemplate_RenderOperation_Tests.swift */; };
DE296BA527A07C37004F571F /* MockMergedSelections.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE296BA427A07C37004F571F /* MockMergedSelections.swift */; };
DE296BA727A09A11004F571F /* IsEverTrue.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE296BA627A09A11004F571F /* IsEverTrue.swift */; };
Expand Down Expand Up @@ -894,7 +893,6 @@
DE223C2C2721FCE8004A0148 /* ScopedSelectionSetHashable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScopedSelectionSetHashable.swift; sourceTree = "<group>"; };
DE2739102769AEBA00B886EF /* SelectionSetTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectionSetTemplate.swift; sourceTree = "<group>"; };
DE296536279B3B8200BF9B49 /* SelectionSetTemplateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectionSetTemplateTests.swift; sourceTree = "<group>"; };
DE296537279B3B8200BF9B49 /* SelectionSetTemplate_RenderFragment_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectionSetTemplate_RenderFragment_Tests.swift; sourceTree = "<group>"; };
DE296538279B3B8200BF9B49 /* SelectionSetTemplate_RenderOperation_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectionSetTemplate_RenderOperation_Tests.swift; sourceTree = "<group>"; };
DE296BA427A07C37004F571F /* MockMergedSelections.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockMergedSelections.swift; sourceTree = "<group>"; };
DE296BA627A09A11004F571F /* IsEverTrue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IsEverTrue.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1949,7 +1947,6 @@
isa = PBXGroup;
children = (
DE296536279B3B8200BF9B49 /* SelectionSetTemplateTests.swift */,
DE296537279B3B8200BF9B49 /* SelectionSetTemplate_RenderFragment_Tests.swift */,
DE296538279B3B8200BF9B49 /* SelectionSetTemplate_RenderOperation_Tests.swift */,
);
path = SelectionSet;
Expand Down Expand Up @@ -3186,7 +3183,6 @@
DE79642F2769A1EB00978A03 /* IROperationBuilderTests.swift in Sources */,
E68D824527A1D8A60040A46F /* TypeTemplateTests.swift in Sources */,
DE29653B279B3B8200BF9B49 /* SelectionSetTemplate_RenderOperation_Tests.swift in Sources */,
DE29653A279B3B8200BF9B49 /* SelectionSetTemplate_RenderFragment_Tests.swift in Sources */,
E657CDBA26FD01D4005834D6 /* ApolloSchemaInternalTests.swift in Sources */,
E6D79AB826E9D59C0094434A /* URLDownloaderTests.swift in Sources */,
E6C9849527929FED009481BE /* EnumTemplateTests.swift in Sources */,
Expand Down
71 changes: 68 additions & 3 deletions Sources/AnimalKingdomAPI/Generated/AllAnimalsQuery.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ApolloAPI

public class AllAnimalsQueryQuery: GraphQLQuery {
public class AllAnimalsQuery: GraphQLQuery {
public let operationName: String = "AllAnimalsQuery"
public let document: DocumentType = .notPersisted(
definition: .init(
Expand Down Expand Up @@ -54,7 +54,7 @@ public class AllAnimalsQueryQuery: GraphQLQuery {

public static var __parentType: ParentType { .Object(AnimalKingdomAPI.Query.self) }
public static var selections: [Selection] { [
.field("allAnimals", [Animal].self),
.field("allAnimals", [AllAnimal].self),
] }

public var allAnimals: [AllAnimal] { data["allAnimals"] }
Expand All @@ -68,7 +68,7 @@ public class AllAnimalsQueryQuery: GraphQLQuery {
.field("height", Height.self),
.field("species", String.self),
.field("skinCovering", GraphQLEnum<SkinCovering>?.self),
.field("predators", [Animal].self),
.field("predators", [Predator].self),
.typeCase(AsWarmBlooded.self),
.typeCase(AsPet.self),
.typeCase(AsCat.self),
Expand Down Expand Up @@ -168,6 +168,18 @@ public class AllAnimalsQueryQuery: GraphQLQuery {
public var warmBloodedDetails: WarmBloodedDetails { _toFragment() }
public var heightInMeters: HeightInMeters { _toFragment() }
}

public struct Height: AnimalKingdomAPI.SelectionSet {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public static var __parentType: ParentType { .Object(AnimalKingdomAPI.Height.self) }

public var feet: Int { data["feet"] }
public var inches: Int { data["inches"] }
public var meters: Int { data["meters"] }
public var yards: Int { data["yards"] }
}
}

public struct AsPet: AnimalKingdomAPI.TypeCase {
Expand Down Expand Up @@ -242,6 +254,20 @@ public class AllAnimalsQueryQuery: GraphQLQuery {
public var heightInMeters: HeightInMeters { _toFragment() }
public var petDetails: PetDetails { _toFragment() }
}

public struct Height: AnimalKingdomAPI.SelectionSet {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public static var __parentType: ParentType { .Object(AnimalKingdomAPI.Height.self) }

public var feet: Int { data["feet"] }
public var inches: Int { data["inches"] }
public var meters: Int { data["meters"] }
public var yards: Int { data["yards"] }
public var relativeSize: GraphQLEnum<RelativeSize> { data["relativeSize"] }
public var centimeters: Int { data["centimeters"] }
}
}
}

Expand Down Expand Up @@ -272,6 +298,20 @@ public class AllAnimalsQueryQuery: GraphQLQuery {
public var warmBloodedDetails: WarmBloodedDetails { _toFragment() }
public var petDetails: PetDetails { _toFragment() }
}

public struct Height: AnimalKingdomAPI.SelectionSet {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public static var __parentType: ParentType { .Object(AnimalKingdomAPI.Height.self) }

public var feet: Int { data["feet"] }
public var inches: Int { data["inches"] }
public var meters: Int { data["meters"] }
public var yards: Int { data["yards"] }
public var relativeSize: GraphQLEnum<RelativeSize> { data["relativeSize"] }
public var centimeters: Int { data["centimeters"] }
}
}

public struct AsClassroomPet: AnimalKingdomAPI.TypeCase {
Expand All @@ -297,6 +337,17 @@ public class AllAnimalsQueryQuery: GraphQLQuery {
public var heightInMeters: HeightInMeters { _toFragment() }
}

public struct Height: AnimalKingdomAPI.SelectionSet {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public static var __parentType: ParentType { .Object(AnimalKingdomAPI.Height.self) }

public var feet: Int { data["feet"] }
public var inches: Int { data["inches"] }
public var meters: Int { data["meters"] }
}

public struct AsBird: AnimalKingdomAPI.TypeCase {
public let data: DataDict
public init(data: DataDict) { self.data = data }
Expand Down Expand Up @@ -324,6 +375,20 @@ public class AllAnimalsQueryQuery: GraphQLQuery {
public var warmBloodedDetails: WarmBloodedDetails { _toFragment() }
public var petDetails: PetDetails { _toFragment() }
}

public struct Height: AnimalKingdomAPI.SelectionSet {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public static var __parentType: ParentType { .Object(AnimalKingdomAPI.Height.self) }

public var feet: Int { data["feet"] }
public var inches: Int { data["inches"] }
public var meters: Int { data["meters"] }
public var yards: Int { data["yards"] }
public var relativeSize: GraphQLEnum<RelativeSize> { data["relativeSize"] }
public var centimeters: Int { data["centimeters"] }
}
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions Sources/AnimalKingdomAPI/Generated/ClassroomPetDetails.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import ApolloAPI

public struct ClassroomPetDetails: AnimalKingdomAPI.SelectionSet, Fragment {
public static var fragmentDefinition: StaticString { """
fragment ClassroomPetDetails on ClassroomPet {
... on Animal {
species
}
... on Pet {
humanName
}
... on WarmBlooded {
laysEggs
}
... on Cat {
bodyTemperature
isJellicle
}
... on Bird {
wingspan
}
... on PetRock {
favoriteToy
}
}
""" }

public let data: DataDict
public init(data: DataDict) { self.data = data }

Expand Down
105 changes: 105 additions & 0 deletions Sources/AnimalKingdomAPI/Generated/ClassroomPets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,111 @@ public class ClassroomPetsQuery: GraphQLQuery {

public var classroomPetDetails: ClassroomPetDetails { _toFragment() }
}

public struct AsAnimal: AnimalKingdomAPI.TypeCase {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public static var __parentType: ParentType { .Interface(AnimalKingdomAPI.Animal.self) }

public var species: String { data["species"] }

public struct Fragments: FragmentContainer {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public var classroomPetDetails: ClassroomPetDetails { _toFragment() }
}
}

public struct AsPet: AnimalKingdomAPI.TypeCase {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public static var __parentType: ParentType { .Interface(AnimalKingdomAPI.Pet.self) }

public var humanName: String? { data["humanName"] }

public struct Fragments: FragmentContainer {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public var classroomPetDetails: ClassroomPetDetails { _toFragment() }
}
}

public struct AsWarmBlooded: AnimalKingdomAPI.TypeCase {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public static var __parentType: ParentType { .Interface(AnimalKingdomAPI.WarmBlooded.self) }

public var species: String { data["species"] }
public var laysEggs: Bool { data["laysEggs"] }

public struct Fragments: FragmentContainer {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public var classroomPetDetails: ClassroomPetDetails { _toFragment() }
}
}

public struct AsCat: AnimalKingdomAPI.TypeCase {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public static var __parentType: ParentType { .Object(AnimalKingdomAPI.Cat.self) }

public var species: String { data["species"] }
public var humanName: String? { data["humanName"] }
public var laysEggs: Bool { data["laysEggs"] }
public var bodyTemperature: Int { data["bodyTemperature"] }
public var isJellicle: Bool { data["isJellicle"] }

public struct Fragments: FragmentContainer {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public var classroomPetDetails: ClassroomPetDetails { _toFragment() }
}
}

public struct AsBird: AnimalKingdomAPI.TypeCase {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public static var __parentType: ParentType { .Object(AnimalKingdomAPI.Bird.self) }

public var species: String { data["species"] }
public var humanName: String? { data["humanName"] }
public var laysEggs: Bool { data["laysEggs"] }
public var wingspan: Int { data["wingspan"] }

public struct Fragments: FragmentContainer {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public var classroomPetDetails: ClassroomPetDetails { _toFragment() }
}
}

public struct AsPetRock: AnimalKingdomAPI.TypeCase {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public static var __parentType: ParentType { .Object(AnimalKingdomAPI.PetRock.self) }

public var humanName: String? { data["humanName"] }
public var favoriteToy: String { data["favoriteToy"] }

public struct Fragments: FragmentContainer {
public let data: DataDict
public init(data: DataDict) { self.data = data }

public var classroomPetDetails: ClassroomPetDetails { _toFragment() }
}
}
}
}
}
8 changes: 8 additions & 0 deletions Sources/AnimalKingdomAPI/Generated/HeightInMeters.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import ApolloAPI

public struct HeightInMeters: AnimalKingdomAPI.SelectionSet, Fragment {
public static var fragmentDefinition: StaticString { """
fragment HeightInMeters on Animal {
height {
meters
}
}
""" }

public let data: DataDict
public init(data: DataDict) { self.data = data }

Expand Down
12 changes: 11 additions & 1 deletion Sources/AnimalKingdomAPI/Generated/PetDetails.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import ApolloAPI

public struct PetDetails: AnimalKingdomAPI.SelectionSet, Fragment {
public static var fragmentDefinition: StaticString { """
fragment PetDetails on Pet {
humanName
favoriteToy
owner {
firstName
}
}
""" }

public let data: DataDict
public init(data: DataDict) { self.data = data }

public static var __parentType: ParentType { .Interface(AnimalKingdomAPI.Pet.self) }
public static var selections: [Selection] { [
.field("humanName", String?.self),
.field("favoriteToy", String.self),
.field("owner", Human?.self),
.field("owner", Owner?.self),
] }

public var humanName: String? { data["humanName"] }
Expand Down
6 changes: 3 additions & 3 deletions Sources/AnimalKingdomAPI/Generated/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public enum Schema: SchemaConfiguration {
public static func objectType(forTypename __typename: String) -> Object.Type? {
switch __typename {
case "Query": return AnimalKingdomAPI.Query.self
case "Height": return AnimalKingdomAPI.Height.self
case "Human": return AnimalKingdomAPI.Human.self
case "Cat": return AnimalKingdomAPI.Cat.self
case "Bird": return AnimalKingdomAPI.Bird.self
case "Rat": return AnimalKingdomAPI.Rat.self
case "PetRock": return AnimalKingdomAPI.PetRock.self
case "Height": return AnimalKingdomAPI.Height.self
case "Human": return AnimalKingdomAPI.Human.self
default: return nil
}
}
}
}
10 changes: 10 additions & 0 deletions Sources/AnimalKingdomAPI/Generated/WarmBloodedDetails.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import ApolloAPI

public struct WarmBloodedDetails: AnimalKingdomAPI.SelectionSet, Fragment {
public static var fragmentDefinition: StaticString { """
fragment WarmBloodedDetails on WarmBlooded {
bodyTemperature
height {
meters
yards
}
}
""" }

public let data: DataDict
public init(data: DataDict) { self.data = data }

Expand Down
2 changes: 1 addition & 1 deletion Sources/ApolloAPI/FragmentProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// A `SelectionSet` can be converted to any `Fragment` included in it's `Fragments` object via
/// its `fragments` property.
public protocol Fragment: AnySelectionSet {
static var fragmentDefinition: String { get }
static var fragmentDefinition: StaticString { get }
}

public protocol FragmentContainer {
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApolloAPI/GraphQLOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public struct OperationDefinition {
public var queryDocument: String {
var document = operationDefinition
fragments?.forEach {
document.append("\n" + $0.fragmentDefinition)
document.append("\n" + $0.fragmentDefinition.description)
}
return document
}
Expand Down
Loading

0 comments on commit 389774d

Please sign in to comment.