Skip to content

Commit 948577c

Browse files
author
Dane Pilcher
authored
fix: pass improve pluralization flag (#797)
1 parent df65382 commit 948577c

File tree

5 files changed

+374
-0
lines changed

5 files changed

+374
-0
lines changed

packages/graphql-generator/API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export type GenerateModelsOptions = {
2727
useExperimentalPipelinedTransformer?: boolean;
2828
transformerVersion?: boolean;
2929
respectPrimaryKeyAttributesOnConnectionField?: boolean;
30+
improvePluralization?: boolean;
3031
generateModelsForLazyLoadAndCustomSelectionSet?: boolean;
3132
addTimestampFields?: boolean;
3233
handleListNullabilityTransparently?: boolean;

packages/graphql-generator/src/__tests__/__snapshots__/models.test.ts.snap

Lines changed: 359 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,3 +2882,362 @@ export const schema: Schema = {
28822882
};",
28832883
}
28842884
`;
2885+
2886+
exports[`generateModels targets improve pluralization swift 1`] = `
2887+
Object {
2888+
"AmplifyModels.swift": "// swiftlint:disable all
2889+
import Amplify
2890+
import Foundation
2891+
2892+
// Contains the set of classes that conforms to the \`Model\` protocol.
2893+
2894+
final public class AmplifyModels: AmplifyModelRegistration {
2895+
public let version: String = \\"165944a36979cd395e3b22145bbfeff0\\"
2896+
2897+
public func registerModels(registry: ModelRegistry.Type) {
2898+
ModelRegistry.register(modelType: Blog.self)
2899+
ModelRegistry.register(modelType: Post.self)
2900+
ModelRegistry.register(modelType: Comment.self)
2901+
}
2902+
}",
2903+
"Blog+Schema.swift": "// swiftlint:disable all
2904+
import Amplify
2905+
import Foundation
2906+
2907+
extension Blog {
2908+
// MARK: - CodingKeys
2909+
public enum CodingKeys: String, ModelKey {
2910+
case id
2911+
case name
2912+
case posts
2913+
case createdAt
2914+
case updatedAt
2915+
}
2916+
2917+
public static let keys = CodingKeys.self
2918+
// MARK: - ModelSchema
2919+
2920+
public static let schema = defineSchema { model in
2921+
let blog = Blog.keys
2922+
2923+
model.listPluralName = \\"Blogs\\"
2924+
model.syncPluralName = \\"Blogs\\"
2925+
2926+
model.attributes(
2927+
.primaryKey(fields: [blog.id])
2928+
)
2929+
2930+
model.fields(
2931+
.field(blog.id, is: .required, ofType: .string),
2932+
.field(blog.name, is: .required, ofType: .string),
2933+
.hasMany(blog.posts, is: .optional, ofType: Post.self, associatedWith: Post.keys.blog),
2934+
.field(blog.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
2935+
.field(blog.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
2936+
)
2937+
}
2938+
public class Path: ModelPath<Blog> { }
2939+
2940+
public static var rootPath: PropertyContainerPath? { Path() }
2941+
}
2942+
2943+
extension Blog: ModelIdentifiable {
2944+
public typealias IdentifierFormat = ModelIdentifierFormat.Default
2945+
public typealias IdentifierProtocol = DefaultModelIdentifier<Self>
2946+
}
2947+
extension ModelPath where ModelType == Blog {
2948+
public var id: FieldPath<String> {
2949+
string(\\"id\\")
2950+
}
2951+
public var name: FieldPath<String> {
2952+
string(\\"name\\")
2953+
}
2954+
public var posts: ModelPath<Post> {
2955+
Post.Path(name: \\"posts\\", isCollection: true, parent: self)
2956+
}
2957+
public var createdAt: FieldPath<Temporal.DateTime> {
2958+
datetime(\\"createdAt\\")
2959+
}
2960+
public var updatedAt: FieldPath<Temporal.DateTime> {
2961+
datetime(\\"updatedAt\\")
2962+
}
2963+
}",
2964+
"Blog.swift": "// swiftlint:disable all
2965+
import Amplify
2966+
import Foundation
2967+
2968+
public struct Blog: Model {
2969+
public let id: String
2970+
public var name: String
2971+
public var posts: List<Post>?
2972+
public var createdAt: Temporal.DateTime?
2973+
public var updatedAt: Temporal.DateTime?
2974+
2975+
public init(id: String = UUID().uuidString,
2976+
name: String,
2977+
posts: List<Post>? = []) {
2978+
self.init(id: id,
2979+
name: name,
2980+
posts: posts,
2981+
createdAt: nil,
2982+
updatedAt: nil)
2983+
}
2984+
internal init(id: String = UUID().uuidString,
2985+
name: String,
2986+
posts: List<Post>? = [],
2987+
createdAt: Temporal.DateTime? = nil,
2988+
updatedAt: Temporal.DateTime? = nil) {
2989+
self.id = id
2990+
self.name = name
2991+
self.posts = posts
2992+
self.createdAt = createdAt
2993+
self.updatedAt = updatedAt
2994+
}
2995+
}",
2996+
"Comment+Schema.swift": "// swiftlint:disable all
2997+
import Amplify
2998+
import Foundation
2999+
3000+
extension Comment {
3001+
// MARK: - CodingKeys
3002+
public enum CodingKeys: String, ModelKey {
3003+
case id
3004+
case post
3005+
case content
3006+
case createdAt
3007+
case updatedAt
3008+
}
3009+
3010+
public static let keys = CodingKeys.self
3011+
// MARK: - ModelSchema
3012+
3013+
public static let schema = defineSchema { model in
3014+
let comment = Comment.keys
3015+
3016+
model.listPluralName = \\"Comments\\"
3017+
model.syncPluralName = \\"Comments\\"
3018+
3019+
model.attributes(
3020+
.primaryKey(fields: [comment.id])
3021+
)
3022+
3023+
model.fields(
3024+
.field(comment.id, is: .required, ofType: .string),
3025+
.belongsTo(comment.post, is: .optional, ofType: Post.self, targetNames: [\\"postCommentsId\\"]),
3026+
.field(comment.content, is: .required, ofType: .string),
3027+
.field(comment.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
3028+
.field(comment.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
3029+
)
3030+
}
3031+
public class Path: ModelPath<Comment> { }
3032+
3033+
public static var rootPath: PropertyContainerPath? { Path() }
3034+
}
3035+
3036+
extension Comment: ModelIdentifiable {
3037+
public typealias IdentifierFormat = ModelIdentifierFormat.Default
3038+
public typealias IdentifierProtocol = DefaultModelIdentifier<Self>
3039+
}
3040+
extension ModelPath where ModelType == Comment {
3041+
public var id: FieldPath<String> {
3042+
string(\\"id\\")
3043+
}
3044+
public var post: ModelPath<Post> {
3045+
Post.Path(name: \\"post\\", parent: self)
3046+
}
3047+
public var content: FieldPath<String> {
3048+
string(\\"content\\")
3049+
}
3050+
public var createdAt: FieldPath<Temporal.DateTime> {
3051+
datetime(\\"createdAt\\")
3052+
}
3053+
public var updatedAt: FieldPath<Temporal.DateTime> {
3054+
datetime(\\"updatedAt\\")
3055+
}
3056+
}",
3057+
"Comment.swift": "// swiftlint:disable all
3058+
import Amplify
3059+
import Foundation
3060+
3061+
public struct Comment: Model {
3062+
public let id: String
3063+
internal var _post: LazyReference<Post>
3064+
public var post: Post? {
3065+
get async throws {
3066+
try await _post.get()
3067+
}
3068+
}
3069+
public var content: String
3070+
public var createdAt: Temporal.DateTime?
3071+
public var updatedAt: Temporal.DateTime?
3072+
3073+
public init(id: String = UUID().uuidString,
3074+
post: Post? = nil,
3075+
content: String) {
3076+
self.init(id: id,
3077+
post: post,
3078+
content: content,
3079+
createdAt: nil,
3080+
updatedAt: nil)
3081+
}
3082+
internal init(id: String = UUID().uuidString,
3083+
post: Post? = nil,
3084+
content: String,
3085+
createdAt: Temporal.DateTime? = nil,
3086+
updatedAt: Temporal.DateTime? = nil) {
3087+
self.id = id
3088+
self._post = LazyReference(post)
3089+
self.content = content
3090+
self.createdAt = createdAt
3091+
self.updatedAt = updatedAt
3092+
}
3093+
public mutating func setPost(_ post: Post? = nil) {
3094+
self._post = LazyReference(post)
3095+
}
3096+
public init(from decoder: Decoder) throws {
3097+
let values = try decoder.container(keyedBy: CodingKeys.self)
3098+
id = try values.decode(String.self, forKey: .id)
3099+
_post = try values.decodeIfPresent(LazyReference<Post>.self, forKey: .post) ?? LazyReference(identifiers: nil)
3100+
content = try values.decode(String.self, forKey: .content)
3101+
createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt)
3102+
updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt)
3103+
}
3104+
public func encode(to encoder: Encoder) throws {
3105+
var container = encoder.container(keyedBy: CodingKeys.self)
3106+
try container.encode(id, forKey: .id)
3107+
try container.encode(_post, forKey: .post)
3108+
try container.encode(content, forKey: .content)
3109+
try container.encode(createdAt, forKey: .createdAt)
3110+
try container.encode(updatedAt, forKey: .updatedAt)
3111+
}
3112+
}",
3113+
"Post+Schema.swift": "// swiftlint:disable all
3114+
import Amplify
3115+
import Foundation
3116+
3117+
extension Post {
3118+
// MARK: - CodingKeys
3119+
public enum CodingKeys: String, ModelKey {
3120+
case id
3121+
case title
3122+
case blog
3123+
case comments
3124+
case createdAt
3125+
case updatedAt
3126+
}
3127+
3128+
public static let keys = CodingKeys.self
3129+
// MARK: - ModelSchema
3130+
3131+
public static let schema = defineSchema { model in
3132+
let post = Post.keys
3133+
3134+
model.listPluralName = \\"Posts\\"
3135+
model.syncPluralName = \\"Posts\\"
3136+
3137+
model.attributes(
3138+
.primaryKey(fields: [post.id])
3139+
)
3140+
3141+
model.fields(
3142+
.field(post.id, is: .required, ofType: .string),
3143+
.field(post.title, is: .required, ofType: .string),
3144+
.belongsTo(post.blog, is: .optional, ofType: Blog.self, targetNames: [\\"blogPostsId\\"]),
3145+
.hasMany(post.comments, is: .optional, ofType: Comment.self, associatedWith: Comment.keys.post),
3146+
.field(post.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
3147+
.field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
3148+
)
3149+
}
3150+
public class Path: ModelPath<Post> { }
3151+
3152+
public static var rootPath: PropertyContainerPath? { Path() }
3153+
}
3154+
3155+
extension Post: ModelIdentifiable {
3156+
public typealias IdentifierFormat = ModelIdentifierFormat.Default
3157+
public typealias IdentifierProtocol = DefaultModelIdentifier<Self>
3158+
}
3159+
extension ModelPath where ModelType == Post {
3160+
public var id: FieldPath<String> {
3161+
string(\\"id\\")
3162+
}
3163+
public var title: FieldPath<String> {
3164+
string(\\"title\\")
3165+
}
3166+
public var blog: ModelPath<Blog> {
3167+
Blog.Path(name: \\"blog\\", parent: self)
3168+
}
3169+
public var comments: ModelPath<Comment> {
3170+
Comment.Path(name: \\"comments\\", isCollection: true, parent: self)
3171+
}
3172+
public var createdAt: FieldPath<Temporal.DateTime> {
3173+
datetime(\\"createdAt\\")
3174+
}
3175+
public var updatedAt: FieldPath<Temporal.DateTime> {
3176+
datetime(\\"updatedAt\\")
3177+
}
3178+
}",
3179+
"Post.swift": "// swiftlint:disable all
3180+
import Amplify
3181+
import Foundation
3182+
3183+
public struct Post: Model {
3184+
public let id: String
3185+
public var title: String
3186+
internal var _blog: LazyReference<Blog>
3187+
public var blog: Blog? {
3188+
get async throws {
3189+
try await _blog.get()
3190+
}
3191+
}
3192+
public var comments: List<Comment>?
3193+
public var createdAt: Temporal.DateTime?
3194+
public var updatedAt: Temporal.DateTime?
3195+
3196+
public init(id: String = UUID().uuidString,
3197+
title: String,
3198+
blog: Blog? = nil,
3199+
comments: List<Comment>? = []) {
3200+
self.init(id: id,
3201+
title: title,
3202+
blog: blog,
3203+
comments: comments,
3204+
createdAt: nil,
3205+
updatedAt: nil)
3206+
}
3207+
internal init(id: String = UUID().uuidString,
3208+
title: String,
3209+
blog: Blog? = nil,
3210+
comments: List<Comment>? = [],
3211+
createdAt: Temporal.DateTime? = nil,
3212+
updatedAt: Temporal.DateTime? = nil) {
3213+
self.id = id
3214+
self.title = title
3215+
self._blog = LazyReference(blog)
3216+
self.comments = comments
3217+
self.createdAt = createdAt
3218+
self.updatedAt = updatedAt
3219+
}
3220+
public mutating func setBlog(_ blog: Blog? = nil) {
3221+
self._blog = LazyReference(blog)
3222+
}
3223+
public init(from decoder: Decoder) throws {
3224+
let values = try decoder.container(keyedBy: CodingKeys.self)
3225+
id = try values.decode(String.self, forKey: .id)
3226+
title = try values.decode(String.self, forKey: .title)
3227+
_blog = try values.decodeIfPresent(LazyReference<Blog>.self, forKey: .blog) ?? LazyReference(identifiers: nil)
3228+
comments = try values.decodeIfPresent(List<Comment>?.self, forKey: .comments) ?? .init()
3229+
createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt)
3230+
updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt)
3231+
}
3232+
public func encode(to encoder: Encoder) throws {
3233+
var container = encoder.container(keyedBy: CodingKeys.self)
3234+
try container.encode(id, forKey: .id)
3235+
try container.encode(title, forKey: .title)
3236+
try container.encode(_blog, forKey: .blog)
3237+
try container.encode(comments, forKey: .comments)
3238+
try container.encode(createdAt, forKey: .createdAt)
3239+
try container.encode(updatedAt, forKey: .updatedAt)
3240+
}
3241+
}",
3242+
}
3243+
`;

packages/graphql-generator/src/__tests__/models.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ describe('generateModels', () => {
1515
expect(models).toMatchSnapshot();
1616
});
1717
});
18+
19+
test(`improve pluralization swift`, async () => {
20+
const options: GenerateModelsOptions = {
21+
schema: readSchema('blog-model.graphql'),
22+
target: 'swift',
23+
directives,
24+
improvePluralization: true,
25+
};
26+
const models = await generateModels(options);
27+
expect(models).toMatchSnapshot();
28+
});
1829
});
1930
});
2031

0 commit comments

Comments
 (0)