Skip to content

Commit

Permalink
feat: Also support GraphQL
Browse files Browse the repository at this point in the history
Signed-off-by: Avior <[email protected]>
  • Loading branch information
Aviortheking committed Aug 21, 2024
1 parent d39d03d commit 06428cf
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions server/src/V2/graphql/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
import { SupportedLanguages } from '@tcgdex/sdk'
import { Query } from '../../interfaces'
import type { SupportedLanguages } from '@tcgdex/sdk'
import { type Query, Sort } from '../../libs/QueryEngine/filter'
import { recordToQuery } from '../../libs/QueryEngine/parsers'
import { checkLanguage } from '../../util'
import Card from '../Components/Card'
import Serie from '../Components/Serie'
import Set from '../Components/Set'

const middleware = (fn: (lang: SupportedLanguages, query: Query) => any) => (
data: Query,
// TODO: make a better way to find the language
function getLang(e: any): SupportedLanguages {
// get the locale directive
const langArgument = e?.fieldNodes?.[0]?.directives?.[0]?.arguments?.[0]?.value

if (!langArgument) {
return 'en'
}

if (langArgument.kind === 'Variable') {
return e.variableValues[langArgument.name.value]
}
return langArgument.value
}

const middleware = (fn: (lang: SupportedLanguages, query: Query<object>) => any) => (
data: Record<string, any>,
_: any,
e: any
) => {
// get the locale directive
const langArgument = e?.fieldNodes?.[0]?.directives?.[0]?.arguments?.[0]?.value
const lang = getLang(e)

// 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
}
const query = recordToQuery(data.filters)

// if there is no locale directive
if (!langArgument) {
return fn('en', data)
// Deprecated code handling
if (data.pagination) {
query.$page = data.pagination.page ?? 1
query.$limit = data.pagination.itemsPerPage ?? 100
}

// set default locale directive value
let lang = 'en'

// handle variable for directive value
if (langArgument.kind === 'Variable') {
lang = e.variableValues[langArgument.name.value]
} else {
lang = langArgument.value
if (data.sort) {
query.$sort = {
[data.sort.field]: data.sort.order === 'DESC' ? Sort.DESC : Sort.ASC
}
}

if (!checkLanguage(lang)) {
return undefined
}
return fn(lang, data)

return fn(lang, query)
}

export default {
Expand Down

0 comments on commit 06428cf

Please sign in to comment.