@@ -2904,4 +2904,172 @@ class ApolloCodegenTests: XCTestCase {
2904
2904
2905
2905
}
2906
2906
2907
+ // MARK: - Local Cache Mutation + Field Merging Integration Tests
2908
+ //
2909
+ // These are integration tests because the codegen test wrapper infrastructure does not support overriding config
2910
+ // values during the test.
2911
+
2912
+ func test__fileRendering__givenLocalCacheMutationQuery_whenSelectionSetInitializersEmpty_andFileMergingNone_shouldGenerateFullSelectionSetInitializers( ) async throws {
2913
+ // given
2914
+ try createFile (
2915
+ body: """
2916
+ type Query {
2917
+ allAnimals: [Animal!]
2918
+ }
2919
+
2920
+ interface Animal {
2921
+ species: String
2922
+ }
2923
+ """ ,
2924
+ filename: " schema.graphqls "
2925
+ )
2926
+
2927
+ try createFile (
2928
+ body: """
2929
+ query TestOperation @apollo_client_ios_localCacheMutation {
2930
+ allAnimals {
2931
+ species
2932
+ }
2933
+ }
2934
+ """ ,
2935
+ filename: " operation.graphql "
2936
+ )
2937
+
2938
+ let fileManager = MockApolloFileManager ( strict: false )
2939
+ let expectation = expectation ( description: " Received local cache mutation file data. " )
2940
+
2941
+ fileManager. mock ( closure: . createFile( { path, data, attributes in
2942
+ if path. hasSuffix ( " TestOperationLocalCacheMutation.graphql.swift " ) {
2943
+ expect ( data? . asString) . to ( equalLineByLine ( """
2944
+ init(
2945
+ allAnimals: [AllAnimal]? = nil
2946
+ ) {
2947
+ """ , atLine: 26 , ignoringExtraLines: true ) )
2948
+
2949
+ expectation. fulfill ( )
2950
+ }
2951
+
2952
+ return true
2953
+ } ) )
2954
+
2955
+ // when
2956
+ let config = ApolloCodegen . ConfigurationContext (
2957
+ config: ApolloCodegenConfiguration . mock (
2958
+ input: . init(
2959
+ schemaSearchPaths: [ directoryURL. appendingPathComponent ( " schema.graphqls " ) . path] ,
2960
+ operationSearchPaths: [ directoryURL. appendingPathComponent ( " operation.graphql " ) . path]
2961
+ ) ,
2962
+ // Apollo codegen should override the next two value to force the generation of selection set initializers
2963
+ // and perform all file merging for the local cache mutation.
2964
+ options: . init( selectionSetInitializers: [ ] ) ,
2965
+ experimentalFeatures: . init( fieldMerging: . none)
2966
+ ) ,
2967
+ rootURL: nil
2968
+ )
2969
+
2970
+ let subject = ApolloCodegen (
2971
+ config: config,
2972
+ operationIdentifierFactory: OperationIdentifierFactory ( ) ,
2973
+ itemsToGenerate: . code
2974
+ )
2975
+
2976
+ let compilationResult = try await subject. compileGraphQLResult ( )
2977
+ let ir = IRBuilder ( compilationResult: compilationResult)
2978
+
2979
+ try await subject. generateFiles (
2980
+ compilationResult: compilationResult,
2981
+ ir: ir,
2982
+ fileManager: fileManager
2983
+ )
2984
+
2985
+ // then
2986
+ expect ( fileManager. allClosuresCalled) . to ( beTrue ( ) )
2987
+
2988
+ await fulfillment ( of: [ expectation] , timeout: 1 )
2989
+ }
2990
+
2991
+ func test__fileRendering__givenLocalCacheMutationFragment_whenSelectionSetInitializersEmpty_andFileMergingNone_shouldGenerateFullSelectionSetInitializers( ) async throws {
2992
+ // given
2993
+ try createFile (
2994
+ body: """
2995
+ type Query {
2996
+ allAnimals: [Animal!]
2997
+ }
2998
+
2999
+ interface Animal {
3000
+ species: String
3001
+ }
3002
+ """ ,
3003
+ filename: " schema.graphqls "
3004
+ )
3005
+
3006
+ try createFile (
3007
+ body: """
3008
+ query TestOperation {
3009
+ allAnimals {
3010
+ ...PredatorFragment
3011
+ }
3012
+ }
3013
+
3014
+ fragment PredatorFragment on Animal @apollo_client_ios_localCacheMutation {
3015
+ species
3016
+ }
3017
+ """ ,
3018
+ filename: " operation.graphql "
3019
+ )
3020
+
3021
+ let fileManager = MockApolloFileManager ( strict: false )
3022
+ let expectation = expectation ( description: " Received local cache mutation file data. " )
3023
+
3024
+ fileManager. mock ( closure: . createFile( { path, data, attributes in
3025
+ if path. hasSuffix ( " PredatorFragment.graphql.swift " ) {
3026
+ expect ( data? . asString) . to ( equalLineByLine ( """
3027
+ init(
3028
+ __typename: String,
3029
+ species: String? = nil
3030
+ ) {
3031
+ """ , atLine: 26 , ignoringExtraLines: true ) )
3032
+
3033
+ expectation. fulfill ( )
3034
+ }
3035
+
3036
+ return true
3037
+ } ) )
3038
+
3039
+ // when
3040
+ let config = ApolloCodegen . ConfigurationContext (
3041
+ config: ApolloCodegenConfiguration . mock (
3042
+ input: . init(
3043
+ schemaSearchPaths: [ directoryURL. appendingPathComponent ( " schema.graphqls " ) . path] ,
3044
+ operationSearchPaths: [ directoryURL. appendingPathComponent ( " operation.graphql " ) . path]
3045
+ ) ,
3046
+ // Apollo codegen should override the next two value to force the generation of selection set initializers
3047
+ // and perform all file merging for the local cache mutation.
3048
+ options: . init( selectionSetInitializers: [ ] ) ,
3049
+ experimentalFeatures: . init( fieldMerging: . none)
3050
+ ) ,
3051
+ rootURL: nil
3052
+ )
3053
+
3054
+ let subject = ApolloCodegen (
3055
+ config: config,
3056
+ operationIdentifierFactory: OperationIdentifierFactory ( ) ,
3057
+ itemsToGenerate: . code
3058
+ )
3059
+
3060
+ let compilationResult = try await subject. compileGraphQLResult ( )
3061
+ let ir = IRBuilder ( compilationResult: compilationResult)
3062
+
3063
+ try await subject. generateFiles (
3064
+ compilationResult: compilationResult,
3065
+ ir: ir,
3066
+ fileManager: fileManager
3067
+ )
3068
+
3069
+ // then
3070
+ expect ( fileManager. allClosuresCalled) . to ( beTrue ( ) )
3071
+
3072
+ await fulfillment ( of: [ expectation] , timeout: 1 )
3073
+ }
3074
+
2907
3075
}
0 commit comments