Commit cee0197b authored by Mikkel Marstein's avatar Mikkel Marstein
Browse files

Ordering sort

parent bc6bffcf
//import Country from './country';
const Country = require('./country');
function buildFilters({OR = [], continent_contains, name_contains}) {
const filter = (continent_contains || name_contains) ? {} : null;
if (continent_contains) {
filter.continent = {$regex: `.*${continent_contains}.*`};
}
if (name_contains) {
filter.name = {$regex: `.*${name_contains}.*`};
}
let filters = filter ? [filter] : [];
for (let i = 0; i < OR.length; i++) {
filters = filters.concat(buildFilters(OR[i]));
}
return filters;
}
const resolvers = {
Query: {
country(parent, args, context, info) {
return country.find(country => country.name === args.name);
}
}
}
module.exports = {
country: async ({filter}) => {
country: async (args) => {
try {
let query = filter ? {$or: buildFilters(resolvers)} : {};
const countryFetched = await Country.find(query);
let kontinent = String(args.continent_contains || "")
//let query = filter ? {$or: buildFilters(resolvers)} : {};
sortOrder = {};
//if(args.sort !== void 0){
sortOrder = { capital: -1};
const countryFetched = await Country.find({continent: {$in: kontinent}})
.sort(sortOrder);
return countryFetched.map((country) => {
return {
...country._doc,
......@@ -37,21 +21,5 @@ module.exports = {
} catch (error) {
throw error;
}
}
}
module.exports = {
country: async ({filter}) => {
try {
let query = filter ? {$or: buildFilters(filter)} : {};
const countryFetched = await Country.find(query);
return countryFetched.map((country) => {
return {
...country._doc,
_id: country._id,
};
});
} catch (error) {
throw error;
}
}
}
},
}
\ No newline at end of file
......@@ -16,10 +16,12 @@ module.exports = buildSchema(`
navn: String!
capital: String!
}
type Query {
country(filter: ContinentFilter): [Country!]
country(continent_contains: String, skip: Int, take: Int, order: String): [Country!]
}
input NameOrder {
nameOrder: String
}
input ContinentFilter {
OR: [ContinentFilter!]
continent_contains: String
......
......@@ -13,6 +13,6 @@ function getData(query: string): Promise<Country[]> {
});
}
export function getAllData(): Promise<Country[]> {
let order: string = ' (order: { capital: Sort }, first: 5) '
return getData('{country'+order+'{name, native, continent, capital, currency, languages}}');
let order: string = ' (or '
return getData('{country'+'{name, native, continent, capital, currency, languages}}');
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment