Skip to content

Commit cbebdae

Browse files
authored
feat: ignore foreign keys in schema generation (#149)
* Ignore elements that are flat foreign keys in schema generation * Variable name cosmetics * Add changelog entry * Add TODO comment for `@cds.api.ignore`
1 parent aa66bf2 commit cbebdae

File tree

5 files changed

+6
-64
lines changed

5 files changed

+6
-64
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Changed
1616

1717
- Bump `@graphiql/plugin-explorer` version to `^1`
18+
- Ignore fields that represent foreign keys in GraphQL schema generation
1819
- When compiling to GraphQL using the CDS API or CLI, only generate GraphQL schemas for services that are annotated with GraphQL protocol annotations
1920

2021
### Fixed

lib/schema/util/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ const hasScalarFields = entity =>
33
([, el]) => !(shouldElementBeIgnored(el) || el.isAssociation || el.isComposition)
44
)
55

6+
const _isFlatForeignKey = element => Boolean(element['@odata.foreignKey4'] || element._foreignKey4)
7+
68
const _isLocalized = element => element.name === 'localized' && element.target?.endsWith('.texts')
79

8-
const shouldElementBeIgnored = element => element.name.startsWith('up_') || _isLocalized(element)
10+
// TODO: add check for @cds.api.ignore
11+
const shouldElementBeIgnored = element =>
12+
element.name.startsWith('up_') || _isLocalized(element) || _isFlatForeignKey(element)
913

1014
const isCompositionOfAspect = entity =>
1115
Object.values(entity.elements.up_?._target.elements ?? {}).some(e => e.targetAspect && e._target.name === entity.name)

test/schemas/bookshop-graphql.gql

