mirror of
https://github.com/FranP-code/countries.git
synced 2025-10-13 00:02:15 +00:00
Add filter to languages and continents
This commit is contained in:
70
index.js
70
index.js
@@ -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(
|
||||||
(acc, [key, operators]) => ({
|
Object.entries(filter).reduce(
|
||||||
...acc,
|
(acc, [key, operators]) => ({
|
||||||
[key]: siftifyOperators(operators)
|
...acc,
|
||||||
}),
|
[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)
|
||||||
code,
|
.map(([code, name]) => ({
|
||||||
name
|
code,
|
||||||
})),
|
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)
|
||||||
...language,
|
.map(([code, language]) => ({
|
||||||
code
|
...language,
|
||||||
}))
|
code
|
||||||
|
}))
|
||||||
|
.filter(filterToSift(filter))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user