JSFiddle - React, Tailwind, and code Playground
by igalst
JavaScript
const express = require('express');
const { pick } = require('lodash');
const gql = require('graphql-tag');
const { keystone } = require('../keystone');
const BASE_PATH = '/rest';
const QUERY_APARTMENTS_BY_CITY = gql`
query apartmentsByCity($citySlug: String!) {
allApartments(where: { city: { slug: $citySlug } }, sortBy: order_DESC) {
id
slug
title
monthPrice
cleaningPrice
utilitiesPrice
size
baths
balconies
floor
bedrooms
location {
lat
lng
formattedAddress
}
isComingSoon
isPhotosComingSoon
order
# bookedPeriods
}
}
`;
// const QUERY_APARTMENT_IMAGES_BY_APARTMENTS = gql`
// query allApartmentImages($apartmentImageIds: [ID]) {
// allApartmentImages(where: { id_in: $apartmentImageIds }) {
// id
// name
// order
// }
// }
// `;
const ERROR_QUERY = new Error('Query error');
const withErrorQueryCatch = handler => async (req, res, next) => {
try {
await handler(req, res, next);
} catch (error) {
const isCatchError = error === ERROR_QUERY;
next(isCatchError ? undefined : error);
}
};
const queryOrFail = async (res, query, variables) => {
const result = await keystone.executeGraphQL({ query, variables });
if (!result.data) {
res.json(result.errors);
return Promise.reject(ERROR_QUERY);
}
return result.data || {};
};
module.exports = app => {
const router = express.Router({ mergeParams: true, caseSensitive: false });
router.get('/apartmentsByCity/:citySlug', async (req, res) => {
const { citySlug } = req.params;
const { allApartments: apartments } = await queryOrFail(res, QUERY_APARTMENTS_BY_CITY, { citySlug });
apartments.forEach(apartment => {
apartment.id = Number(apartment.id); // eslint-disable-line no-param-reassign
});
const apartmentIds = apartments.map(({ id }) => id);
const [{ rows: images }, { rows: beds }] =...