Skip to content

Commit

Permalink
feat: ignore foreign keys in schema generation (#149)
Browse files Browse the repository at this point in the history
* Ignore elements that are flat foreign keys in schema generation

* Variable name cosmetics

* Add changelog entry

* Add TODO comment for `@cds.api.ignore`
  • Loading branch information
schwma authored Jan 30, 2024
1 parent aa66bf2 commit cbebdae
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 64 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

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

### Fixed
Expand Down
6 changes: 5 additions & 1 deletion lib/schema/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ const hasScalarFields = entity =>
([, el]) => !(shouldElementBeIgnored(el) || el.isAssociation || el.isComposition)
)

const _isFlatForeignKey = element => Boolean(element['@odata.foreignKey4'] || element._foreignKey4)

const _isLocalized = element => element.name === 'localized' && element.target?.endsWith('.texts')

const shouldElementBeIgnored = element => element.name.startsWith('up_') || _isLocalized(element)
// TODO: add check for @cds.api.ignore
const shouldElementBeIgnored = element =>
element.name.startsWith('up_') || _isLocalized(element) || _isFlatForeignKey(element)

const isCompositionOfAspect = entity =>
Object.values(entity.elements.up_?._target.elements ?? {}).some(e => e.targetAspect && e._target.name === entity.name)
Expand Down
53 changes: 0 additions & 53 deletions test/schemas/bookshop-graphql.gql
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ See https://cap.cloud.sap/docs/cds/common#aspect-managed
type AdminService_Books {
ID: Int
author: AdminService_Authors
author_ID: Int
chapters(filter: [AdminService_Chapters_filter], orderBy: [AdminService_Chapters_orderBy], skip: Int, top: Int): AdminService_Chapters_connection
createdAt: Timestamp

Expand All @@ -118,10 +117,8 @@ type AdminService_Books {
See https://cap.cloud.sap/docs/cds/common#type-currency
"""
currency: AdminService_Currencies
currency_code: String
descr: String
genre: AdminService_Genres
genre_ID: Int
image: Binary
modifiedAt: Timestamp

Expand All @@ -136,15 +133,12 @@ type AdminService_Books {
input AdminService_Books_C {
ID: Int
author: AdminService_Authors_C
author_ID: Int
chapters: [AdminService_Chapters_C]
createdAt: Timestamp
createdBy: String
currency: AdminService_Currencies_C
currency_code: String
descr: String
genre: AdminService_Genres_C
genre_ID: Int
image: Binary
modifiedAt: Timestamp
modifiedBy: String
Expand All @@ -156,15 +150,12 @@ input AdminService_Books_C {

input AdminService_Books_U {
author: AdminService_Authors_C
author_ID: Int
chapters: [AdminService_Chapters_C]
createdAt: Timestamp
createdBy: String
currency: AdminService_Currencies_C
currency_code: String
descr: String
genre: AdminService_Genres_C
genre_ID: Int
image: Binary
modifiedAt: Timestamp
modifiedBy: String
Expand All @@ -181,12 +172,9 @@ type AdminService_Books_connection {

input AdminService_Books_filter {
ID: [Int_filter]
author_ID: [Int_filter]
createdAt: [Timestamp_filter]
createdBy: [String_filter]
currency_code: [String_filter]
descr: [String_filter]
genre_ID: [Int_filter]
image: [Binary_filter]
modifiedAt: [Timestamp_filter]
modifiedBy: [String_filter]
Expand All @@ -203,12 +191,9 @@ type AdminService_Books_input {

input AdminService_Books_orderBy {
ID: SortDirection
author_ID: SortDirection
createdAt: SortDirection
createdBy: SortDirection
currency_code: SortDirection
descr: SortDirection
genre_ID: SortDirection
image: SortDirection
modifiedAt: SortDirection
modifiedBy: SortDirection
Expand Down Expand Up @@ -266,7 +251,6 @@ input AdminService_Books_texts_orderBy {
"""A Chapter of a Book"""
type AdminService_Chapters {
book: AdminService_Books
book_ID: Int
createdAt: Timestamp

"""Canonical user ID"""
Expand All @@ -281,7 +265,6 @@ type AdminService_Chapters {

input AdminService_Chapters_C {
book: AdminService_Books_C
book_ID: Int
createdAt: Timestamp
createdBy: String
modifiedAt: Timestamp
Expand All @@ -304,7 +287,6 @@ type AdminService_Chapters_connection {
}

input AdminService_Chapters_filter {
book_ID: [Int_filter]
createdAt: [Timestamp_filter]
createdBy: [String_filter]
modifiedAt: [Timestamp_filter]
Expand All @@ -320,7 +302,6 @@ type AdminService_Chapters_input {
}

input AdminService_Chapters_orderBy {
book_ID: SortDirection
createdAt: SortDirection
createdBy: SortDirection
modifiedAt: SortDirection
Expand Down Expand Up @@ -440,7 +421,6 @@ type AdminService_Genres {
descr: String
name: String
parent: AdminService_Genres
parent_ID: Int
texts(filter: [AdminService_Genres_texts_filter], orderBy: [AdminService_Genres_texts_orderBy], skip: Int, top: Int): AdminService_Genres_texts_connection
}

Expand All @@ -450,7 +430,6 @@ input AdminService_Genres_C {
descr: String
name: String
parent: AdminService_Genres_C
parent_ID: Int
texts: [AdminService_Genres_texts_C]
}

Expand All @@ -459,7 +438,6 @@ input AdminService_Genres_U {
descr: String
name: String
parent: AdminService_Genres_C
parent_ID: Int
texts: [AdminService_Genres_texts_C]
}

Expand All @@ -472,7 +450,6 @@ input AdminService_Genres_filter {
ID: [Int_filter]
descr: [String_filter]
name: [String_filter]
parent_ID: [Int_filter]
}

type AdminService_Genres_input {
Expand All @@ -485,7 +462,6 @@ input AdminService_Genres_orderBy {
ID: SortDirection
descr: SortDirection
name: SortDirection
parent_ID: SortDirection
}

type AdminService_Genres_texts {
Expand Down Expand Up @@ -579,10 +555,8 @@ type CatalogService_Books {
See https://cap.cloud.sap/docs/cds/common#type-currency
"""
currency: CatalogService_Currencies
currency_code: String
descr: String
genre: CatalogService_Genres
genre_ID: Int
image: Binary
modifiedAt: Timestamp
price: Decimal
Expand All @@ -597,10 +571,8 @@ input CatalogService_Books_C {
chapters: [CatalogService_Chapters_C]
createdAt: Timestamp
currency: CatalogService_Currencies_C
currency_code: String
descr: String
genre: CatalogService_Genres_C
genre_ID: Int
image: Binary
modifiedAt: Timestamp
price: Decimal
Expand All @@ -614,10 +586,8 @@ input CatalogService_Books_U {
chapters: [CatalogService_Chapters_C]
createdAt: Timestamp
currency: CatalogService_Currencies_C
currency_code: String
descr: String
genre: CatalogService_Genres_C
genre_ID: Int
image: Binary
modifiedAt: Timestamp
price: Decimal
Expand All @@ -635,9 +605,7 @@ input CatalogService_Books_filter {
ID: [Int_filter]
author: [String_filter]
createdAt: [Timestamp_filter]
currency_code: [String_filter]
descr: [String_filter]
genre_ID: [Int_filter]
image: [Binary_filter]
modifiedAt: [Timestamp_filter]
price: [Decimal_filter]
Expand All @@ -655,9 +623,7 @@ input CatalogService_Books_orderBy {
ID: SortDirection
author: SortDirection
createdAt: SortDirection
currency_code: SortDirection
descr: SortDirection
genre_ID: SortDirection
image: SortDirection
modifiedAt: SortDirection
price: SortDirection
Expand Down Expand Up @@ -714,7 +680,6 @@ input CatalogService_Books_texts_orderBy {
"""A Chapter of a Book"""
type CatalogService_Chapters {
book: CatalogService_Books
book_ID: Int
createdAt: Timestamp

"""Canonical user ID"""
Expand All @@ -729,7 +694,6 @@ type CatalogService_Chapters {

input CatalogService_Chapters_C {
book: CatalogService_Books_C
book_ID: Int
createdAt: Timestamp
createdBy: String
modifiedAt: Timestamp
Expand All @@ -752,7 +716,6 @@ type CatalogService_Chapters_connection {
}

input CatalogService_Chapters_filter {
book_ID: [Int_filter]
createdAt: [Timestamp_filter]
createdBy: [String_filter]
modifiedAt: [Timestamp_filter]
Expand All @@ -768,7 +731,6 @@ type CatalogService_Chapters_input {
}

input CatalogService_Chapters_orderBy {
book_ID: SortDirection
createdAt: SortDirection
createdBy: SortDirection
modifiedAt: SortDirection
Expand Down Expand Up @@ -888,7 +850,6 @@ type CatalogService_Genres {
descr: String
name: String
parent: CatalogService_Genres
parent_ID: Int
texts(filter: [CatalogService_Genres_texts_filter], orderBy: [CatalogService_Genres_texts_orderBy], skip: Int, top: Int): CatalogService_Genres_texts_connection
}

Expand All @@ -898,7 +859,6 @@ input CatalogService_Genres_C {
descr: String
name: String
parent: CatalogService_Genres_C
parent_ID: Int
texts: [CatalogService_Genres_texts_C]
}

Expand All @@ -907,7 +867,6 @@ input CatalogService_Genres_U {
descr: String
name: String
parent: CatalogService_Genres_C
parent_ID: Int
texts: [CatalogService_Genres_texts_C]
}

Expand All @@ -920,7 +879,6 @@ input CatalogService_Genres_filter {
ID: [Int_filter]
descr: [String_filter]
name: [String_filter]
parent_ID: [Int_filter]
}

type CatalogService_Genres_input {
Expand All @@ -933,7 +891,6 @@ input CatalogService_Genres_orderBy {
ID: SortDirection
descr: SortDirection
name: SortDirection
parent_ID: SortDirection
}

type CatalogService_Genres_texts {
Expand Down Expand Up @@ -995,9 +952,7 @@ type CatalogService_ListOfBooks {
See https://cap.cloud.sap/docs/cds/common#type-currency
"""
currency: CatalogService_Currencies
currency_code: String
genre: CatalogService_Genres
genre_ID: Int
image: Binary
modifiedAt: Timestamp
price: Decimal
Expand All @@ -1012,9 +967,7 @@ input CatalogService_ListOfBooks_C {
chapters: [CatalogService_Chapters_C]
createdAt: Timestamp
currency: CatalogService_Currencies_C
currency_code: String
genre: CatalogService_Genres_C
genre_ID: Int
image: Binary
modifiedAt: Timestamp
price: Decimal
Expand All @@ -1028,9 +981,7 @@ input CatalogService_ListOfBooks_U {
chapters: [CatalogService_Chapters_C]
createdAt: Timestamp
currency: CatalogService_Currencies_C
currency_code: String
genre: CatalogService_Genres_C
genre_ID: Int
image: Binary
modifiedAt: Timestamp
price: Decimal
Expand All @@ -1048,8 +999,6 @@ input CatalogService_ListOfBooks_filter {
ID: [Int_filter]
author: [String_filter]
createdAt: [Timestamp_filter]
currency_code: [String_filter]
genre_ID: [Int_filter]
image: [Binary_filter]
modifiedAt: [Timestamp_filter]
price: [Decimal_filter]
Expand All @@ -1067,8 +1016,6 @@ input CatalogService_ListOfBooks_orderBy {
ID: SortDirection
author: SortDirection
createdAt: SortDirection
currency_code: SortDirection
genre_ID: SortDirection
image: SortDirection
modifiedAt: SortDirection
price: SortDirection
Expand Down
5 changes: 0 additions & 5 deletions test/schemas/edge-cases/field-named-localized.gql
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,17 @@ type FieldNamedLocalizedService_localized {
ID: Int
localized: String
root: FieldNamedLocalizedService_Root
root_ID: Int
}

input FieldNamedLocalizedService_localized_C {
ID: Int
localized: String
root: FieldNamedLocalizedService_Root_C
root_ID: Int
}

input FieldNamedLocalizedService_localized_U {
localized: String
root: FieldNamedLocalizedService_Root_C
root_ID: Int
}

type FieldNamedLocalizedService_localized_connection {
Expand All @@ -69,7 +66,6 @@ type FieldNamedLocalizedService_localized_connection {
input FieldNamedLocalizedService_localized_filter {
ID: [Int_filter]
localized: [String_filter]
root_ID: [Int_filter]
}

type FieldNamedLocalizedService_localized_input {
Expand All @@ -81,7 +77,6 @@ type FieldNamedLocalizedService_localized_input {
input FieldNamedLocalizedService_localized_orderBy {
ID: SortDirection
localized: SortDirection
root_ID: SortDirection
}

input Int_filter {
Expand Down
Loading

0 comments on commit cbebdae

Please sign in to comment.