Skip to content

Commit

Permalink
Add Union and root types to referenced types in JS FrontEnd (#2126)
Browse files Browse the repository at this point in the history
* Render Operation Definition

* Generation of Operation Definition 7 Clean up Swift Scripts

* Added import ApolloAPI to templates

* Implement Fragment Template + File Generator

And change ResponseDict to DataDict in template renderer

* Calculate generatedSelectionSetName with merging from parent and type cases

* WIP: TypeCase Accessors

* fixed errors after rebasing

* Move caching of generated seleciton set names to object retained by SelectionSetTemplate

* Calculating TypeCase Merged names

* Fixing TemplateString new line removal for sections

* Render Fragment Container

* Render Direct TypeCase SelectionSets

* Regenerate AnimalKingdom API

* Render Boolean as "Bool"

* Missing newline after field selections sets

* Add Union and root types to referenced types in JS FrontEnd

* Minor fix
  • Loading branch information
AnthonyMDev authored Feb 1, 2022
1 parent dabe11e commit ff490d4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 11 deletions.
8 changes: 8 additions & 0 deletions Apollo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@
DE09F9C6270269F800795949 /* OperationDefinitionTemplate_DocumentType_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE09F9C5270269F800795949 /* OperationDefinitionTemplate_DocumentType_Tests.swift */; };
DE12B2D7273B204B003371CC /* TestError.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE12B2D6273B204B003371CC /* TestError.swift */; };
DE12B2D9273C4338003371CC /* MockIRSubscripts.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE12B2D8273C4338003371CC /* MockIRSubscripts.swift */; };
DE16ADF827A8C0C3007F5C59 /* Rat.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE16ADF727A8C0C3007F5C59 /* Rat.swift */; };
DE16ADFA27A8C0CC007F5C59 /* Query.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE16ADF927A8C0CC007F5C59 /* Query.swift */; };
DE181A2C26C5C0CB000C0B9C /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE181A2B26C5C0CB000C0B9C /* WebSocket.swift */; };
DE181A2E26C5C299000C0B9C /* SSLClientCertificate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE181A2D26C5C299000C0B9C /* SSLClientCertificate.swift */; };
DE181A3026C5C38E000C0B9C /* SSLSecurity.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE181A2F26C5C38E000C0B9C /* SSLSecurity.swift */; };
Expand Down Expand Up @@ -877,6 +879,8 @@
DE09F9C727026E0B00795949 /* Schema.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Schema.swift; sourceTree = "<group>"; };
DE12B2D6273B204B003371CC /* TestError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestError.swift; sourceTree = "<group>"; };
DE12B2D8273C4338003371CC /* MockIRSubscripts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockIRSubscripts.swift; sourceTree = "<group>"; };
DE16ADF727A8C0C3007F5C59 /* Rat.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Rat.swift; sourceTree = "<group>"; };
DE16ADF927A8C0CC007F5C59 /* Query.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Query.swift; sourceTree = "<group>"; };
DE181A2B26C5C0CB000C0B9C /* WebSocket.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebSocket.swift; sourceTree = "<group>"; };
DE181A2D26C5C299000C0B9C /* SSLClientCertificate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SSLClientCertificate.swift; sourceTree = "<group>"; };
DE181A2F26C5C38E000C0B9C /* SSLSecurity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SSLSecurity.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2102,7 +2106,9 @@
DE5B317227A48C8D0051C9D3 /* Animal.swift */,
DE5B317327A48C8D0051C9D3 /* AllAnimalsQuery.swift */,
DE5B317427A48C8D0051C9D3 /* Pet.swift */,
DE16ADF727A8C0C3007F5C59 /* Rat.swift */,
DE5B317527A48C8D0051C9D3 /* Schema.swift */,
DE16ADF927A8C0CC007F5C59 /* Query.swift */,
DE5B317627A48C8D0051C9D3 /* ClassroomPet.swift */,
DE5B317727A48C8D0051C9D3 /* Height.swift */,
DE5B317827A48C8D0051C9D3 /* WarmBlooded.swift */,
Expand Down Expand Up @@ -3401,6 +3407,7 @@
buildActionMask = 2147483647;
files = (
DE5B318927A48C8D0051C9D3 /* Height.swift in Sources */,
DE16ADFA27A8C0CC007F5C59 /* Query.swift in Sources */,
DE5B318527A48C8D0051C9D3 /* AllAnimalsQuery.swift in Sources */,
DE5B318B27A48C8D0051C9D3 /* SkinCovering.swift in Sources */,
DE5B318827A48C8D0051C9D3 /* ClassroomPet.swift in Sources */,
Expand All @@ -3419,6 +3426,7 @@
DE5B318227A48C8D0051C9D3 /* WarmBloodedDetails.swift in Sources */,
DE5B318027A48C8D0051C9D3 /* ClassroomPetDetails.swift in Sources */,
DE5B318C27A48C8D0051C9D3 /* RelativeSize.swift in Sources */,
DE16ADF827A8C0C3007F5C59 /* Rat.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
9 changes: 9 additions & 0 deletions Sources/AnimalKingdomAPI/Generated/Query.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ApolloAPI

public final class Query: Object {
override public class var __typename: String { "Query" }

override public class var __metadata: Metadata { _metadata }
private static let _metadata: Metadata = Metadata(implements: [
])
}
11 changes: 11 additions & 0 deletions Sources/AnimalKingdomAPI/Generated/Rat.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ApolloAPI

public final class Rat: Object {
override public class var __typename: String { "Rat" }

override public class var __metadata: Metadata { _metadata }
private static let _metadata: Metadata = Metadata(implements: [
Animal.self,
Pet.self
])
}
4 changes: 3 additions & 1 deletion Sources/AnimalKingdomAPI/Generated/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ where Schema == AnimalKingdomAPI.Schema {}
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
default: return nil
}
}
}
}
28 changes: 19 additions & 9 deletions Sources/ApolloCodegenLib/Frontend/JavaScript/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {
DocumentNode,
FragmentDefinitionNode,
getNamedType,
getOperationRootType,
GraphQLCompositeType,
GraphQLError,
GraphQLNamedType,
GraphQLObjectType,
GraphQLSchema,
GraphQLType,
isCompositeType,
isUnionType,
Kind,
OperationDefinitionNode,
print,
Expand Down Expand Up @@ -73,6 +73,17 @@ export function compileToIR(
referencedTypes: Array.from(referencedTypes.values()),
};

function addReferencedType(type: GraphQLNamedType) {
referencedTypes.add(type)

if (isUnionType(type)) {
const unionReferencedTypes = type.getTypes()
for (type of unionReferencedTypes) {
referencedTypes.add(getNamedType(type))
}
}
}

function getFragment(name: string): ir.FragmentDefinition | undefined {
let fragment = fragmentMap.get(name);
if (fragment) return fragment;
Expand Down Expand Up @@ -117,7 +128,7 @@ export function compileToIR(
);
}

referencedTypes.add(getNamedType(type));
addReferencedType(getNamedType(type));

return {
name,
Expand All @@ -127,10 +138,9 @@ export function compileToIR(
);

const source = print(operationDefinition);
const rootType = getOperationRootType(
schema,
operationDefinition
) as GraphQLObjectType;
const rootType = schema.getRootType(operationType) as GraphQLObjectType;

referencedTypes.add(getNamedType(rootType));

return {
filePath,
Expand Down Expand Up @@ -159,7 +169,7 @@ export function compileToIR(
fragmentDefinition.typeCondition
) as GraphQLCompositeType;

referencedTypes.add(typeCondition);
addReferencedType(getNamedType(typeCondition));

return {
name,
Expand Down Expand Up @@ -209,7 +219,7 @@ export function compileToIR(
const fieldType = fieldDef.type;
const unwrappedFieldType = getNamedType(fieldType);

referencedTypes.add(unwrappedFieldType);
addReferencedType(getNamedType(unwrappedFieldType));

const { description, deprecationReason } = fieldDef;

Expand Down Expand Up @@ -265,7 +275,7 @@ export function compileToIR(
? (typeFromAST(schema, typeNode) as GraphQLCompositeType)
: parentType;

referencedTypes.add(typeCondition);
addReferencedType(typeCondition);

return {
kind: "InlineFragment",
Expand Down

Large diffs are not rendered by default.

0 comments on commit ff490d4

Please sign in to comment.