Skip to content

Commit 551cbe8

Browse files
authored
3599 mock custom scalar (#838)
1 parent 5867aa0 commit 551cbe8

File tree

9 files changed

+53
-14
lines changed

9 files changed

+53
-14
lines changed

Sources/AnimalKingdomAPI/animalkingdom-graphql/AnimalSchema.graphqls

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ input PetAdoptionInput {
2727
favoriteToy: String!
2828
isSpayedOrNeutered: Boolean! = false
2929
measurements: MeasurementsInput
30+
adoptionDate: CustomDate
3031
}
3132

3233
input MeasurementsInput {
@@ -54,6 +55,7 @@ interface Pet @typePolicy(keyFields: "id") {
5455
humanName: String
5556
favoriteToy: String!
5657
owner: Human
58+
adoptionDate: CustomDate
5759
}
5860

5961
interface HousePet implements Animal & Pet {
@@ -68,7 +70,8 @@ interface HousePet implements Animal & Pet {
6870
bestFriend: Pet
6971
rival: Pet
7072
livesWith: ClassroomPet
71-
birthdate: CustomDate
73+
adoptionDate: CustomDate
74+
birthdate: CustomDate!
7275
}
7376

7477
interface WarmBlooded implements Animal {
@@ -110,6 +113,7 @@ type Cat implements Animal & Pet & WarmBlooded {
110113
humanName: String
111114
favoriteToy: String!
112115
owner: Human
116+
adoptionDate: CustomDate
113117
bodyTemperature: Int!
114118
laysEggs: Boolean!
115119
isJellicle: Boolean!
@@ -124,12 +128,13 @@ type Dog implements Animal & Pet & HousePet & WarmBlooded {
124128
humanName: String
125129
favoriteToy: String!
126130
owner: Human
131+
adoptionDate: CustomDate
127132
bodyTemperature: Int!
128133
laysEggs: Boolean!
129134
bestFriend: HousePet
130135
rival: Cat
131136
livesWith: Bird
132-
birthdate: CustomDate
137+
birthdate: CustomDate!
133138
houseDetails: Object
134139
}
135140

@@ -142,6 +147,7 @@ type Bird implements Animal & Pet & WarmBlooded {
142147
humanName: String
143148
favoriteToy: String!
144149
owner: Human
150+
adoptionDate: CustomDate
145151
bodyTemperature: Int!
146152
laysEggs: Boolean!
147153
wingspan: Float!
@@ -156,6 +162,7 @@ type Fish implements Animal & Pet {
156162
humanName: String
157163
favoriteToy: String!
158164
owner: Human
165+
adoptionDate: CustomDate
159166
}
160167

161168
type Rat implements Animal & Pet {
@@ -167,6 +174,7 @@ type Rat implements Animal & Pet {
167174
humanName: String
168175
favoriteToy: String!
169176
owner: Human
177+
adoptionDate: CustomDate
170178
}
171179

172180
type Crocodile implements Animal {
@@ -184,6 +192,7 @@ type PetRock implements Pet {
184192
humanName: String
185193
favoriteToy: String!
186194
owner: Human
195+
adoptionDate: CustomDate
187196
}
188197

189198
union ClassroomPet = Cat | Bird | Rat | PetRock

Sources/AnimalKingdomAPI/animalkingdom-graphql/PetDetailsMutation.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ fragment PetDetailsMutation on Pet @apollo_client_ios_localCacheMutation {
22
owner {
33
firstName
44
}
5+
adoptionDate
56
}

Tests/ApolloCodegenTests/CodeGeneration/Templates/MockObjectTemplateTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ class MockObjectTemplateTests: XCTestCase {
482482
483483
public extension Mock where O == Dog {
484484
convenience init(
485-
customScalar: TestSchema.CustomScalar = try! .init(_jsonValue: ""),
485+
customScalar: TestSchema.CustomScalar = .defaultMockValue,
486486
customScalarList: [TestSchema.CustomScalar]? = nil,
487487
customScalarOptionalList: [TestSchema.CustomScalar?]? = nil,
488488
enumList: [GraphQLEnum<TestSchema.EnumType>]? = nil,
@@ -580,7 +580,7 @@ class MockObjectTemplateTests: XCTestCase {
580580
581581
public extension Mock where O == Dog {
582582
convenience init(
583-
customScalar: TestSchema.CustomScalar = try! .init(_jsonValue: ""),
583+
customScalar: TestSchema.CustomScalar = .defaultMockValue,
584584
customScalarList: [TestSchema.CustomScalar] = [],
585585
enumList: [GraphQLEnum<TestSchema.EnumType>] = [],
586586
enumType: GraphQLEnum<TestSchema.EnumType> = .case(.foo),
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/bin/bash
22

3-
xcodebuild archive -configuration Release -project "CodegenXCFramework.xcodeproj" -scheme "CodegenXCFramework" -destination 'generic/platform=iOS Simulator' -archivePath "./build/iphonesimulator.xcarchive" SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES | xcbeautify
4-
xcodebuild archive -configuration Release -project "CodegenXCFramework.xcodeproj" -scheme "CodegenXCFramework" -destination 'generic/platform=iOS' -archivePath "./build/iphoneos.xcarchive" SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES | xcbeautify
5-
xcodebuild -create-xcframework -output ./build/CodegenXCFramework.xcframework -framework ./build/iphonesimulator.xcarchive/Products/Library/Frameworks/CodegenXCFramework.framework -framework ./build/iphoneos.xcarchive/Products/Library/Frameworks/CodegenXCFramework.framework | xcbeautify
3+
set -o pipefail # Fail if any command in a pipeline fails
4+
5+
# Clean up previous build artifacts
6+
rm -rf ./build
7+
8+
xcodebuild archive -configuration Release -project "CodegenXCFramework.xcodeproj" -scheme "CodegenXCFramework" -destination 'generic/platform=macOS' -archivePath "./build/macOS.xcarchive" SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=NO | xcbeautify
9+
10+
xcodebuild -create-xcframework -allow-internal-distribution -output ./build/CodegenXCFramework.xcframework -framework ./build/macOS.xcarchive/Products/Library/Frameworks/CodegenXCFramework.framework | xcbeautify

Tests/TestCodeGenConfigurations/EmbeddedInTarget-InSchemaModule/Sources/TestApp/AnimalKingdomAPI/Schema/SchemaConfiguration.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import ApolloAPI
99

1010
enum SchemaConfiguration: ApolloAPI.SchemaConfiguration {
11-
static func cacheKeyInfo(for type: Object, object: ObjectData) -> CacheKeyInfo? {
12-
try? CacheKeyInfo(jsonValue: object["id"])
11+
static func cacheKeyInfo(for type: ApolloAPI.Object, object: ApolloAPI.ObjectData) -> CacheKeyInfo? {
12+
// Implement this function to configure cache key resolution for your schema types.
13+
return try? CacheKeyInfo(jsonValue: object["id"])
1314
}
1415
}

apollo-ios-codegen/Sources/ApolloCodegenLib/FileGenerators/DefaultMockValueProviding.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extension GraphQLScalarType: DefaultMockValueProviding {
3838
case "Boolean":
3939
return "false"
4040
default:
41-
return "try! .init(_jsonValue: \"\")"
41+
return ".defaultMockValue"
4242
}
4343
}
4444
}
34 KB
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@_spi(Internal) import ApolloAPI
2+
3+
public extension ScalarType {
4+
static var defaultMockValue: Self {
5+
try! .init(_jsonValue: "")
6+
}
7+
}
8+
9+
public extension CustomScalarType {
10+
static var defaultMockValue: Self {
11+
try! .init(_jsonValue: "")
12+
}
13+
}

scripts/run-test-codegen-configurations.sh

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,27 @@ for dir in `ls $CodeGenConfigsDirectory`;
2424
do
2525
echo "-- Generating code for project: $dir --"
2626
(cd apollo-ios-codegen && swift run apollo-ios-cli generate -p ../$CodeGenConfigsDirectory/$dir/apollo-codegen-config.json)
27+
28+
if [ $? -ne 0 ]; then
29+
echo "Error: Code generation failed for $dir"
30+
exit 1
31+
fi
2732

2833
if [ "$test_projects" = true ]
2934
then
3035
echo -e "-- Testing project: $dir --"
3136
cd $CodeGenConfigsDirectory/$dir
3237

33-
if /bin/bash ./test-project.sh; then
34-
echo -e "\n"
35-
cd - > /dev/null
36-
else
38+
/bin/bash ./test-project.sh
39+
test_exit_code=$?
40+
41+
cd - > /dev/null
42+
43+
if [ $test_exit_code -ne 0 ]; then
44+
echo "Error: Test failed for $dir"
3745
exit 1
3846
fi
47+
48+
echo -e "\n"
3949
fi
4050
done

0 commit comments

Comments
 (0)