Skip to content

Commit

Permalink
fix: Missing elements in the definition
Browse files Browse the repository at this point in the history
Signed-off-by: Avior <[email protected]>
  • Loading branch information
Aviortheking committed Jan 2, 2024
1 parent dd37152 commit ec9d721
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 39 deletions.
95 changes: 71 additions & 24 deletions meta/definitions/graphql.gql
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,62 @@ directive @locale (
lang: String!
) on FIELD

# Queries to use on the DB
"""
Every queries available on the GraphQL API
If you have more queries that you would like added, make a new issue here
https://github.com/tcgdex/cards-database/issues/new/choose
"""
type Query {
cards(filters: CardsFilters, pagination: Pagination): [Card]
sets: [Set]
series: [Serie]
"""Find the cards"""
cards(filters: CardsFilters, pagination: Pagination, sort: Sort): [Card]

"""Find the sets"""
sets(filters: SetFilters, pagination: Pagination, sort: Sort): [Set]

"""Find the series"""
series(filters: SerieFilters, pagination: Pagination, sort: Sort): [Serie]

"""Find one card (using the id and set is deprecated)"""
card(
id: ID!,
set: String
set: String,
"""The new way to filter"""
filters: CardsFilters
): Card

"""Find one set (using the id is deprecated)"""
set(
id: ID!
id: ID!,
"""The new way to filter"""
filters: SetFilters
): Set

"""Find one serie (using the id is deprecated)"""
serie(
id: ID!
id: ID!,
"""The new way to filter"""
filters: SerieFilters
): Serie

}

# Pagination input
"""Paginate the datas you fetch"""
input Pagination {
page: Float!
count: Float!
"""Indicate the page number (from 1)"""
page: Int!
"""Indicate the number of items in one page"""
itemsPerPage: Int
count: Float @deprecated(reason: "use itemsPerPage instead")
}

"""Change how the data is sorted"""
input Sort {
"""Indicate which field it will sort using"""
field: String!
"""Indicate how it is sorted ("ASC" or "DESC) (default: "ASC")"""
order: String
}

##################
Expand All @@ -41,45 +75,45 @@ input CardsFilters {
description: String
energyType: String
evolveFrom: String
hp: Float
hp: Int
id: ID
localId: String
dexId: Float
dexId: Int
illustrator: String
image: String
level: Float
level: Int
levelId: String
name: String
rarity: String
regulationMark: String
stage: String
suffix: String
trainerType: String
retreat: Float
retreat: Int
}

type Card {
abilities: [AbilitiesListItem]
attacks: [AttacksListItem]
category: String!
description: String
dexId: [Float]
dexId: [Int]
effect: String
energyType: String
evolveFrom: String
hp: Float
hp: Int
id: String!
illustrator: String
image: String
item: Item
legal: Legal!
level: Float
level: Int
localId: String!
name: String!
rarity: String!
regulationMark: String
resistances: [WeakResListItem]
retreat: Float
retreat: Int
set: Set!
stage: String
suffix: String
Expand Down Expand Up @@ -143,13 +177,21 @@ type Set {
tcgOnline: String
}

input SetFilters {
id: String
name: String
serie: String
releaseDate: String
tcgOnline: String
}

type CardCount {
firstEd: Float
holo: Float
normal: Float
official: Float!
reverse: Float
total: Float!
firstEd: Int
holo: Int
normal: Int
official: Int!
reverse: Int
total: Int!
}

##################
Expand All @@ -163,6 +205,11 @@ type Serie {
sets: [Set]!
}

input SerieFilters {
id: String
name: String
}

##################
# StringEndpoint #
##################
Expand Down
10 changes: 7 additions & 3 deletions server/src/V2/graphql/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ const middleware = (fn: (lang: SupportedLanguages, query: Query) => any) => (
_: any,
e: any
) => {

console.log(data)

// get the locale directive
const langArgument = e?.fieldNodes?.[0]?.directives?.[0]?.arguments?.[0]?.value

// Deprecated code handling
// @ts-expect-error count is deprectaed in the frontend
if (data.pagination?.count) {
// @ts-expect-error count is deprectaed in the frontend
data.pagination.itemsPerPage = data.pagination.count
}

// if there is no locale directive
if (!langArgument) {
return fn('en', data)
Expand Down
38 changes: 26 additions & 12 deletions server/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,35 +105,42 @@ export function validateItem(validator: any | Array<any>, value: any): boolean {
*/
export function handleSort(data: Array<any>, query: Query<any>) {
const sort: Query<any>['sort'] = query.sort ?? {field: 'id', order: 'ASC'}
const field = sort.field
const order = sort.order ?? 'ASC'
const firstEntry = data[0]

// early exit if the order is not correctly set
if (sort.order !== 'ASC' && sort.order !== 'DESC') {
console.warn('Sort order is not valid', sort.order)
if (order !== 'ASC' && order !== 'DESC') {
console.warn('Sort order is not valid', order)
return data
}

console.log('pouet', sort)

if (!(sort.field in firstEntry)) {
if (!(field in firstEntry)) {
return data
}
const sortType = typeof data[0][sort.field]
const sortType = typeof data[0][field]
if (sortType === 'number') {
if (sort.order === 'ASC') {
return data.sort((a, b) => a[sort.field] - b[sort.field])
if (order === 'ASC') {
return data.sort((a, b) => a[field] - b[field])
} else {
return data.sort((a, b) => b[sort.field] - a[sort.field])
return data.sort((a, b) => b[field] - a[field])
}
} else {
if (sort.order === 'ASC') {
return data.sort((a, b) => a[sort.field] > b[sort.field] ? 1 : -1)
if (order === 'ASC') {
return data.sort((a, b) => a[field] > b[field] ? 1 : -1)
} else {
return data.sort((a, b) => a[sort.field] > b[sort.field] ? -1 : 1)
return data.sort((a, b) => a[field] > b[field] ? -1 : 1)
}
}
}

/**
* filter data out to make it paginated
*
* @param data the data to paginate
* @param query the query
* @returns the data that is in the paginated query
*/
export function handlePagination(data: Array<any>, query: Query<any>) {
if (!query.pagination) {
return data
Expand All @@ -148,6 +155,13 @@ export function handlePagination(data: Array<any>, query: Query<any>) {
)
}

/**
* filter the data using the specified query
*
* @param data the data to validate
* @param query the query to validate against
* @returns the filtered data
*/
export function handleValidation(data: Array<any>, query: Query) {
const filters = query.filters
if (!filters) {
Expand Down

0 comments on commit ec9d721

Please sign in to comment.