@k2works/claude-code-booster 3.2.1 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/lib/assets/docs/article/index.md +4 -1
  2. package/lib/assets/docs/article/practical-database-design/index.md +121 -0
  3. package/lib/assets/docs/article/practical-database-design/part1/chapter01.md +288 -0
  4. package/lib/assets/docs/article/practical-database-design/part1/chapter02.md +518 -0
  5. package/lib/assets/docs/article/practical-database-design/part1/chapter03.md +557 -0
  6. package/lib/assets/docs/article/practical-database-design/part2/chapter04.md +924 -0
  7. package/lib/assets/docs/article/practical-database-design/part2/chapter05.md +1627 -0
  8. package/lib/assets/docs/article/practical-database-design/part2/chapter06.md +2716 -0
  9. package/lib/assets/docs/article/practical-database-design/part2/chapter07.md +2082 -0
  10. package/lib/assets/docs/article/practical-database-design/part2/chapter08.md +2105 -0
  11. package/lib/assets/docs/article/practical-database-design/part2/chapter09.md +2031 -0
  12. package/lib/assets/docs/article/practical-database-design/part2/chapter10.md +1387 -0
  13. package/lib/assets/docs/article/practical-database-design/part2/chapter11.md +1677 -0
  14. package/lib/assets/docs/article/practical-database-design/part2/chapter12.md +1417 -0
  15. package/lib/assets/docs/article/practical-database-design/part2/chapter13.md +1434 -0
  16. package/lib/assets/docs/article/practical-database-design/part3/chapter14.md +667 -0
  17. package/lib/assets/docs/article/practical-database-design/part3/chapter15.md +1625 -0
  18. package/lib/assets/docs/article/practical-database-design/part3/chapter16.md +1915 -0
  19. package/lib/assets/docs/article/practical-database-design/part3/chapter17.md +1708 -0
  20. package/lib/assets/docs/article/practical-database-design/part3/chapter18.md +2095 -0
  21. package/lib/assets/docs/article/practical-database-design/part3/chapter19.md +1123 -0
  22. package/lib/assets/docs/article/practical-database-design/part3/chapter20.md +1031 -0
  23. package/lib/assets/docs/article/practical-database-design/part3/chapter21.md +1382 -0
  24. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter14-orm.md +991 -0
  25. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter15-orm.md +1300 -0
  26. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter16-orm.md +1166 -0
  27. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter17-orm.md +1584 -0
  28. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter18-orm.md +1183 -0
  29. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter19-orm.md +1016 -0
  30. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter20-orm.md +1753 -0
  31. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter21-orm.md +1447 -0
  32. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter22-orm.md +1878 -0
  33. package/lib/assets/docs/article/practical-database-design/part4/chapter22.md +965 -0
  34. package/lib/assets/docs/article/practical-database-design/part4/chapter23.md +2069 -0
  35. package/lib/assets/docs/article/practical-database-design/part4/chapter24.md +2439 -0
  36. package/lib/assets/docs/article/practical-database-design/part4/chapter25.md +3661 -0
  37. package/lib/assets/docs/article/practical-database-design/part4/chapter26.md +2916 -0
  38. package/lib/assets/docs/article/practical-database-design/part4/chapter27.md +3105 -0
  39. package/lib/assets/docs/article/practical-database-design/part4/chapter28.md +2697 -0
  40. package/lib/assets/docs/article/practical-database-design/part4/chapter29.md +2544 -0
  41. package/lib/assets/docs/article/practical-database-design/part4/chapter30.md +2180 -0
  42. package/lib/assets/docs/article/practical-database-design/part4/chapter31.md +1192 -0
  43. package/lib/assets/docs/article/practical-database-design/part4/chapter32.md +2101 -0
  44. package/lib/assets/docs/article/practical-database-design/part5/chapter33.md +1032 -0
  45. package/lib/assets/docs/article/practical-database-design/part5/chapter34.md +1609 -0
  46. package/lib/assets/docs/article/practical-database-design/part5/chapter35.md +1453 -0
  47. package/lib/assets/docs/article/practical-database-design/part5/chapter36.md +1292 -0
  48. package/lib/assets/docs/article/practical-database-design/part5/chapter37.md +1470 -0
  49. package/lib/assets/docs/article/practical-database-design/part5/chapter38.md +1698 -0
  50. package/lib/assets/docs/article/practical-database-design/part5/chapter39.md +2334 -0
  51. package/lib/assets/docs/article/practical-database-design/study/study2-1.md +1693 -0
  52. package/lib/assets/docs/article/practical-database-design/study/study2-2.md +1347 -0
  53. package/lib/assets/docs/article/practical-database-design/study/study2-3.md +2044 -0
  54. package/lib/assets/docs/article/practical-database-design/study/study2-4.md +2229 -0
  55. package/lib/assets/docs/article/practical-database-design/study/study2-5.md +2418 -0
  56. package/lib/assets/docs/article/practical-database-design/study/study3-1.md +2205 -0
  57. package/lib/assets/docs/article/practical-database-design/study/study3-2.md +2221 -0
  58. package/lib/assets/docs/article/practical-database-design/study/study3-3.md +2253 -0
  59. package/lib/assets/docs/article/practical-database-design/study/study3-4.md +2106 -0
  60. package/lib/assets/docs/article/practical-database-design/study/study3-5.md +2507 -0
  61. package/lib/assets/docs/article/practical-database-design/study/study4-1.md +2587 -0
  62. package/lib/assets/docs/article/practical-database-design/study/study4-2.md +2075 -0
  63. package/lib/assets/docs/article/practical-database-design/study/study4-3.md +1805 -0
  64. package/lib/assets/docs/article/practical-database-design/study/study4-4.md +1895 -0
  65. package/lib/assets/docs/article/practical-database-design/study/study4-5.md +2878 -0
  66. package/package.json +1 -1
