Add filter to languages and continents

This commit is contained in:
Trevor Blades
2020-03-30 09:24:32 -07:00
parent 7e0d2de6ed
commit 5892f6d21e

View File

@@ -37,12 +37,6 @@ const typeDefs = gql`
rtl: Boolean! rtl: Boolean!
} }
input CountryFilterInput {
code: StringQueryOperatorInput
currency: StringQueryOperatorInput
continent: StringQueryOperatorInput
}
input StringQueryOperatorInput { input StringQueryOperatorInput {
eq: String eq: String
ne: String ne: String
@@ -52,27 +46,43 @@ const typeDefs = gql`
glob: String glob: String
} }
input CountryFilterInput {
code: StringQueryOperatorInput
currency: StringQueryOperatorInput
continent: StringQueryOperatorInput
}
input ContinentFilterInput {
code: StringQueryOperatorInput
}
input LanguageFilterInput {
code: StringQueryOperatorInput
}
type Query { type Query {
continents: [Continent!]! continents(filter: ContinentFilterInput): [Continent!]!
continent(code: ID!): Continent continent(code: ID!): Continent
countries(filter: CountryFilterInput): [Country!]! countries(filter: CountryFilterInput): [Country!]!
country(code: ID!): Country country(code: ID!): Country
languages: [Language!]! languages(filter: LanguageFilterInput): [Language!]!
language(code: ID!): Language language(code: ID!): Language
} }
`; `;
function siftifyFilter(filter) { function filterToSift(filter = {}) {
return Object.entries(filter).reduce( return sift(
Object.entries(filter).reduce(
(acc, [key, operators]) => ({ (acc, [key, operators]) => ({
...acc, ...acc,
[key]: siftifyOperators(operators) [key]: operatorsToSift(operators)
}), }),
{} {}
)
); );
} }
function siftifyOperators(operators) { function operatorsToSift(operators) {
return Object.entries(operators).reduce( return Object.entries(operators).reduce(
(acc, [operator, value]) => ({ (acc, [operator, value]) => ({
...acc, ...acc,
@@ -127,11 +137,13 @@ const resolvers = {
} }
); );
}, },
continents: () => continents: (parent, {filter}) =>
Object.entries(continents).map(([code, name]) => ({ Object.entries(continents)
.map(([code, name]) => ({
code, code,
name name
})), }))
.filter(filterToSift(filter)),
country(parent, {code}) { country(parent, {code}) {
const country = countries[code]; const country = countries[code];
return ( return (
@@ -141,13 +153,13 @@ const resolvers = {
} }
); );
}, },
countries: (parent, {filter = {}}) => countries: (parent, {filter}) =>
Object.entries(countries) Object.entries(countries)
.map(([code, country]) => ({ .map(([code, country]) => ({
...country, ...country,
code code
})) }))
.filter(sift(siftifyFilter(filter))), .filter(filterToSift(filter)),
language(parent, {code}) { language(parent, {code}) {
const language = languages[code]; const language = languages[code];
return ( return (
@@ -157,11 +169,13 @@ const resolvers = {
} }
); );
}, },
languages: () => languages: (parent, {filter}) =>
Object.entries(languages).map(([code, language]) => ({ Object.entries(languages)
.map(([code, language]) => ({
...language, ...language,
code code
})) }))
.filter(filterToSift(filter))
} }
}; };