-53
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ See https://cap.cloud.sap/docs/cds/common#aspect-managed
105105
type AdminService_Books {
106106
ID: Int
107107
author: AdminService_Authors
108-
author_ID: Int
109108
chapters(filter: [AdminService_Chapters_filter], orderBy: [AdminService_Chapters_orderBy], skip: Int, top: Int): AdminService_Chapters_connection
110109
createdAt: Timestamp
111110

@@ -118,10 +117,8 @@ type AdminService_Books {
118117
See https://cap.cloud.sap/docs/cds/common#type-currency
119118
"""
120119
currency: AdminService_Currencies
121-
currency_code: String
122120
descr: String
123121
genre: AdminService_Genres
124-
genre_ID: Int
125122
image: Binary
126123
modifiedAt: Timestamp
127124

@@ -136,15 +133,12 @@ type AdminService_Books {
136133
input AdminService_Books_C {
137134
ID: Int
138135
author: AdminService_Authors_C
139-
author_ID: Int
140136
chapters: [AdminService_Chapters_C]
141137
createdAt: Timestamp
142138
createdBy: String
143139
currency: AdminService_Currencies_C
144-
currency_code: String
145140
descr: String
146141
genre: AdminService_Genres_C
147-
genre_ID: Int
148142
image: Binary
149143
modifiedAt: Timestamp
150144
modifiedBy: String
@@ -156,15 +150,12 @@ input AdminService_Books_C {
156150

157151
input AdminService_Books_U {
158152
author: AdminService_Authors_C
159-
author_ID: Int
160153
chapters: [AdminService_Chapters_C]
161154
createdAt: Timestamp
162155
createdBy: String
163156
currency: AdminService_Currencies_C
164-
currency_code: String
165157
descr: String
166158
genre: AdminService_Genres_C
167-
genre_ID: Int
168159
image: Binary
169160
modifiedAt: Timestamp
170161
modifiedBy: String
@@ -181,12 +172,9 @@ type AdminService_Books_connection {
181172

182173
input AdminService_Books_filter {
183174
ID: [Int_filter]
184-
author_ID: [Int_filter]
185175
createdAt: [Timestamp_filter]
186176
createdBy: [String_filter]
187-
currency_code: [String_filter]
188177
descr: [String_filter]
189-
genre_ID: [Int_filter]
190178
image: [Binary_filter]
191179
modifiedAt: [Timestamp_filter]
192180
modifiedBy: [String_filter]
@@ -203,12 +191,9 @@ type AdminService_Books_input {
203191

204192
input AdminService_Books_orderBy {
205193
ID: SortDirection
206-
author_ID: SortDirection
207194
createdAt: SortDirection
208195
createdBy: SortDirection
209-
currency_code: SortDirection
210196
descr: SortDirection
211-
genre_ID: SortDirection
212197
image: SortDirection
213198
modifiedAt: SortDirection
214199
modifiedBy: SortDirection
@@ -266,7 +251,6 @@ input AdminService_Books_texts_orderBy {
266251
"""A Chapter of a Book"""
267252
type AdminService_Chapters {
268253
book: AdminService_Books
269-
book_ID: Int
270254
createdAt: Timestamp
271255

272256
"""Canonical user ID"""
@@ -281,7 +265,6 @@ type AdminService_Chapters {
281265

282266
input AdminService_Chapters_C {
283267
book: AdminService_Books_C
284-
book_ID: Int
285268
createdAt: Timestamp
286269
createdBy: String
287270
modifiedAt: Timestamp
@@ -304,7 +287,6 @@ type AdminService_Chapters_connection {
304287
}
305288

306289
input AdminService_Chapters_filter {
307-
book_ID: [Int_filter]
308290
createdAt: [Timestamp_filter]
309291
createdBy: [String_filter]
310292
modifiedAt: [Timestamp_filter]
@@ -320,7 +302,6 @@ type AdminService_Chapters_input {
320302
}
321303

322304
input AdminService_Chapters_orderBy {
323-
book_ID: SortDirection
324305
createdAt: SortDirection
325306
createdBy: SortDirection
326307
modifiedAt: SortDirection
@@ -440,7 +421,6 @@ type AdminService_Genres {
440421
descr: String
441422
name: String
442423
parent: AdminService_Genres
443-
parent_ID: Int
444424
texts(filter: [AdminService_Genres_texts_filter], orderBy: [AdminService_Genres_texts_orderBy], skip: Int, top: Int): AdminService_Genres_texts_connection
445425
}
446426

@@ -450,7 +430,6 @@ input AdminService_Genres_C {
450430
descr: String
451431
name: String
452432
parent: AdminService_Genres_C
453-
parent_ID: Int
454433
texts: [AdminService_Genres_texts_C]
455434
}
456435

@@ -459,7 +438,6 @@ input AdminService_Genres_U {
459438
descr: String
460439
name: String
461440
parent: AdminService_Genres_C
462-
parent_ID: Int
463441
texts: [AdminService_Genres_texts_C]
464442
}
465443

@@ -472,7 +450,6 @@ input AdminService_Genres_filter {
472450
ID: [Int_filter]
473451
descr: [String_filter]
474452
name: [String_filter]
475-
parent_ID: [Int_filter]
476453
}
477454

478455
type AdminService_Genres_input {
@@ -485,7 +462,6 @@ input AdminService_Genres_orderBy {
485462
ID: SortDirection
486463
descr: SortDirection
487464
name: SortDirection
488-
parent_ID: SortDirection
489465
}
490466

491467
type AdminService_Genres_texts {
@@ -579,10 +555,8 @@ type CatalogService_Books {
579555
See https://cap.cloud.sap/docs/cds/common#type-currency
580556
"""
581557
currency: CatalogService_Currencies
582-
currency_code: String
583558
descr: String
584559
genre: CatalogService_Genres
585-
genre_ID: Int
586560
image: Binary
587561
modifiedAt: Timestamp
588562
price: Decimal
@@ -597,10 +571,8 @@ input CatalogService_Books_C {
597571
chapters: [CatalogService_Chapters_C]
598572
createdAt: Timestamp
599573
currency: CatalogService_Currencies_C
600-
currency_code: String
601574
descr: String
602575
genre: CatalogService_Genres_C
603-
genre_ID: Int
604576
image: Binary
605577
modifiedAt: Timestamp
606578
price: Decimal
@@ -614,10 +586,8 @@ input CatalogService_Books_U {
614586
chapters: [CatalogService_Chapters_C]
615587
createdAt: Timestamp
616588
currency: CatalogService_Currencies_C
617-
currency_code: String
618589
descr: String
619590
genre: CatalogService_Genres_C
620-
genre_ID: Int
621591
image: Binary
622592
modifiedAt: Timestamp
623593
price: Decimal
@@ -635,9 +605,7 @@ input CatalogService_Books_filter {
635605
ID: [Int_filter]
636606
author: [String_filter]
637607
createdAt: [Timestamp_filter]
638-
currency_code: [String_filter]
639608
descr: [String_filter]
640-
genre_ID: [Int_filter]
641609
image: [Binary_filter]
642610
modifiedAt: [Timestamp_filter]
643611
price: [Decimal_filter]
@@ -655,9 +623,7 @@ input CatalogService_Books_orderBy {
655623
ID: SortDirection
656624
author: SortDirection
657625
createdAt: SortDirection
658-
currency_code: SortDirection
659626
descr: SortDirection
660-
genre_ID: SortDirection
661627
image: SortDirection
662628
modifiedAt: SortDirection
663629
price: SortDirection
@@ -714,7 +680,6 @@ input CatalogService_Books_texts_orderBy {
714680
"""A Chapter of a Book"""
715681
type CatalogService_Chapters {
716682
book: CatalogService_Books
717-
book_ID: Int
718683
createdAt: Timestamp
719684

720685
"""Canonical user ID"""
@@ -729,7 +694,6 @@ type CatalogService_Chapters {
729694

730695
input CatalogService_Chapters_C {
731696
book: CatalogService_Books_C
732-
book_ID: Int
733697
createdAt: Timestamp
734698
createdBy: String
735699
modifiedAt: Timestamp
@@ -752,7 +716,6 @@ type CatalogService_Chapters_connection {
752716
}
753717

754718
input CatalogService_Chapters_filter {
755-
book_ID: [Int_filter]
756719
createdAt: [Timestamp_filter]
757720
createdBy: [String_filter]
758721
modifiedAt: [Timestamp_filter]
@@ -768,7 +731,6 @@ type CatalogService_Chapters_input {
768731
}
769732

770733
input CatalogService_Chapters_orderBy {
771-
book_ID: SortDirection
772734
createdAt: SortDirection
773735
createdBy: SortDirection
774736
modifiedAt: SortDirection
@@ -888,7 +850,6 @@ type CatalogService_Genres {
888850
descr: String
889851
name: String
890852
parent: CatalogService_Genres
891-
parent_ID: Int
892853
texts(filter: [CatalogService_Genres_texts_filter], orderBy: [CatalogService_Genres_texts_orderBy], skip: Int, top: Int): CatalogService_Genres_texts_connection
893854
}
894855

@@ -898,7 +859,6 @@ input CatalogService_Genres_C {
898859
descr: String
899860
name: String
900861
parent: CatalogService_Genres_C
901-
parent_ID: Int
902862
texts: [CatalogService_Genres_texts_C]
903863
}
904864

@@ -907,7 +867,6 @@ input CatalogService_Genres_U {
907867
descr: String
908868
name: String
909869
parent: CatalogService_Genres_C
910-
parent_ID: Int
911870
texts: [CatalogService_Genres_texts_C]
912871
}
913872

@@ -920,7 +879,6 @@ input CatalogService_Genres_filter {
920879
ID: [Int_filter]
921880
descr: [String_filter]
922881
name: [String_filter]
923-
parent_ID: [Int_filter]
924882
}
925883

926884
type CatalogService_Genres_input {
@@ -933,7 +891,6 @@ input CatalogService_Genres_orderBy {
933891
ID: SortDirection
934892
descr: SortDirection
935893
name: SortDirection
936-
parent_ID: SortDirection
937894
}
938895

939896
type CatalogService_Genres_texts {
@@ -995,9 +952,7 @@ type CatalogService_ListOfBooks {
995952
See https://cap.cloud.sap/docs/cds/common#type-currency
996953
"""
997954
currency: CatalogService_Currencies
998-
currency_code: String
999955
genre: CatalogService_Genres
1000-
genre_ID: Int
1001956
image: Binary
1002957
modifiedAt: Timestamp
1003958
price: Decimal
@@ -1012,9 +967,7 @@ input CatalogService_ListOfBooks_C {
1012967
chapters: [CatalogService_Chapters_C]
1013968
createdAt: Timestamp
1014969
currency: CatalogService_Currencies_C
1015-
currency_code: String
1016970
genre: CatalogService_Genres_C
1017-
genre_ID: Int
1018971
image: Binary
1019972
modifiedAt: Timestamp
1020973
price: Decimal
@@ -1028,9 +981,7 @@ input CatalogService_ListOfBooks_U {
1028981
chapters: [CatalogService_Chapters_C]
1029982
createdAt: Timestamp
1030983
currency: CatalogService_Currencies_C
1031-
currency_code: String
1032984
genre: CatalogService_Genres_C
1033-
genre_ID: Int
1034985
image: Binary
1035986
modifiedAt: Timestamp
1036987
price: Decimal
@@ -1048,8 +999,6 @@ input CatalogService_ListOfBooks_filter {
1048999
ID: [Int_filter]
10491000
author: [String_filter]
10501001
createdAt: [Timestamp_filter]
1051-
currency_code: [String_filter]
1052-
genre_ID: [Int_filter]
10531002
image: [Binary_filter]
10541003
modifiedAt: [Timestamp_filter]
10551004
price: [Decimal_filter]
@@ -1067,8 +1016,6 @@ input CatalogService_ListOfBooks_orderBy {
10671016
ID: SortDirection
10681017
author: SortDirection
10691018
createdAt: SortDirection
1070-
currency_code: SortDirection
1071-
genre_ID: SortDirection
10721019
image: SortDirection
10731020
modifiedAt: SortDirection
10741021
price: SortDirection

test/schemas/edge-cases/field-named-localized.gql

-5
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,17 @@ type FieldNamedLocalizedService_localized {
4545
ID: Int
4646
localized: String
4747
root: FieldNamedLocalizedService_Root
48-
root_ID: Int
4948
}
5049

5150
input FieldNamedLocalizedService_localized_C {
5251
ID: Int
5352
localized: String
5453
root: FieldNamedLocalizedService_Root_C
55-
root_ID: Int
5654
}
5755

5856
input FieldNamedLocalizedService_localized_U {
5957
localized: String
6058
root: FieldNamedLocalizedService_Root_C
61-
root_ID: Int
6259
}
6360

6461
type FieldNamedLocalizedService_localized_connection {
@@ -69,7 +66,6 @@ type FieldNamedLocalizedService_localized_connection {
6966
input FieldNamedLocalizedService_localized_filter {
7067
ID: [Int_filter]
7168
localized: [String_filter]
72-
root_ID: [Int_filter]
7369
}
7470

7571
type FieldNamedLocalizedService_localized_input {
@@ -81,7 +77,6 @@ type FieldNamedLocalizedService_localized_input {
8177
input FieldNamedLocalizedService_localized_orderBy {
8278
ID: SortDirection
8379
localized: SortDirection
84-
root_ID: SortDirection
8580
}
8681

8782
input Int_filter {

0 commit comments

Comments
 (0)