@@ -0,0 +1,2106 @@
1
+ # 研究 4:GraphQL サービスの実装
2
+
3
+ ## はじめに
4
+
5
+ 本パートでは、REST API や gRPC とは異なるアプローチとして、**GraphQL** による財務会計システムを実装します。クライアントが必要なデータを正確に指定できる柔軟なクエリと、リアルタイム更新を実現する Subscription を活用します。
6
+
7
+ ヘキサゴナルアーキテクチャ(ドメイン層・アプリケーション層)はそのまま共有し、**Input Adapter として GraphQL リゾルバ層のみを追加**します。
8
+
9
+ ---
10
+
11
+ ## 第 22 章:GraphQL サーバーの基礎
12
+
13
+ ### 22.1 GraphQL とは
14
+
15
+ GraphQL は Facebook が開発したクエリ言語および実行エンジンです。クライアントが必要なデータの形状を指定でき、Over-fetching(不要なデータの取得)や Under-fetching(必要なデータの不足)を防ぎます。
16
+
17
+ ```plantuml
18
+ @startuml graphql_architecture
19
+ !define RECTANGLE class
20
+
21
+ skinparam backgroundColor #FEFEFE
22
+
23
+ package "GraphQL Architecture (財務会計システム)" {
24
+
25
+ package "Client Side" {
26
+ RECTANGLE "GraphQL Client\n(Apollo/Relay/urql)" as client {
27
+ - Query (読み取り)
28
+ - Mutation (書き込み)
29
+ - Subscription (リアルタイム)
30
+ }
31
+ }
32
+
33
+ package "Server Side" {
34
+ RECTANGLE "GraphQL Server\n(Spring for GraphQL)" as server {
35
+ - AccountResolver
36
+ - JournalResolver
37
+ - BalanceResolver
38
+ - ReportResolver
39
+ }
40
+ }
41
+
42
+ package "Shared" {
43
+ RECTANGLE "GraphQL Schema\n(.graphqls files)" as schema {
44
+ - schema.graphqls
45
+ - account.graphqls
46
+ - journal.graphqls
47
+ - balance.graphqls
48
+ - report.graphqls
49
+ }
50
+ }
51
+ }
52
+
53
+ client --> schema : "スキーマに基づいて\nクエリを構築"
54
+ server --> schema : "スキーマに基づいて\nリゾルバを実装"
55
+ client <--> server : "HTTP/WebSocket\n(JSON)"
56
+
57
+ note bottom of schema
58
+ スキーマ駆動開発
59
+ クライアント主導のデータ取得
60
+ 型安全な API
61
+ end note
62
+
63
+ @enduml
64
+ ```
65
+
66
+ **REST API / gRPC / GraphQL の比較:**
67
+
68
+ | 特徴 | REST API | gRPC | GraphQL |
69
+ |------|----------|------|---------|
70
+ | プロトコル | HTTP/1.1 | HTTP/2 | HTTP/1.1 or HTTP/2 |
71
+ | データ形式 | JSON | Protocol Buffers | JSON |
72
+ | スキーマ | OpenAPI (任意) | .proto (必須) | .graphqls (必須) |
73
+ | データ取得 | 固定レスポンス | 固定レスポンス | クライアント指定 |
74
+ | エンドポイント | 複数 | 複数 | 単一 |
75
+ | リアルタイム | WebSocket 別実装 | ストリーミング | Subscription |
76
+ | 主な用途 | 汎用 API | マイクロサービス | フロントエンド向け |
77
+
78
+ ### 22.2 3 つの操作タイプ
79
+
80
+ GraphQL は 3 つの操作タイプをサポートします:
81
+
82
+ ```plantuml
83
+ @startuml graphql_operations
84
+ skinparam backgroundColor #FEFEFE
85
+
86
+ rectangle "1. Query\n(読み取り)" as query #LightBlue {
87
+ }
88
+ note right of query
89
+ { accounts { code name category } }
90
+ → { accounts: [...] }
91
+
92
+ データの取得
93
+ 複数リソースを1回で取得可能
94
+ end note
95
+
96
+ rectangle "2. Mutation\n(書き込み)" as mutation #LightGreen {
97
+ }
98
+ note right of mutation
99
+ mutation { createJournal(...) }
100
+ → { createJournal: {...} }
101
+
102
+ データの作成・更新・削除
103
+ end note
104
+
105
+ rectangle "3. Subscription\n(リアルタイム)" as subscription #LightCoral {
106
+ }
107
+ note right of subscription
108
+ subscription { balanceChanged }
109
+ → (WebSocket でプッシュ通知)
110
+
111
+ リアルタイム更新の受信
112
+ end note
113
+
114
+ query -[hidden]-> mutation
115
+ mutation -[hidden]-> subscription
116
+
117
+ @enduml
118
+ ```
119
+
120
+ **用途:**
121
+
122
+ 1. **Query**: データ取得(勘定科目一覧、仕訳照会、残高照会)
123
+ 2. **Mutation**: データ更新(仕訳登録、仕訳取消、決算仕訳)
124
+ 3. **Subscription**: リアルタイム通知(残高変動、仕訳登録通知)
125
+
126
+ ### 22.3 GraphQL におけるヘキサゴナルアーキテクチャ
127
+
128
+ GraphQL を導入しても、ヘキサゴナルアーキテクチャ(ドメイン層・アプリケーション層)はそのまま共有し、**Input Adapter として GraphQL リゾルバ層のみを追加**します。
129
+
130
+ ```plantuml
131
+ @startuml hexagonal_graphql
132
+ !define RECTANGLE class
133
+
134
+ package "Hexagonal Architecture (GraphQL版)" {
135
+
136
+ RECTANGLE "Application Core\n(Domain + Use Cases)" as core {
137
+ - Account (勘定科目)
138
+ - Journal (仕訳)
139
+ - DailyBalance (日次残高)
140
+ - MonthlyBalance (月次残高)
141
+ - AccountUseCase
142
+ - JournalUseCase
143
+ - BalanceUseCase
144
+ }
145
+
146
+ RECTANGLE "Input Adapters\n(Driving Side)" as input {
147
+ - REST Controller(既存)
148
+ - gRPC Service(既存)
149
+ - GraphQL Resolver(新規追加)
150
+ - DataFetcher
151
+ - DataLoader
152
+ }
153
+
154
+ RECTANGLE "Output Adapters\n(Driven Side)" as output {
155
+ - MyBatis Repository
156
+ - Database Access
157
+ - Entity Mapping
158
+ }
159
+ }
160
+
161
+ input --> core : "Input Ports\n(Use Cases)"
162
+ core --> output : "Output Ports\n(Repository Interfaces)"
163
+
164
+ note top of core
165
+ 既存のビジネスロジック
166
+ REST API / gRPC 版と完全に共有
167
+ GraphQL 固有のコードは含まない
168
+ end note
169
+
170
+ note left of input
171
+ GraphQL リゾルバを
172
+ Input Adapter として追加
173
+ 既存の REST/gRPC と共存可能
174
+ end note
175
+
176
+ note right of output
177
+ 既存の Repository を
178
+ そのまま使用
179
+ 変更不要
180
+ end note
181
+
182
+ @enduml
183
+ ```
184
+
185
+ ### 22.4 ディレクトリ構成
186
+
187
+ 既存の構成に `infrastructure/graphql/` を追加するだけです。
188
+
189
+ ```
190
+ src/main/java/com/example/accounting/
191
+ ├── domain/ # ドメイン層(API版と共通)
192
+ │ ├── model/
193
+ │ │ ├── account/
194
+ │ │ ├── journal/
195
+ │ │ ├── balance/
196
+ │ │ └── report/
197
+ │ └── exception/
198
+
199
+ ├── application/ # アプリケーション層(API版と共通)
200
+ │ ├── port/
201
+ │ │ ├── in/ # Input Port(ユースケース)
202
+ │ │ └── out/ # Output Port(リポジトリ)
203
+ │ └── service/
204
+
205
+ ├── infrastructure/
206
+ │ ├── persistence/ # Output Adapter(DB実装)- 既存
207
+ │ │ ├── mapper/
208
+ │ │ └── repository/
209
+ │ ├── rest/ # Input Adapter(REST実装)- 既存
210
+ │ ├── grpc/ # Input Adapter(gRPC実装)- 既存
211
+ │ └── graphql/ # Input Adapter(GraphQL実装)- 新規追加
212
+ │ ├── resolver/ # Query/Mutation リゾルバ
213
+ │ ├── dataloader/ # N+1 問題対策
214
+ │ ├── scalar/ # カスタムスカラー型
215
+ │ └── subscription/ # Subscription ハンドラ
216
+
217
+ ├── config/
218
+
219
+ └── src/main/resources/
220
+ └── graphql/ # GraphQL スキーマ定義
221
+ ├── schema.graphqls
222
+ ├── account.graphqls
223
+ ├── journal.graphqls
224
+ ├── balance.graphqls
225
+ └── report.graphqls
226
+ ```
227
+
228
+ ### 22.5 技術スタックの追加
229
+
230
+ 既存の `build.gradle.kts` に GraphQL 関連の依存関係を追加します。
231
+
232
+ <details>
233
+ <summary>build.gradle.kts(差分)</summary>
234
+
235
+ ```kotlin
236
+ dependencies {
237
+ // 既存の依存関係(Spring Boot, MyBatis, PostgreSQL等)はそのまま
238
+
239
+ // GraphQL 関連を追加
240
+ implementation("org.springframework.boot:spring-boot-starter-graphql")
241
+ implementation("org.springframework.boot:spring-boot-starter-websocket") // Subscription 用
242
+
243
+ // GraphQL 拡張
244
+ implementation("com.graphql-java:graphql-java-extended-scalars:21.0")
245
+
246
+ // Test
247
+ testImplementation("org.springframework.graphql:spring-graphql-test")
248
+ }
249
+ ```
250
+
251
+ </details>
252
+
253
+ **追加パッケージの説明:**
254
+
255
+ | パッケージ | 用途 |
256
+ |-----------|------|
257
+ | spring-boot-starter-graphql | Spring Boot GraphQL 統合 |
258
+ | spring-boot-starter-websocket | Subscription (WebSocket) |
259
+ | graphql-java-extended-scalars | DateTime, BigDecimal 等のスカラー型 |
260
+ | spring-graphql-test | GraphQL テストサポート |
261
+
262
+ ### 22.6 GraphQL 設定
263
+
264
+ <details>
265
+ <summary>application.yml(差分)</summary>
266
+
267
+ ```yaml
268
+ spring:
269
+ graphql:
270
+ graphiql:
271
+ enabled: true
272
+ path: /graphiql
273
+ websocket:
274
+ path: /graphql
275
+ schema:
276
+ printer:
277
+ enabled: true
278
+ locations:
279
+ - classpath:graphql/
280
+ cors:
281
+ allowed-origins: "*"
282
+ allowed-methods: GET, POST
283
+ ```
284
+
285
+ </details>
286
+
287
+ <details>
288
+ <summary>GraphQLConfig.java</summary>
289
+
290
+ ```java
291
+ package com.example.accounting.config;
292
+
293
+ import graphql.scalars.ExtendedScalars;
294
+ import graphql.schema.idl.RuntimeWiring;
295
+ import org.springframework.context.annotation.Bean;
296
+ import org.springframework.context.annotation.Configuration;
297
+ import org.springframework.graphql.execution.RuntimeWiringConfigurer;
298
+
299
+ /**
300
+ * GraphQL 設定
301
+ */
302
+ @Configuration
303
+ public class GraphQLConfig {
304
+
305
+ @Bean
306
+ public RuntimeWiringConfigurer runtimeWiringConfigurer() {
307
+ return wiringBuilder -> wiringBuilder
308
+ .scalar(ExtendedScalars.Date)
309
+ .scalar(ExtendedScalars.DateTime)
310
+ .scalar(ExtendedScalars.GraphQLBigDecimal)
311
+ .scalar(ExtendedScalars.GraphQLLong);
312
+ }
313
+ }
314
+ ```
315
+
316
+ </details>
317
+
318
+ ---
319
+
320
+ ## 第 23 章:GraphQL スキーマの定義
321
+
322
+ ### 23.1 ルートスキーマ
323
+
324
+ <details>
325
+ <summary>src/main/resources/graphql/schema.graphqls</summary>
326
+
327
+ ```graphql
328
+ # ルートスキーマ
329
+ type Query {
330
+ # 勘定科目
331
+ account(accountCode: ID!): Account
332
+ accounts(bsplCategory: BSPLCategory, page: Int, size: Int): AccountConnection!
333
+ accountHierarchy(accountCode: ID!): [Account!]!
334
+
335
+ # 課税取引
336
+ taxTransaction(taxCode: ID!): TaxTransaction
337
+ taxTransactions: [TaxTransaction!]!
338
+
339
+ # 勘定科目構成
340
+ accountConfig(accountCode: ID!): AccountConfig
341
+ accountConfigs(parentPath: String): [AccountConfig!]!
342
+
343
+ # 仕訳
344
+ journal(journalNumber: ID!): Journal
345
+ journals(fromDate: Date, toDate: Date, accountCode: ID, page: Int, size: Int): JournalConnection!
346
+
347
+ # 残高
348
+ dailyBalance(date: Date!, accountCode: ID!, subAccountCode: ID): DailyBalance
349
+ dailyBalances(date: Date!, page: Int, size: Int): DailyBalanceConnection!
350
+
351
+ monthlyBalance(fiscalYear: Int!, month: Int!, accountCode: ID!, subAccountCode: ID): MonthlyBalance
352
+ monthlyBalances(fiscalYear: Int!, month: Int!, page: Int, size: Int): MonthlyBalanceConnection!
353
+
354
+ # 帳票
355
+ dailyReport(date: Date!): DailyReport!
356
+ trialBalance(fiscalYear: Int!, month: Int!): TrialBalance!
357
+ financialStatements(fiscalYear: Int!): FinancialStatements!
358
+ }
359
+
360
+ type Mutation {
361
+ # 勘定科目
362
+ createAccount(input: CreateAccountInput!): Account!
363
+ updateAccount(input: UpdateAccountInput!): Account!
364
+ deleteAccount(accountCode: ID!): Boolean!
365
+
366
+ # 課税取引
367
+ createTaxTransaction(input: CreateTaxTransactionInput!): TaxTransaction!
368
+ updateTaxTransaction(input: UpdateTaxTransactionInput!): TaxTransaction!
369
+ deleteTaxTransaction(taxCode: ID!): Boolean!
370
+
371
+ # 仕訳
372
+ createJournal(input: CreateJournalInput!): Journal!
373
+ updateJournal(input: UpdateJournalInput!): Journal!
374
+ deleteJournal(journalNumber: ID!): Boolean!
375
+ reverseJournal(journalNumber: ID!, reason: String): Journal!
376
+
377
+ # 残高更新
378
+ recalculateDailyBalance(date: Date!): RecalculateResult!
379
+ recalculateMonthlyBalance(fiscalYear: Int!, month: Int!): RecalculateResult!
380
+
381
+ # 決算処理
382
+ executeClosing(input: ExecuteClosingInput!): ClosingResult!
383
+ }
384
+
385
+ type Subscription {
386
+ # 残高変動
387
+ balanceChanged(accountCode: ID): BalanceChange!
388
+
389
+ # 仕訳登録通知
390
+ journalCreated: JournalNotification!
391
+
392
+ # 決算処理進捗
393
+ closingProgressUpdated(closingId: ID!): ClosingProgress!
394
+ }
395
+
396
+ # ページネーション共通型
397
+ type PageInfo {
398
+ hasNextPage: Boolean!
399
+ hasPreviousPage: Boolean!
400
+ totalElements: Int!
401
+ totalPages: Int!
402
+ currentPage: Int!
403
+ }
404
+
405
+ # カスタムスカラー
406
+ scalar Date
407
+ scalar DateTime
408
+ scalar BigDecimal
409
+ scalar Long
410
+ ```
411
+
412
+ </details>
413
+
414
+ ### 23.2 勘定科目スキーマ
415
+
416
+ <details>
417
+ <summary>src/main/resources/graphql/account.graphqls</summary>
418
+
419
+ ```graphql
420
+ # BSPL区分
421
+ enum BSPLCategory {
422
+ BS # 貸借対照表
423
+ PL # 損益計算書
424
+ }
425
+
426
+ # 貸借区分
427
+ enum DebitCreditCategory {
428
+ DEBIT # 借方
429
+ CREDIT # 貸方
430
+ }
431
+
432
+ # 取引要素区分
433
+ enum TransactionElementCategory {
434
+ ASSET # 資産
435
+ LIABILITY # 負債
436
+ CAPITAL # 資本
437
+ REVENUE # 収益
438
+ EXPENSE # 費用
439
+ }
440
+
441
+ # 集計区分
442
+ enum AggregationCategory {
443
+ HEADING # 見出科目
444
+ SUBTOTAL # 集計科目
445
+ POSTING # 計上科目
446
+ }
447
+
448
+ # 勘定科目
449
+ type Account {
450
+ accountCode: ID!
451
+ accountName: String!
452
+ accountShortName: String
453
+ accountKana: String
454
+ bsplCategory: BSPLCategory!
455
+ debitCreditCategory: DebitCreditCategory!
456
+ transactionElementCategory: TransactionElementCategory!
457
+ aggregationCategory: AggregationCategory!
458
+ managementAccountingCategory: String
459
+ expenseCategory: String
460
+ ledgerOutputCategory: String
461
+ subAccountType: String
462
+ taxCalculationCategory: String
463
+ taxTransactionCode: ID
464
+ dueDateManagementCategory: String
465
+ createdAt: DateTime!
466
+ updatedAt: DateTime!
467
+ updatedBy: String
468
+
469
+ # 関連データ(必要な場合のみ取得)
470
+ taxTransaction: TaxTransaction
471
+ accountConfig: AccountConfig
472
+ childAccounts: [Account!]!
473
+ dailyBalances(date: Date): [DailyBalance!]!
474
+ monthlyBalances(fiscalYear: Int, month: Int): [MonthlyBalance!]!
475
+ }
476
+
477
+ # 勘定科目一覧(ページネーション付き)
478
+ type AccountConnection {
479
+ edges: [AccountEdge!]!
480
+ pageInfo: PageInfo!
481
+ }
482
+
483
+ type AccountEdge {
484
+ node: Account!
485
+ cursor: String!
486
+ }
487
+
488
+ # 課税取引マスタ
489
+ type TaxTransaction {
490
+ taxCode: ID!
491
+ taxName: String!
492
+ taxRate: BigDecimal!
493
+ updatedAt: DateTime!
494
+ updatedBy: String
495
+
496
+ # 関連データ
497
+ accounts: [Account!]!
498
+ }
499
+
500
+ # 勘定科目構成マスタ
501
+ type AccountConfig {
502
+ accountCode: ID!
503
+ accountPath: String!
504
+ updatedAt: DateTime!
505
+ updatedBy: String
506
+
507
+ # 関連データ
508
+ account: Account!
509
+ parentAccount: Account
510
+ childAccounts: [Account!]!
511
+ }
512
+
513
+ # 入力型
514
+ input CreateAccountInput {
515
+ accountCode: ID!
516
+ accountName: String!
517
+ accountShortName: String
518
+ accountKana: String
519
+ bsplCategory: BSPLCategory!
520
+ debitCreditCategory: DebitCreditCategory!
521
+ transactionElementCategory: TransactionElementCategory!
522
+ aggregationCategory: AggregationCategory!
523
+ managementAccountingCategory: String
524
+ expenseCategory: String
525
+ ledgerOutputCategory: String
526
+ subAccountType: String
527
+ taxCalculationCategory: String
528
+ taxTransactionCode: ID
529
+ dueDateManagementCategory: String
530
+ }
531
+
532
+ input UpdateAccountInput {
533
+ accountCode: ID!
534
+ accountName: String
535
+ accountShortName: String
536
+ accountKana: String
537
+ bsplCategory: BSPLCategory
538
+ debitCreditCategory: DebitCreditCategory
539
+ transactionElementCategory: TransactionElementCategory
540
+ aggregationCategory: AggregationCategory
541
+ managementAccountingCategory: String
542
+ expenseCategory: String
543
+ ledgerOutputCategory: String
544
+ subAccountType: String
545
+ taxCalculationCategory: String
546
+ taxTransactionCode: ID
547
+ dueDateManagementCategory: String
548
+ }
549
+ ```
550
+
551
+ </details>
552
+
553
+ ### 23.3 仕訳スキーマ
554
+
555
+ <details>
556
+ <summary>src/main/resources/graphql/journal.graphqls</summary>
557
+
558
+ ```graphql
559
+ # 仕訳伝票区分
560
+ enum JournalVoucherType {
561
+ NORMAL # 通常仕訳
562
+ ADJUSTMENT # 調整仕訳
563
+ CLOSING # 決算仕訳
564
+ }
565
+
566
+ # 仕訳
567
+ type Journal {
568
+ journalNumber: ID!
569
+ postingDate: Date!
570
+ entryDate: Date!
571
+ isClosingEntry: Boolean!
572
+ isSingleEntry: Boolean!
573
+ voucherType: JournalVoucherType!
574
+ isRecurring: Boolean!
575
+ employeeCode: String
576
+ departmentCode: String
577
+ isReversal: Boolean!
578
+ reversalJournalNumber: ID
579
+ createdAt: DateTime!
580
+ updatedAt: DateTime!
581
+
582
+ # 関連データ
583
+ details: [JournalDetail!]!
584
+ department: Department
585
+ totalDebitAmount: BigDecimal!
586
+ totalCreditAmount: BigDecimal!
587
+ isBalanced: Boolean!
588
+ }
589
+
590
+ # 仕訳明細
591
+ type JournalDetail {
592
+ journalNumber: ID!
593
+ lineNumber: Int!
594
+ description: String
595
+ createdAt: DateTime!
596
+ updatedAt: DateTime!
597
+
598
+ # 関連データ
599
+ debitCreditDetails: [JournalDebitCreditDetail!]!
600
+ }
601
+
602
+ # 仕訳貸借明細
603
+ type JournalDebitCreditDetail {
604
+ journalNumber: ID!
605
+ lineNumber: Int!
606
+ debitCreditCategory: DebitCreditCategory!
607
+ currencyCode: String
608
+ exchangeRate: BigDecimal
609
+ departmentCode: String
610
+ projectCode: String
611
+ accountCode: ID!
612
+ subAccountCode: String
613
+ amount: BigDecimal!
614
+ baseAmount: BigDecimal!
615
+ taxCategory: String
616
+ taxRate: BigDecimal
617
+ taxCalculationCategory: String
618
+ dueDate: Date
619
+ cashFlowFlag: Boolean
620
+ segmentCode: String
621
+ counterAccountCode: String
622
+ counterSubAccountCode: String
623
+ tagCode: String
624
+ tagDescription: String
625
+ createdAt: DateTime!
626
+ updatedAt: DateTime!
627
+
628
+ # 関連データ
629
+ account: Account!
630
+ counterAccount: Account
631
+ }
632
+
633
+ # 仕訳一覧
634
+ type JournalConnection {
635
+ edges: [JournalEdge!]!
636
+ pageInfo: PageInfo!
637
+ }
638
+
639
+ type JournalEdge {
640
+ node: Journal!
641
+ cursor: String!
642
+ }
643
+
644
+ # 仕訳登録通知イベント
645
+ type JournalNotification {
646
+ journalNumber: ID!
647
+ postingDate: Date!
648
+ totalDebitAmount: BigDecimal!
649
+ totalCreditAmount: BigDecimal!
650
+ createdBy: String
651
+ createdAt: DateTime!
652
+ }
653
+
654
+ # 入力型
655
+ input CreateJournalInput {
656
+ postingDate: Date!
657
+ isClosingEntry: Boolean
658
+ isSingleEntry: Boolean
659
+ voucherType: JournalVoucherType
660
+ isRecurring: Boolean
661
+ employeeCode: String
662
+ departmentCode: String
663
+ details: [JournalDetailInput!]!
664
+ }
665
+
666
+ input JournalDetailInput {
667
+ lineNumber: Int!
668
+ description: String
669
+ debitCreditDetails: [JournalDebitCreditDetailInput!]!
670
+ }
671
+
672
+ input JournalDebitCreditDetailInput {
673
+ debitCreditCategory: DebitCreditCategory!
674
+ currencyCode: String
675
+ exchangeRate: BigDecimal
676
+ departmentCode: String
677
+ projectCode: String
678
+ accountCode: ID!
679
+ subAccountCode: String
680
+ amount: BigDecimal!
681
+ baseAmount: BigDecimal
682
+ taxCategory: String
683
+ taxRate: BigDecimal
684
+ dueDate: Date
685
+ segmentCode: String
686
+ counterAccountCode: String
687
+ tagCode: String
688
+ tagDescription: String
689
+ }
690
+ ```
691
+
692
+ </details>
693
+
694
+ ### 23.4 残高・帳票スキーマ
695
+
696
+ <details>
697
+ <summary>src/main/resources/graphql/balance.graphqls</summary>
698
+
699
+ ```graphql
700
+ # 日次勘定科目残高
701
+ type DailyBalance {
702
+ postingDate: Date!
703
+ accountCode: ID!
704
+ subAccountCode: String
705
+ departmentCode: String
706
+ projectCode: String
707
+ isClosingEntry: Boolean!
708
+ debitAmount: BigDecimal!
709
+ creditAmount: BigDecimal!
710
+ balance: BigDecimal!
711
+
712
+ # 関連データ
713
+ account: Account!
714
+ }
715
+
716
+ # 日次残高一覧
717
+ type DailyBalanceConnection {
718
+ edges: [DailyBalanceEdge!]!
719
+ pageInfo: PageInfo!
720
+ }
721
+
722
+ type DailyBalanceEdge {
723
+ node: DailyBalance!
724
+ cursor: String!
725
+ }
726
+
727
+ # 月次勘定科目残高
728
+ type MonthlyBalance {
729
+ fiscalYear: Int!
730
+ month: Int!
731
+ accountCode: ID!
732
+ subAccountCode: String
733
+ departmentCode: String
734
+ projectCode: String
735
+ isClosingEntry: Boolean!
736
+ beginningBalance: BigDecimal!
737
+ debitAmount: BigDecimal!
738
+ creditAmount: BigDecimal!
739
+ endingBalance: BigDecimal!
740
+
741
+ # 関連データ
742
+ account: Account!
743
+ }
744
+
745
+ # 残高変動イベント
746
+ type BalanceChange {
747
+ accountCode: ID!
748
+ accountName: String!
749
+ changeType: BalanceChangeType!
750
+ previousBalance: BigDecimal!
751
+ currentBalance: BigDecimal!
752
+ changeAmount: BigDecimal!
753
+ timestamp: DateTime!
754
+ }
755
+
756
+ enum BalanceChangeType {
757
+ DEBIT # 借方記入
758
+ CREDIT # 貸方記入
759
+ ADJUSTMENT # 調整
760
+ CLOSING # 決算
761
+ }
762
+
763
+ # 再計算結果
764
+ type RecalculateResult {
765
+ processedCount: Int!
766
+ updatedAccounts: [String!]!
767
+ timestamp: DateTime!
768
+ }
769
+ ```
770
+
771
+ </details>
772
+
773
+ <details>
774
+ <summary>src/main/resources/graphql/report.graphqls</summary>
775
+
776
+ ```graphql
777
+ # 日計表
778
+ type DailyReport {
779
+ reportDate: Date!
780
+ entries: [DailyReportEntry!]!
781
+ totalDebitAmount: BigDecimal!
782
+ totalCreditAmount: BigDecimal!
783
+ generatedAt: DateTime!
784
+ }
785
+
786
+ type DailyReportEntry {
787
+ accountCode: ID!
788
+ accountName: String!
789
+ debitAmount: BigDecimal!
790
+ creditAmount: BigDecimal!
791
+ balance: BigDecimal!
792
+ }
793
+
794
+ # 合計残高試算表
795
+ type TrialBalance {
796
+ fiscalYear: Int!
797
+ month: Int!
798
+ entries: [TrialBalanceEntry!]!
799
+ totalDebitAmount: BigDecimal!
800
+ totalCreditAmount: BigDecimal!
801
+ totalDebitBalance: BigDecimal!
802
+ totalCreditBalance: BigDecimal!
803
+ generatedAt: DateTime!
804
+ }
805
+
806
+ type TrialBalanceEntry {
807
+ accountCode: ID!
808
+ accountName: String!
809
+ bsplCategory: BSPLCategory!
810
+ debitAmount: BigDecimal!
811
+ creditAmount: BigDecimal!
812
+ debitBalance: BigDecimal!
813
+ creditBalance: BigDecimal!
814
+ }
815
+
816
+ # 財務諸表
817
+ type FinancialStatements {
818
+ fiscalYear: Int!
819
+ balanceSheet: BalanceSheet!
820
+ incomeStatement: IncomeStatement!
821
+ generatedAt: DateTime!
822
+ }
823
+
824
+ # 貸借対照表
825
+ type BalanceSheet {
826
+ fiscalYear: Int!
827
+ assetEntries: [FinancialStatementEntry!]!
828
+ liabilityEntries: [FinancialStatementEntry!]!
829
+ capitalEntries: [FinancialStatementEntry!]!
830
+ totalAssets: BigDecimal!
831
+ totalLiabilities: BigDecimal!
832
+ totalCapital: BigDecimal!
833
+ }
834
+
835
+ # 損益計算書
836
+ type IncomeStatement {
837
+ fiscalYear: Int!
838
+ revenueEntries: [FinancialStatementEntry!]!
839
+ expenseEntries: [FinancialStatementEntry!]!
840
+ totalRevenue: BigDecimal!
841
+ totalExpense: BigDecimal!
842
+ netIncome: BigDecimal!
843
+ }
844
+
845
+ type FinancialStatementEntry {
846
+ accountCode: ID!
847
+ accountName: String!
848
+ level: Int!
849
+ amount: BigDecimal!
850
+ isSubtotal: Boolean!
851
+ }
852
+
853
+ # 決算処理結果
854
+ type ClosingResult {
855
+ closingId: ID!
856
+ fiscalYear: Int!
857
+ processedJournalCount: Int!
858
+ closingJournals: [Journal!]!
859
+ completedAt: DateTime!
860
+ }
861
+
862
+ # 決算処理進捗
863
+ type ClosingProgress {
864
+ closingId: ID!
865
+ status: ClosingStatus!
866
+ currentStep: String!
867
+ processedCount: Int!
868
+ totalCount: Int!
869
+ progressPercent: Float!
870
+ updatedAt: DateTime!
871
+ }
872
+
873
+ enum ClosingStatus {
874
+ PROCESSING # 処理中
875
+ COMPLETED # 完了
876
+ FAILED # 失敗
877
+ }
878
+ ```
879
+
880
+ </details>
881
+
882
+ ---
883
+
884
+ ## 第 24 章:マスタ API の実装
885
+
886
+ ### 24.1 Query リゾルバ
887
+
888
+ <details>
889
+ <summary>QueryResolver.java</summary>
890
+
891
+ ```java
892
+ package com.example.accounting.infrastructure.graphql.resolver;
893
+
894
+ import com.example.accounting.application.port.in.AccountUseCase;
895
+ import com.example.accounting.application.port.in.JournalUseCase;
896
+ import com.example.accounting.application.port.in.BalanceUseCase;
897
+ import com.example.accounting.domain.model.account.Account;
898
+ import com.example.accounting.domain.model.account.TaxTransaction;
899
+ import com.example.accounting.domain.model.account.AccountConfig;
900
+ import com.example.accounting.infrastructure.graphql.dto.*;
901
+ import org.springframework.graphql.data.method.annotation.Argument;
902
+ import org.springframework.graphql.data.method.annotation.QueryMapping;
903
+ import org.springframework.stereotype.Controller;
904
+
905
+ import java.util.List;
906
+
907
+ /**
908
+ * GraphQL Query リゾルバ
909
+ */
910
+ @Controller
911
+ public class QueryResolver {
912
+
913
+ private final AccountUseCase accountUseCase;
914
+ private final JournalUseCase journalUseCase;
915
+ private final BalanceUseCase balanceUseCase;
916
+
917
+ public QueryResolver(
918
+ AccountUseCase accountUseCase,
919
+ JournalUseCase journalUseCase,
920
+ BalanceUseCase balanceUseCase) {
921
+ this.accountUseCase = accountUseCase;
922
+ this.journalUseCase = journalUseCase;
923
+ this.balanceUseCase = balanceUseCase;
924
+ }
925
+
926
+ // === 勘定科目 ===
927
+
928
+ @QueryMapping
929
+ public Account account(@Argument String accountCode) {
930
+ return accountUseCase.findByCode(accountCode)
931
+ .orElse(null);
932
+ }
933
+
934
+ @QueryMapping
935
+ public AccountConnection accounts(
936
+ @Argument String bsplCategory,
937
+ @Argument Integer page,
938
+ @Argument Integer size) {
939
+
940
+ int pageNum = page != null ? page : 0;
941
+ int pageSize = size != null ? size : 20;
942
+
943
+ List<Account> accounts = accountUseCase.findAll(bsplCategory, pageNum, pageSize);
944
+ long totalCount = accountUseCase.count(bsplCategory);
945
+
946
+ return AccountConnection.of(accounts, pageNum, pageSize, totalCount);
947
+ }
948
+
949
+ @QueryMapping
950
+ public List<Account> accountHierarchy(@Argument String accountCode) {
951
+ return accountUseCase.findHierarchy(accountCode);
952
+ }
953
+
954
+ // === 課税取引 ===
955
+
956
+ @QueryMapping
957
+ public TaxTransaction taxTransaction(@Argument String taxCode) {
958
+ return accountUseCase.findTaxTransaction(taxCode)
959
+ .orElse(null);
960
+ }
961
+
962
+ @QueryMapping
963
+ public List<TaxTransaction> taxTransactions() {
964
+ return accountUseCase.findAllTaxTransactions();
965
+ }
966
+
967
+ // === 勘定科目構成 ===
968
+
969
+ @QueryMapping
970
+ public AccountConfig accountConfig(@Argument String accountCode) {
971
+ return accountUseCase.findAccountConfig(accountCode)
972
+ .orElse(null);
973
+ }
974
+
975
+ @QueryMapping
976
+ public List<AccountConfig> accountConfigs(@Argument String parentPath) {
977
+ if (parentPath != null) {
978
+ return accountUseCase.findAccountConfigsByPath(parentPath);
979
+ }
980
+ return accountUseCase.findAllAccountConfigs();
981
+ }
982
+ }
983
+ ```
984
+
985
+ </details>
986
+
987
+ ### 24.2 N+1 問題とは
988
+
989
+ GraphQL では、ネストしたデータを取得する際に **N+1 問題** が発生しやすくなります。
990
+
991
+ ```plantuml
992
+ @startuml n_plus_1_problem
993
+
994
+ title N+1 問題の発生パターン
995
+
996
+ participant "GraphQL Client" as client
997
+ participant "AccountResolver" as resolver
998
+ participant "TaxTransactionRepository" as repo
999
+ database "PostgreSQL" as db
1000
+
1001
+ client -> resolver : accounts(first: 10)
1002
+ resolver -> db : SELECT * FROM 勘定科目マスタ LIMIT 10
1003
+ db --> resolver : 10件の勘定科目
1004
+
1005
+ loop 各勘定科目に対して(N回)
1006
+ resolver -> repo : findByCode(taxCode)
1007
+ repo -> db : SELECT * FROM 課税取引マスタ WHERE 課税取引コード = ?
1008
+ db --> repo : 課税取引データ
1009
+ repo --> resolver : 課税取引
1010
+ end
1011
+
1012
+ resolver --> client : 勘定科目一覧(課税取引付き)
1013
+
1014
+ note right of db
1015
+ 合計 11回のクエリ
1016
+ (1 + N = 1 + 10)
1017
+
1018
+ 勘定科目が100件なら101回!
1019
+ end note
1020
+
1021
+ @enduml
1022
+ ```
1023
+
1024
+ ### 24.3 DataLoader による解決
1025
+
1026
+ **DataLoader** は、複数の個別リクエストをバッチ処理にまとめることで N+1 問題を解決します。
1027
+
1028
+ ```plantuml
1029
+ @startuml dataloader_solution
1030
+
1031
+ title DataLoader による N+1 問題の解決
1032
+
1033
+ participant "GraphQL Client" as client
1034
+ participant "AccountResolver" as resolver
1035
+ participant "TaxTransactionDataLoader" as loader
1036
+ participant "TaxTransactionRepository" as repo
1037
+ database "PostgreSQL" as db
1038
+
1039
+ client -> resolver : accounts(first: 10)
1040
+ resolver -> db : SELECT * FROM 勘定科目マスタ LIMIT 10
1041
+ db --> resolver : 10件の勘定科目
1042
+
1043
+ loop 各勘定科目に対して
1044
+ resolver -> loader : load(taxCode)
1045
+ note right of loader : キューに追加
1046
+ end
1047
+
1048
+ loader -> repo : loadMany([code1, code2, ...code10])
1049
+ repo -> db : SELECT * FROM 課税取引マスタ WHERE 課税取引コード IN (?, ?, ..., ?)
1050
+ db --> repo : 課税取引データ
1051
+ repo --> loader : Map<課税取引コード, 課税取引>
1052
+ loader --> resolver : 各勘定科目の課税取引
1053
+
1054
+ resolver --> client : 勘定科目一覧(課税取引付き)
1055
+
1056
+ note right of db
1057
+ 合計 2回のクエリ
1058
+ (1 + 1 = 2)
1059
+
1060
+ 勘定科目が100件でも2回!
1061
+ end note
1062
+
1063
+ @enduml
1064
+ ```
1065
+
1066
+ <details>
1067
+ <summary>TaxTransactionDataLoader.java</summary>
1068
+
1069
+ ```java
1070
+ package com.example.accounting.infrastructure.graphql.dataloader;
1071
+
1072
+ import com.example.accounting.application.port.out.AccountRepository;
1073
+ import com.example.accounting.domain.model.account.TaxTransaction;
1074
+ import org.dataloader.BatchLoaderEnvironment;
1075
+ import org.dataloader.MappedBatchLoader;
1076
+ import org.springframework.stereotype.Component;
1077
+
1078
+ import java.util.Map;
1079
+ import java.util.Set;
1080
+ import java.util.concurrent.CompletableFuture;
1081
+ import java.util.concurrent.CompletionStage;
1082
+ import java.util.function.Function;
1083
+ import java.util.stream.Collectors;
1084
+
1085
+ /**
1086
+ * 課税取引データの DataLoader
1087
+ */
1088
+ @Component
1089
+ public class TaxTransactionDataLoader implements MappedBatchLoader<String, TaxTransaction> {
1090
+
1091
+ private final AccountRepository accountRepository;
1092
+
1093
+ public TaxTransactionDataLoader(AccountRepository accountRepository) {
1094
+ this.accountRepository = accountRepository;
1095
+ }
1096
+
1097
+ @Override
1098
+ public CompletionStage<Map<String, TaxTransaction>> load(
1099
+ Set<String> taxCodes,
1100
+ BatchLoaderEnvironment environment) {
1101
+
1102
+ return CompletableFuture.supplyAsync(() -> {
1103
+ return accountRepository.findTaxTransactionsByCodes(taxCodes).stream()
1104
+ .collect(Collectors.toMap(
1105
+ TaxTransaction::getTaxCode,
1106
+ Function.identity()
1107
+ ));
1108
+ });
1109
+ }
1110
+ }
1111
+ ```
1112
+
1113
+ </details>
1114
+
1115
+ ### 24.4 勘定科目リゾルバの実装
1116
+
1117
+ <details>
1118
+ <summary>AccountResolver.java</summary>
1119
+
1120
+ ```java
1121
+ package com.example.accounting.infrastructure.graphql.resolver;
1122
+
1123
+ import com.example.accounting.application.port.in.AccountUseCase;
1124
+ import com.example.accounting.config.DataLoaderConfig;
1125
+ import com.example.accounting.domain.model.account.Account;
1126
+ import com.example.accounting.domain.model.account.TaxTransaction;
1127
+ import com.example.accounting.domain.model.account.AccountConfig;
1128
+ import com.example.accounting.domain.model.balance.DailyBalance;
1129
+ import com.example.accounting.infrastructure.graphql.dto.*;
1130
+ import graphql.schema.DataFetchingEnvironment;
1131
+ import org.dataloader.DataLoader;
1132
+ import org.springframework.graphql.data.method.annotation.Argument;
1133
+ import org.springframework.graphql.data.method.annotation.MutationMapping;
1134
+ import org.springframework.graphql.data.method.annotation.QueryMapping;
1135
+ import org.springframework.graphql.data.method.annotation.SchemaMapping;
1136
+ import org.springframework.stereotype.Controller;
1137
+
1138
+ import java.util.List;
1139
+ import java.util.concurrent.CompletableFuture;
1140
+
1141
+ /**
1142
+ * 勘定科目 GraphQL リゾルバ
1143
+ */
1144
+ @Controller
1145
+ public class AccountResolver {
1146
+
1147
+ private final AccountUseCase accountUseCase;
1148
+
1149
+ public AccountResolver(AccountUseCase accountUseCase) {
1150
+ this.accountUseCase = accountUseCase;
1151
+ }
1152
+
1153
+ // === Query ===
1154
+
1155
+ @QueryMapping
1156
+ public Account account(@Argument String accountCode) {
1157
+ return accountUseCase.findByCode(accountCode).orElse(null);
1158
+ }
1159
+
1160
+ @QueryMapping
1161
+ public AccountConnection accounts(
1162
+ @Argument String bsplCategory,
1163
+ @Argument Integer page,
1164
+ @Argument Integer size) {
1165
+
1166
+ int pageNum = page != null ? page : 0;
1167
+ int pageSize = size != null ? size : 20;
1168
+
1169
+ List<Account> accounts = accountUseCase.findAll(bsplCategory, pageNum, pageSize);
1170
+ long totalCount = accountUseCase.count(bsplCategory);
1171
+
1172
+ return AccountConnection.of(accounts, pageNum, pageSize, totalCount);
1173
+ }
1174
+
1175
+ // === Mutation ===
1176
+
1177
+ @MutationMapping
1178
+ public Account createAccount(@Argument CreateAccountInput input) {
1179
+ Account account = Account.builder()
1180
+ .accountCode(input.accountCode())
1181
+ .accountName(input.accountName())
1182
+ .accountShortName(input.accountShortName())
1183
+ .bsplCategory(input.bsplCategory())
1184
+ .debitCreditCategory(input.debitCreditCategory())
1185
+ .transactionElementCategory(input.transactionElementCategory())
1186
+ .aggregationCategory(input.aggregationCategory())
1187
+ .taxTransactionCode(input.taxTransactionCode())
1188
+ .build();
1189
+
1190
+ return accountUseCase.create(account);
1191
+ }
1192
+
1193
+ @MutationMapping
1194
+ public boolean deleteAccount(@Argument String accountCode) {
1195
+ return accountUseCase.delete(accountCode);
1196
+ }
1197
+
1198
+ // === フィールドリゾルバ(DataLoader 使用)===
1199
+
1200
+ @SchemaMapping(typeName = "Account", field = "taxTransaction")
1201
+ public CompletableFuture<TaxTransaction> taxTransaction(
1202
+ Account account,
1203
+ DataFetchingEnvironment env) {
1204
+
1205
+ if (account.getTaxTransactionCode() == null) {
1206
+ return CompletableFuture.completedFuture(null);
1207
+ }
1208
+
1209
+ DataLoader<String, TaxTransaction> loader =
1210
+ env.getDataLoader(DataLoaderConfig.TAX_TRANSACTION_LOADER);
1211
+
1212
+ return loader.load(account.getTaxTransactionCode());
1213
+ }
1214
+
1215
+ @SchemaMapping(typeName = "Account", field = "accountConfig")
1216
+ public CompletableFuture<AccountConfig> accountConfig(
1217
+ Account account,
1218
+ DataFetchingEnvironment env) {
1219
+
1220
+ DataLoader<String, AccountConfig> loader =
1221
+ env.getDataLoader(DataLoaderConfig.ACCOUNT_CONFIG_LOADER);
1222
+
1223
+ return loader.load(account.getAccountCode());
1224
+ }
1225
+
1226
+ @SchemaMapping(typeName = "Account", field = "childAccounts")
1227
+ public List<Account> childAccounts(Account account) {
1228
+ return accountUseCase.findChildAccounts(account.getAccountCode());
1229
+ }
1230
+ }
1231
+ ```
1232
+
1233
+ </details>
1234
+
1235
+ ---
1236
+
1237
+ ## 第 25 章:トランザクション API の実装
1238
+
1239
+ ### 25.1 仕訳リゾルバの実装
1240
+
1241
+ <details>
1242
+ <summary>JournalResolver.java</summary>
1243
+
1244
+ ```java
1245
+ package com.example.accounting.infrastructure.graphql.resolver;
1246
+
1247
+ import com.example.accounting.application.port.in.JournalUseCase;
1248
+ import com.example.accounting.config.DataLoaderConfig;
1249
+ import com.example.accounting.domain.model.journal.Journal;
1250
+ import com.example.accounting.domain.model.journal.JournalDetail;
1251
+ import com.example.accounting.domain.model.journal.JournalDebitCreditDetail;
1252
+ import com.example.accounting.domain.model.account.Account;
1253
+ import com.example.accounting.infrastructure.graphql.dto.*;
1254
+ import graphql.schema.DataFetchingEnvironment;
1255
+ import org.dataloader.DataLoader;
1256
+ import org.springframework.graphql.data.method.annotation.Argument;
1257
+ import org.springframework.graphql.data.method.annotation.MutationMapping;
1258
+ import org.springframework.graphql.data.method.annotation.QueryMapping;
1259
+ import org.springframework.graphql.data.method.annotation.SchemaMapping;
1260
+ import org.springframework.stereotype.Controller;
1261
+
1262
+ import java.math.BigDecimal;
1263
+ import java.time.LocalDate;
1264
+ import java.util.List;
1265
+ import java.util.concurrent.CompletableFuture;
1266
+
1267
+ /**
1268
+ * 仕訳 GraphQL リゾルバ
1269
+ */
1270
+ @Controller
1271
+ public class JournalResolver {
1272
+
1273
+ private final JournalUseCase journalUseCase;
1274
+
1275
+ public JournalResolver(JournalUseCase journalUseCase) {
1276
+ this.journalUseCase = journalUseCase;
1277
+ }
1278
+
1279
+ // === Query ===
1280
+
1281
+ @QueryMapping
1282
+ public Journal journal(@Argument String journalNumber) {
1283
+ return journalUseCase.findByNumber(journalNumber).orElse(null);
1284
+ }
1285
+
1286
+ @QueryMapping
1287
+ public JournalConnection journals(
1288
+ @Argument LocalDate fromDate,
1289
+ @Argument LocalDate toDate,
1290
+ @Argument String accountCode,
1291
+ @Argument Integer page,
1292
+ @Argument Integer size) {
1293
+
1294
+ int pageNum = page != null ? page : 0;
1295
+ int pageSize = size != null ? size : 20;
1296
+
1297
+ List<Journal> journals = journalUseCase.findAll(
1298
+ fromDate, toDate, accountCode, pageNum, pageSize);
1299
+ long totalCount = journalUseCase.count(fromDate, toDate, accountCode);
1300
+
1301
+ return JournalConnection.of(journals, pageNum, pageSize, totalCount);
1302
+ }
1303
+
1304
+ // === Mutation ===
1305
+
1306
+ @MutationMapping
1307
+ public Journal createJournal(@Argument CreateJournalInput input) {
1308
+ Journal journal = Journal.builder()
1309
+ .postingDate(input.postingDate())
1310
+ .entryDate(LocalDate.now())
1311
+ .isClosingEntry(input.isClosingEntry() != null ? input.isClosingEntry() : false)
1312
+ .voucherType(input.voucherType())
1313
+ .employeeCode(input.employeeCode())
1314
+ .departmentCode(input.departmentCode())
1315
+ .details(convertToDetails(input.details()))
1316
+ .build();
1317
+
1318
+ // 貸借バランスチェック
1319
+ validateBalance(journal);
1320
+
1321
+ return journalUseCase.create(journal);
1322
+ }
1323
+
1324
+ @MutationMapping
1325
+ public boolean deleteJournal(@Argument String journalNumber) {
1326
+ return journalUseCase.delete(journalNumber);
1327
+ }
1328
+
1329
+ @MutationMapping
1330
+ public Journal reverseJournal(
1331
+ @Argument String journalNumber,
1332
+ @Argument String reason) {
1333
+ return journalUseCase.reverse(journalNumber, reason);
1334
+ }
1335
+
1336
+ // === フィールドリゾルバ ===
1337
+
1338
+ @SchemaMapping(typeName = "Journal", field = "totalDebitAmount")
1339
+ public BigDecimal totalDebitAmount(Journal journal) {
1340
+ return journal.getDetails().stream()
1341
+ .flatMap(d -> d.getDebitCreditDetails().stream())
1342
+ .filter(dc -> dc.getDebitCreditCategory().isDebit())
1343
+ .map(JournalDebitCreditDetail::getAmount)
1344
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
1345
+ }
1346
+
1347
+ @SchemaMapping(typeName = "Journal", field = "totalCreditAmount")
1348
+ public BigDecimal totalCreditAmount(Journal journal) {
1349
+ return journal.getDetails().stream()
1350
+ .flatMap(d -> d.getDebitCreditDetails().stream())
1351
+ .filter(dc -> dc.getDebitCreditCategory().isCredit())
1352
+ .map(JournalDebitCreditDetail::getAmount)
1353
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
1354
+ }
1355
+
1356
+ @SchemaMapping(typeName = "Journal", field = "isBalanced")
1357
+ public boolean isBalanced(Journal journal) {
1358
+ BigDecimal debit = totalDebitAmount(journal);
1359
+ BigDecimal credit = totalCreditAmount(journal);
1360
+ return debit.compareTo(credit) == 0;
1361
+ }
1362
+
1363
+ @SchemaMapping(typeName = "JournalDebitCreditDetail", field = "account")
1364
+ public CompletableFuture<Account> account(
1365
+ JournalDebitCreditDetail detail,
1366
+ DataFetchingEnvironment env) {
1367
+
1368
+ DataLoader<String, Account> loader =
1369
+ env.getDataLoader(DataLoaderConfig.ACCOUNT_LOADER);
1370
+
1371
+ return loader.load(detail.getAccountCode());
1372
+ }
1373
+
1374
+ // === Private Methods ===
1375
+
1376
+ private void validateBalance(Journal journal) {
1377
+ BigDecimal debit = journal.getDetails().stream()
1378
+ .flatMap(d -> d.getDebitCreditDetails().stream())
1379
+ .filter(dc -> dc.getDebitCreditCategory().isDebit())
1380
+ .map(JournalDebitCreditDetail::getAmount)
1381
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
1382
+
1383
+ BigDecimal credit = journal.getDetails().stream()
1384
+ .flatMap(d -> d.getDebitCreditDetails().stream())
1385
+ .filter(dc -> dc.getDebitCreditCategory().isCredit())
1386
+ .map(JournalDebitCreditDetail::getAmount)
1387
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
1388
+
1389
+ if (debit.compareTo(credit) != 0) {
1390
+ throw new IllegalArgumentException(
1391
+ "貸借が一致しません。借方合計: " + debit + ", 貸方合計: " + credit);
1392
+ }
1393
+ }
1394
+ }
1395
+ ```
1396
+
1397
+ </details>
1398
+
1399
+ ### 25.2 GraphQL Subscription の実装
1400
+
1401
+ GraphQL **Subscription** は、サーバーからクライアントへのリアルタイム通知を実現する仕組みです。
1402
+
1403
+ ```plantuml
1404
+ @startuml subscription_flow
1405
+
1406
+ title GraphQL Subscription フロー
1407
+
1408
+ participant "Client" as client
1409
+ participant "WebSocket" as ws
1410
+ participant "GraphQL Server" as server
1411
+ participant "EventPublisher" as publisher
1412
+ participant "JournalService" as service
1413
+
1414
+ == Subscription 開始 ==
1415
+ client -> ws : subscription { balanceChanged }
1416
+ ws -> server : WebSocket 接続確立
1417
+ server -> publisher : 購読登録
1418
+
1419
+ == 仕訳登録時 ==
1420
+ service -> publisher : 仕訳登録イベント発行
1421
+ publisher -> server : イベント受信
1422
+ server -> ws : BalanceChange データ送信
1423
+ ws -> client : リアルタイム通知
1424
+
1425
+ == Subscription 終了 ==
1426
+ client -> ws : 接続終了
1427
+ ws -> server : WebSocket 切断
1428
+ server -> publisher : 購読解除
1429
+
1430
+ @enduml
1431
+ ```
1432
+
1433
+ <details>
1434
+ <summary>SubscriptionResolver.java</summary>
1435
+
1436
+ ```java
1437
+ package com.example.accounting.infrastructure.graphql.subscription;
1438
+
1439
+ import com.example.accounting.domain.event.BalanceChangeEvent;
1440
+ import com.example.accounting.domain.event.JournalCreatedEvent;
1441
+ import com.example.accounting.domain.event.ClosingProgressEvent;
1442
+ import com.example.accounting.infrastructure.graphql.dto.*;
1443
+ import org.springframework.graphql.data.method.annotation.Argument;
1444
+ import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
1445
+ import org.springframework.stereotype.Controller;
1446
+ import reactor.core.publisher.Flux;
1447
+
1448
+ /**
1449
+ * GraphQL Subscription リゾルバ
1450
+ */
1451
+ @Controller
1452
+ public class SubscriptionResolver {
1453
+
1454
+ private final BalanceEventPublisher balanceEventPublisher;
1455
+ private final JournalEventPublisher journalEventPublisher;
1456
+ private final ClosingEventPublisher closingEventPublisher;
1457
+
1458
+ public SubscriptionResolver(
1459
+ BalanceEventPublisher balanceEventPublisher,
1460
+ JournalEventPublisher journalEventPublisher,
1461
+ ClosingEventPublisher closingEventPublisher) {
1462
+ this.balanceEventPublisher = balanceEventPublisher;
1463
+ this.journalEventPublisher = journalEventPublisher;
1464
+ this.closingEventPublisher = closingEventPublisher;
1465
+ }
1466
+
1467
+ @SubscriptionMapping
1468
+ public Flux<BalanceChange> balanceChanged(@Argument String accountCode) {
1469
+ Flux<BalanceChangeEvent> eventFlux = balanceEventPublisher.getEventFlux();
1470
+
1471
+ if (accountCode != null) {
1472
+ eventFlux = eventFlux.filter(event ->
1473
+ accountCode.equals(event.getAccountCode()));
1474
+ }
1475
+
1476
+ return eventFlux.map(this::toBalanceChange);
1477
+ }
1478
+
1479
+ @SubscriptionMapping
1480
+ public Flux<JournalNotification> journalCreated() {
1481
+ return journalEventPublisher.getEventFlux()
1482
+ .map(this::toJournalNotification);
1483
+ }
1484
+
1485
+ @SubscriptionMapping
1486
+ public Flux<ClosingProgress> closingProgressUpdated(@Argument String closingId) {
1487
+ return closingEventPublisher.getEventFlux()
1488
+ .filter(event -> closingId.equals(event.getClosingId()))
1489
+ .map(this::toClosingProgress);
1490
+ }
1491
+
1492
+ private BalanceChange toBalanceChange(BalanceChangeEvent event) {
1493
+ return new BalanceChange(
1494
+ event.getAccountCode(),
1495
+ event.getAccountName(),
1496
+ event.getChangeType(),
1497
+ event.getPreviousBalance(),
1498
+ event.getCurrentBalance(),
1499
+ event.getChangeAmount(),
1500
+ event.getTimestamp()
1501
+ );
1502
+ }
1503
+
1504
+ private JournalNotification toJournalNotification(JournalCreatedEvent event) {
1505
+ return new JournalNotification(
1506
+ event.getJournalNumber(),
1507
+ event.getPostingDate(),
1508
+ event.getTotalDebitAmount(),
1509
+ event.getTotalCreditAmount(),
1510
+ event.getCreatedBy(),
1511
+ event.getCreatedAt()
1512
+ );
1513
+ }
1514
+
1515
+ private ClosingProgress toClosingProgress(ClosingProgressEvent event) {
1516
+ return new ClosingProgress(
1517
+ event.getClosingId(),
1518
+ event.getStatus(),
1519
+ event.getCurrentStep(),
1520
+ event.getProcessedCount(),
1521
+ event.getTotalCount(),
1522
+ event.getProgressPercent(),
1523
+ event.getUpdatedAt()
1524
+ );
1525
+ }
1526
+ }
1527
+ ```
1528
+
1529
+ </details>
1530
+
1531
+ <details>
1532
+ <summary>BalanceEventPublisher.java</summary>
1533
+
1534
+ ```java
1535
+ package com.example.accounting.infrastructure.graphql.subscription;
1536
+
1537
+ import com.example.accounting.domain.event.BalanceChangeEvent;
1538
+ import org.springframework.stereotype.Component;
1539
+ import reactor.core.publisher.Flux;
1540
+ import reactor.core.publisher.Sinks;
1541
+
1542
+ /**
1543
+ * 残高変動イベントパブリッシャー
1544
+ */
1545
+ @Component
1546
+ public class BalanceEventPublisher {
1547
+
1548
+ private final Sinks.Many<BalanceChangeEvent> sink;
1549
+
1550
+ public BalanceEventPublisher() {
1551
+ this.sink = Sinks.many().multicast().onBackpressureBuffer();
1552
+ }
1553
+
1554
+ public void publish(BalanceChangeEvent event) {
1555
+ sink.tryEmitNext(event);
1556
+ }
1557
+
1558
+ public Flux<BalanceChangeEvent> getEventFlux() {
1559
+ return sink.asFlux();
1560
+ }
1561
+ }
1562
+ ```
1563
+
1564
+ </details>
1565
+
1566
+ ---
1567
+
1568
+ ## 第 26 章:エラーハンドリングとバリデーション
1569
+
1570
+ ### 26.1 GraphQL エラー設計
1571
+
1572
+ GraphQL では、エラーは `errors` フィールドとして返されます。
1573
+
1574
+ ```plantuml
1575
+ @startuml graphql_error_handling
1576
+
1577
+ title GraphQL エラーハンドリング
1578
+
1579
+ package "GraphQL Layer" {
1580
+ [GraphQL Error Handler]
1581
+ }
1582
+
1583
+ package "Application Layer" {
1584
+ [Use Case Exception]
1585
+ }
1586
+
1587
+ package "Domain Layer" {
1588
+ [Domain Exception]
1589
+ }
1590
+
1591
+ [Domain Exception] --> [Use Case Exception] : 伝播
1592
+ [Use Case Exception] --> [GraphQL Error Handler] : 伝播
1593
+
1594
+ note right of [Domain Exception]
1595
+ ビジネスルール違反
1596
+ - 貸借不一致
1597
+ - 科目未登録
1598
+ - 決算済み期間
1599
+ end note
1600
+
1601
+ note right of [Use Case Exception]
1602
+ ユースケース固有
1603
+ - リソース未発見
1604
+ - バリデーションエラー
1605
+ - 権限不足
1606
+ end note
1607
+
1608
+ note right of [GraphQL Error Handler]
1609
+ GraphQL エラー形式に変換
1610
+ - エラーコード
1611
+ - エラーメッセージ
1612
+ - エラー詳細
1613
+ end note
1614
+
1615
+ @enduml
1616
+ ```
1617
+
1618
+ ### 26.2 カスタム例外クラス
1619
+
1620
+ <details>
1621
+ <summary>AccountingException.java(基底クラス)</summary>
1622
+
1623
+ ```java
1624
+ package com.example.accounting.domain.exception;
1625
+
1626
+ /**
1627
+ * 財務会計ドメイン例外の基底クラス
1628
+ */
1629
+ public abstract class AccountingException extends RuntimeException {
1630
+
1631
+ private final String errorCode;
1632
+
1633
+ protected AccountingException(String errorCode, String message) {
1634
+ super(message);
1635
+ this.errorCode = errorCode;
1636
+ }
1637
+
1638
+ protected AccountingException(String errorCode, String message, Throwable cause) {
1639
+ super(message, cause);
1640
+ this.errorCode = errorCode;
1641
+ }
1642
+
1643
+ public String getErrorCode() {
1644
+ return errorCode;
1645
+ }
1646
+ }
1647
+ ```
1648
+
1649
+ </details>
1650
+
1651
+ <details>
1652
+ <summary>BalanceNotMatchException.java</summary>
1653
+
1654
+ ```java
1655
+ package com.example.accounting.domain.exception;
1656
+
1657
+ import java.math.BigDecimal;
1658
+
1659
+ /**
1660
+ * 貸借不一致例外
1661
+ */
1662
+ public class BalanceNotMatchException extends AccountingException {
1663
+
1664
+ private final BigDecimal debitTotal;
1665
+ private final BigDecimal creditTotal;
1666
+
1667
+ public BalanceNotMatchException(BigDecimal debitTotal, BigDecimal creditTotal) {
1668
+ super("BALANCE_NOT_MATCH",
1669
+ String.format("貸借が一致しません。借方合計: %s, 貸方合計: %s", debitTotal, creditTotal));
1670
+ this.debitTotal = debitTotal;
1671
+ this.creditTotal = creditTotal;
1672
+ }
1673
+
1674
+ public BigDecimal getDebitTotal() {
1675
+ return debitTotal;
1676
+ }
1677
+
1678
+ public BigDecimal getCreditTotal() {
1679
+ return creditTotal;
1680
+ }
1681
+ }
1682
+ ```
1683
+
1684
+ </details>
1685
+
1686
+ ### 26.3 GraphQL 例外ハンドラー
1687
+
1688
+ <details>
1689
+ <summary>GraphQLExceptionHandler.java</summary>
1690
+
1691
+ ```java
1692
+ package com.example.accounting.infrastructure.graphql.exception;
1693
+
1694
+ import com.example.accounting.domain.exception.*;
1695
+ import graphql.GraphQLError;
1696
+ import graphql.GraphqlErrorBuilder;
1697
+ import graphql.schema.DataFetchingEnvironment;
1698
+ import org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter;
1699
+ import org.springframework.graphql.execution.ErrorType;
1700
+ import org.springframework.stereotype.Component;
1701
+
1702
+ import java.util.HashMap;
1703
+ import java.util.Map;
1704
+
1705
+ /**
1706
+ * GraphQL 例外ハンドラー
1707
+ */
1708
+ @Component
1709
+ public class GraphQLExceptionHandler extends DataFetcherExceptionResolverAdapter {
1710
+
1711
+ @Override
1712
+ protected GraphQLError resolveToSingleError(Throwable ex, DataFetchingEnvironment env) {
1713
+ if (ex instanceof AccountingException accountingEx) {
1714
+ return handleAccountingException(accountingEx, env);
1715
+ }
1716
+
1717
+ if (ex instanceof IllegalArgumentException) {
1718
+ return GraphqlErrorBuilder.newError(env)
1719
+ .errorType(ErrorType.BAD_REQUEST)
1720
+ .message(ex.getMessage())
1721
+ .build();
1722
+ }
1723
+
1724
+ // その他の例外
1725
+ return GraphqlErrorBuilder.newError(env)
1726
+ .errorType(ErrorType.INTERNAL_ERROR)
1727
+ .message("内部エラーが発生しました")
1728
+ .build();
1729
+ }
1730
+
1731
+ private GraphQLError handleAccountingException(
1732
+ AccountingException ex, DataFetchingEnvironment env) {
1733
+
1734
+ Map<String, Object> extensions = new HashMap<>();
1735
+ extensions.put("errorCode", ex.getErrorCode());
1736
+
1737
+ ErrorType errorType = mapToErrorType(ex);
1738
+
1739
+ // 例外固有の情報を追加
1740
+ if (ex instanceof BalanceNotMatchException balanceEx) {
1741
+ extensions.put("debitTotal", balanceEx.getDebitTotal());
1742
+ extensions.put("creditTotal", balanceEx.getCreditTotal());
1743
+ } else if (ex instanceof AccountNotFoundException accountEx) {
1744
+ extensions.put("accountCode", accountEx.getAccountCode());
1745
+ }
1746
+
1747
+ return GraphqlErrorBuilder.newError(env)
1748
+ .errorType(errorType)
1749
+ .message(ex.getMessage())
1750
+ .extensions(extensions)
1751
+ .build();
1752
+ }
1753
+
1754
+ private ErrorType mapToErrorType(AccountingException ex) {
1755
+ if (ex instanceof AccountNotFoundException ||
1756
+ ex instanceof JournalNotFoundException) {
1757
+ return ErrorType.NOT_FOUND;
1758
+ }
1759
+
1760
+ if (ex instanceof BalanceNotMatchException ||
1761
+ ex instanceof ClosedPeriodException) {
1762
+ return ErrorType.BAD_REQUEST;
1763
+ }
1764
+
1765
+ return ErrorType.INTERNAL_ERROR;
1766
+ }
1767
+ }
1768
+ ```
1769
+
1770
+ </details>
1771
+
1772
+ ### 26.4 Spring for GraphQL テスト
1773
+
1774
+ <details>
1775
+ <summary>AccountResolverTest.java</summary>
1776
+
1777
+ ```java
1778
+ package com.example.accounting.infrastructure.graphql;
1779
+
1780
+ import org.junit.jupiter.api.Test;
1781
+ import org.springframework.beans.factory.annotation.Autowired;
1782
+ import org.springframework.boot.test.autoconfigure.graphql.tester.AutoConfigureHttpGraphQlTester;
1783
+ import org.springframework.boot.test.context.SpringBootTest;
1784
+ import org.springframework.graphql.test.tester.HttpGraphQlTester;
1785
+ import org.springframework.test.context.DynamicPropertyRegistry;
1786
+ import org.springframework.test.context.DynamicPropertySource;
1787
+ import org.testcontainers.containers.PostgreSQLContainer;
1788
+ import org.testcontainers.junit.jupiter.Container;
1789
+ import org.testcontainers.junit.jupiter.Testcontainers;
1790
+
1791
+ import static org.assertj.core.api.Assertions.assertThat;
1792
+
1793
+ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
1794
+ @AutoConfigureHttpGraphQlTester
1795
+ @Testcontainers
1796
+ class AccountResolverTest {
1797
+
1798
+ @Container
1799
+ static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine");
1800
+
1801
+ @DynamicPropertySource
1802
+ static void configureProperties(DynamicPropertyRegistry registry) {
1803
+ registry.add("spring.datasource.url", postgres::getJdbcUrl);
1804
+ registry.add("spring.datasource.username", postgres::getUsername);
1805
+ registry.add("spring.datasource.password", postgres::getPassword);
1806
+ }
1807
+
1808
+ @Autowired
1809
+ private HttpGraphQlTester graphQlTester;
1810
+
1811
+ @Test
1812
+ void testQueryAccount() {
1813
+ // language=GraphQL
1814
+ String query = """
1815
+ query {
1816
+ account(accountCode: "11110") {
1817
+ accountCode
1818
+ accountName
1819
+ bsplCategory
1820
+ debitCreditCategory
1821
+ }
1822
+ }
1823
+ """;
1824
+
1825
+ graphQlTester.document(query)
1826
+ .execute()
1827
+ .path("account.accountCode")
1828
+ .entity(String.class)
1829
+ .isEqualTo("11110");
1830
+ }
1831
+
1832
+ @Test
1833
+ void testMutationCreateAccount() {
1834
+ // language=GraphQL
1835
+ String mutation = """
1836
+ mutation {
1837
+ createAccount(input: {
1838
+ accountCode: "99999"
1839
+ accountName: "テスト科目"
1840
+ bsplCategory: BS
1841
+ debitCreditCategory: DEBIT
1842
+ transactionElementCategory: ASSET
1843
+ aggregationCategory: POSTING
1844
+ }) {
1845
+ accountCode
1846
+ accountName
1847
+ }
1848
+ }
1849
+ """;
1850
+
1851
+ graphQlTester.document(mutation)
1852
+ .execute()
1853
+ .path("createAccount.accountCode")
1854
+ .entity(String.class)
1855
+ .isEqualTo("99999");
1856
+ }
1857
+ }
1858
+ ```
1859
+
1860
+ </details>
1861
+
1862
+ ### 26.5 GraphiQL による動作確認
1863
+
1864
+ サーバー起動後、`http://localhost:8080/graphiql` にアクセスして対話的にクエリを実行できます。
1865
+
1866
+ ```graphql
1867
+ # 勘定科目の取得
1868
+ query {
1869
+ account(accountCode: "11110") {
1870
+ accountCode
1871
+ accountName
1872
+ bsplCategory
1873
+ debitCreditCategory
1874
+ taxTransaction {
1875
+ taxCode
1876
+ taxName
1877
+ taxRate
1878
+ }
1879
+ dailyBalances {
1880
+ postingDate
1881
+ debitAmount
1882
+ creditAmount
1883
+ balance
1884
+ }
1885
+ }
1886
+ }
1887
+
1888
+ # 仕訳登録
1889
+ mutation {
1890
+ createJournal(input: {
1891
+ postingDate: "2025-12-29"
1892
+ voucherType: NORMAL
1893
+ details: [
1894
+ {
1895
+ lineNumber: 1
1896
+ description: "現金売上"
1897
+ debitCreditDetails: [
1898
+ {
1899
+ debitCreditCategory: DEBIT
1900
+ accountCode: "11110"
1901
+ amount: "100000"
1902
+ }
1903
+ {
1904
+ debitCreditCategory: CREDIT
1905
+ accountCode: "41100"
1906
+ amount: "100000"
1907
+ }
1908
+ ]
1909
+ }
1910
+ ]
1911
+ }) {
1912
+ journalNumber
1913
+ postingDate
1914
+ totalDebitAmount
1915
+ totalCreditAmount
1916
+ isBalanced
1917
+ }
1918
+ }
1919
+
1920
+ # 試算表
1921
+ query {
1922
+ trialBalance(fiscalYear: 2025, month: 12) {
1923
+ fiscalYear
1924
+ month
1925
+ entries {
1926
+ accountCode
1927
+ accountName
1928
+ bsplCategory
1929
+ debitAmount
1930
+ creditAmount
1931
+ debitBalance
1932
+ creditBalance
1933
+ }
1934
+ totalDebitAmount
1935
+ totalCreditAmount
1936
+ }
1937
+ }
1938
+ ```
1939
+
1940
+ ---
1941
+
1942
+ ## 第 27 章:まとめと発展
1943
+
1944
+ ### 27.1 GraphQL 実装の振り返り
1945
+
1946
+ ```plantuml
1947
+ @startuml graphql_summary
1948
+
1949
+ title 財務会計システム GraphQL 実装全体像
1950
+
1951
+ package "GraphQL Schema" {
1952
+ [schema.graphqls]
1953
+ [account.graphqls]
1954
+ [journal.graphqls]
1955
+ [balance.graphqls]
1956
+ [report.graphqls]
1957
+ }
1958
+
1959
+ package "Resolvers" {
1960
+ [QueryResolver]
1961
+ [AccountResolver]
1962
+ [JournalResolver]
1963
+ [BalanceResolver]
1964
+ [ReportResolver]
1965
+ [SubscriptionResolver]
1966
+ }
1967
+
1968
+ package "DataLoaders" {
1969
+ [TaxTransactionDataLoader]
1970
+ [DailyBalanceDataLoader]
1971
+ [MonthlyBalanceDataLoader]
1972
+ [AccountConfigDataLoader]
1973
+ }
1974
+
1975
+ package "Application Layer" {
1976
+ [AccountUseCase]
1977
+ [JournalUseCase]
1978
+ [BalanceUseCase]
1979
+ [ReportUseCase]
1980
+ }
1981
+
1982
+ package "Infrastructure" {
1983
+ [MyBatis Repository]
1984
+ [Event Publisher]
1985
+ [PostgreSQL]
1986
+ }
1987
+
1988
+ [schema.graphqls] --> [QueryResolver]
1989
+ [QueryResolver] --> [AccountUseCase]
1990
+ [AccountResolver] --> [TaxTransactionDataLoader]
1991
+ [AccountResolver] --> [DailyBalanceDataLoader]
1992
+ [JournalResolver] --> [JournalUseCase]
1993
+ [SubscriptionResolver] --> [Event Publisher]
1994
+
1995
+ [AccountUseCase] --> [MyBatis Repository]
1996
+ [MyBatis Repository] --> [PostgreSQL]
1997
+
1998
+ @enduml
1999
+ ```
2000
+
2001
+ ### 27.2 REST / gRPC / GraphQL の使い分け
2002
+
2003
+ | 観点 | REST API | gRPC | GraphQL |
2004
+ |------|----------|------|---------|
2005
+ | **クライアント** | Web ブラウザ、外部連携 | マイクロサービス間 | SPA、モバイルアプリ |
2006
+ | **データ量** | 固定レスポンス | 固定レスポンス | クライアント指定 |
2007
+ | **リアルタイム** | WebSocket 別実装 | ストリーミング | Subscription |
2008
+ | **型安全性** | △(OpenAPI) | ◎(.proto) | ◎(.graphqls) |
2009
+ | **学習コスト** | 低 | 中 | 中 |
2010
+ | **ブラウザ対応** | ◎ | △(gRPC-Web) | ◎ |
2011
+
2012
+ ```plantuml
2013
+ @startuml api_usage
2014
+
2015
+ title API 使い分けパターン
2016
+
2017
+ package "フロントエンド" {
2018
+ [経理担当者 Web UI] as web
2019
+ [経営ダッシュボード] as dashboard
2020
+ [Excel アドイン] as excel
2021
+ }
2022
+
2023
+ package "バックエンド" {
2024
+ [財務会計システム] as accounting
2025
+ }
2026
+
2027
+ package "連携システム" {
2028
+ [販売管理システム] as sales
2029
+ [購買管理システム] as purchase
2030
+ [人事給与システム] as hr
2031
+ }
2032
+
2033
+ web --> accounting : GraphQL\n(柔軟なクエリ)
2034
+ dashboard --> accounting : GraphQL\n(リアルタイム更新)
2035
+ excel --> accounting : REST API\n(シンプルな連携)
2036
+
2037
+ sales --> accounting : gRPC\n(高性能・自動仕訳)
2038
+ purchase --> accounting : gRPC\n(高性能・自動仕訳)
2039
+ hr --> accounting : gRPC\n(給与仕訳連携)
2040
+
2041
+ @enduml
2042
+ ```
2043
+
2044
+ ### 27.3 ヘキサゴナルアーキテクチャの利点
2045
+
2046
+ 複数の API 形式を同時提供できるのは、ヘキサゴナルアーキテクチャの恩恵です。
2047
+
2048
+ ```plantuml
2049
+ @startuml hexagonal_benefit
2050
+
2051
+ title 複数 API の同時提供
2052
+
2053
+ package "Input Adapters" {
2054
+ [REST Controller]
2055
+ [gRPC Service]
2056
+ [GraphQL Resolver]
2057
+ }
2058
+
2059
+ package "Application Core" {
2060
+ [AccountUseCase]
2061
+ [JournalUseCase]
2062
+ [BalanceUseCase]
2063
+ }
2064
+
2065
+ package "Output Adapters" {
2066
+ [MyBatis Repository]
2067
+ }
2068
+
2069
+ [REST Controller] --> [AccountUseCase]
2070
+ [gRPC Service] --> [AccountUseCase]
2071
+ [GraphQL Resolver] --> [AccountUseCase]
2072
+
2073
+ [REST Controller] --> [JournalUseCase]
2074
+ [gRPC Service] --> [JournalUseCase]
2075
+ [GraphQL Resolver] --> [JournalUseCase]
2076
+
2077
+ note top of [Application Core]
2078
+ ビジネスロジックは
2079
+ API 形式に依存しない
2080
+
2081
+ 1つのコードベースで
2082
+ 複数の API を提供
2083
+ end note
2084
+
2085
+ @enduml
2086
+ ```
2087
+
2088
+ ### 27.4 今後の発展
2089
+
2090
+ 本パートで構築した GraphQL サービスをベースに、以下の拡張が可能です:
2091
+
2092
+ 1. **認証・認可**: Spring Security + JWT による GraphQL 認証
2093
+ 2. **キャッシュ**: Redis によるクエリ結果キャッシュ
2094
+ 3. **監視**: GraphQL メトリクスの収集と監視
2095
+ 4. **フェデレーション**: Apollo Federation による複数サービス統合
2096
+ 5. **コード生成**: GraphQL Code Generator によるクライアントコード自動生成
2097
+
2098
+ ### 27.5 学習した内容
2099
+
2100
+ 1. **GraphQL の基本概念**: Query、Mutation、Subscription の 3 つの操作タイプ
2101
+ 2. **スキーマ駆動開発**: `.graphqls` ファイルによる型安全な API 定義
2102
+ 3. **N+1 問題と DataLoader**: バッチ処理によるパフォーマンス最適化
2103
+ 4. **リゾルバ実装**: `@QueryMapping`、`@MutationMapping`、`@SchemaMapping`
2104
+ 5. **Subscription**: WebSocket によるリアルタイム通知
2105
+ 6. **エラーハンドリング**: カスタム例外と GraphQL エラー形式への変換
2106
+ 7. **テスト**: `HttpGraphQlTester` による統合テスト