diff --git a/meta/definitions/graphql.gql b/meta/definitions/graphql.gql index 085e78d07a..45efe1a5e7 100644 --- a/meta/definitions/graphql.gql +++ b/meta/definitions/graphql.gql @@ -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 } ################## @@ -41,13 +75,13 @@ 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 @@ -55,7 +89,7 @@ input CardsFilters { stage: String suffix: String trainerType: String - retreat: Float + retreat: Int } type Card { @@ -63,23 +97,23 @@ type Card { 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 @@ -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! } ################## @@ -163,6 +205,11 @@ type Serie { sets: [Set]! } +input SerieFilters { + id: String + name: String +} + ################## # StringEndpoint # ################## diff --git a/server/src/V2/graphql/resolver.ts b/server/src/V2/graphql/resolver.ts index 9e5bca27a2..2c05f2a910 100644 --- a/server/src/V2/graphql/resolver.ts +++ b/server/src/V2/graphql/resolver.ts @@ -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) diff --git a/server/src/util.ts b/server/src/util.ts index fb69be2bd2..282d4956db 100644 --- a/server/src/util.ts +++ b/server/src/util.ts @@ -105,35 +105,42 @@ export function validateItem(validator: any | Array, value: any): boolean { */ export function handleSort(data: Array, query: Query) { const sort: Query['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, query: Query) { if (!query.pagination) { return data @@ -148,6 +155,13 @@ export function handlePagination(data: Array, query: Query) { ) } +/** + * 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, query: Query) { const filters = query.filters if (!filters) {