From 595140a07db607e02bc9577bc5ec0edd9e175aab Mon Sep 17 00:00:00 2001 From: Anthony Miller Date: Tue, 16 Aug 2022 12:44:16 -0700 Subject: [PATCH] Fix reference to fragment with lowercase name (#2443) --- .../OperationDefinitionTemplate.swift | 2 +- ...efinitionTemplate_DocumentType_Tests.swift | 33 +++++++++++++++++++ .../PackageOne/graphql/DogFragment.graphql | 2 +- .../PackageOne/graphql/DogFragment.swift | 2 +- .../PackageOne/graphql/DogQuery.graphql | 2 +- .../Sources/PackageOne/graphql/DogQuery.swift | 2 +- 6 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Sources/ApolloCodegenLib/Templates/OperationDefinitionTemplate.swift b/Sources/ApolloCodegenLib/Templates/OperationDefinitionTemplate.swift index 2386a3fd8e..a1d5347403 100644 --- a/Sources/ApolloCodegenLib/Templates/OperationDefinitionTemplate.swift +++ b/Sources/ApolloCodegenLib/Templates/OperationDefinitionTemplate.swift @@ -64,7 +64,7 @@ struct OperationDefinitionTemplate: OperationTemplateRenderer { definition: .init( \(operation.source.formatted(for: queryStringLiteralFormat))\(if: includeFragments, ",") \(if: includeFragments, - "fragments: [\(fragments.map { "\($0.name).self" }, separator: ", ")]") + "fragments: [\(fragments.map { "\($0.name.firstUppercased).self" }, separator: ", ")]") )) """, else: """ diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplate_DocumentType_Tests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplate_DocumentType_Tests.swift index 08ad114fe3..7cef9d4439 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplate_DocumentType_Tests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplate_DocumentType_Tests.swift @@ -162,6 +162,39 @@ class OperationDefinitionTemplate_DocumentType_Tests: XCTestCase { expect(actual).to(equalLineByLine(expected)) } + func test__generate__givenIncludesFragment_fragmentNameStartsWithLowercase_generatesWithOperationDefinitionAndFragment_withFirstUppercased() throws { + // given + referencedFragments = [ + .mock("nameFragment"), + ] + + definition.source = + """ + query NameQuery { + ...nameFragment + } + """ + + config = .mock(options: .init( + queryStringLiteralFormat: .singleLine, + apqs: .disabled + )) + + // when + let actual = try renderDocumentType() + + // then + let expected = + """ + public static let document: DocumentType = .notPersisted( + definition: .init( + "query NameQuery { ...nameFragment}", + fragments: [NameFragment.self] + )) + """ + expect(actual).to(equalLineByLine(expected)) + } + func test__generate__givenIncludesManyFragments_formatMultiline_generatesWithOperationDefinitionAndFragment_asMultiline() throws { // given referencedFragments = [ diff --git a/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogFragment.graphql b/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogFragment.graphql index 92e7f4ba48..473c3d6a13 100644 --- a/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogFragment.graphql +++ b/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogFragment.graphql @@ -1,3 +1,3 @@ -fragment DogFragment on Dog { +fragment dogFragment on Dog { species } diff --git a/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogFragment.swift b/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogFragment.swift index 9dd515c620..4b1d0681b4 100644 --- a/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogFragment.swift +++ b/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogFragment.swift @@ -8,7 +8,7 @@ import PackageTwo struct DogFragment: MySchemaModule.SelectionSet, Fragment { public static var fragmentDefinition: StaticString { """ - fragment DogFragment on Dog { + fragment dogFragment on Dog { __typename species } diff --git a/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogQuery.graphql b/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogQuery.graphql index 505d019d8d..413c362787 100644 --- a/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogQuery.graphql +++ b/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogQuery.graphql @@ -1,5 +1,5 @@ query DogQuery { dog { - ... DogFragment + ... dogFragment } } diff --git a/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogQuery.swift b/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogQuery.swift index 2cf1a0f3a4..61e69aa494 100644 --- a/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogQuery.swift +++ b/Tests/TestCodeGenConfigurations/EmbeddedInTarget-RelativeAbsolute/PackageOne/Sources/PackageOne/graphql/DogQuery.swift @@ -14,7 +14,7 @@ class DogQuery: GraphQLQuery { query DogQuery { dog { __typename - ...DogFragment + ...dogFragment } } """,