在访问Server中的第三级子级时,GraphQL返回null。当我将静态数据推送到客户端时,它可以工作,但是当我返回动态数据时,它返回null。
当我在本地上运行它时,它可以正常工作(通过本地数据库连接),但是服务器中的相同代码无法正常工作。
堆栈版本:
节点版本:10
NPM版本:
“ apollo-server”:“ ^ 2.3.2”,
“ apollo-server-express”:“ ^ 2.3.2”,
“ graphql”:“ ^ 14.1.1”,
下面是fetchSingleHomepage函数(后端代码)
exports.fetchSingleHomepage = async function (context) {
return new Promise((resolve, reject) => {
let query = { isDefault: true }
if (context && context.city) { query = { city: context.city } }
Homepages.findOne(query, async (err, homepage) => {
if (err) { resolve(); }
else if (homepage) {
homepage.city.name = (context && context.language && context.language == 'en') ? homepage.city.name_en : homepage.city.name_ar;
homepage.city.country.name = (context && context.language && context.language == 'en') ? homepage.city.country.name_en : homepage.city.country.name_ar;
let banners = [];
let newSections = [];
homepage.sections = await homepage.sections.map(async (section) => {
section.title = (context && context.language && context.language == 'en') ? section.title_en : section.title_ar;
section.image = (context && context.language && context.language == 'en') ? (Helpers.getOptimizationImageURl(section.image_en.Location)) : (Helpers.getOptimizationImageURl(section.image_ar.Location));
section.image_en = Helpers.getOptimizationImageURl(section.image_en.Location);
section.image_ar = Helpers.getOptimizationImageURl(section.image_ar.Location);
section.image_sizes = await Helpers.getImageSizes(section.image);
if (section.products && section.products.length) {
section.products = section.products.map(async (product) => {
return await Helpers.formatProduct(product, context.language).then((response) => { return response; });
})
}
if (section.category) {
section.category.name = (context && context.language && context.language == 'en') ? section.category.name_en : section.category.name_ar;
if (typeof section.category.image == 'object') { section.category.image = Helpers.getOptimizationImageURl(section.category.image.Location); }
else if (typeof section.category.image == 'string') { section.category.image = Helpers.getOptimizationImageURl(section.category.image) }
else { section.category.image = ''; }
section.category.image_sizes = await Helpers.getImageSizes(section.category.image);
}
if (section.event) {
section.event.name = (context && context.language && context.language == 'en') ? section.event.name_en : section.event.name_ar;
if (section.event.coverImage && typeof section.event.coverImage == 'object') { section.event.coverImage = section.event.coverImage.Location; }
else if (!section.event.image) { section.event.image = ''; }
section.event.image_sizes = await Helpers.getImageSizes(section.event.image);
section.event.coverImage_sizes = await Helpers.getImageSizes(section.event.coverImage);
}
return section;
})
homepage.sections.forEach((section) => {
if (section.isBanner) { banners.push(section); }
else if (section.isSection) { newSections.push(section); }
})
homepage.sections = newSections;
homepage.banners = banners;
resolve(homepage);
}
else { resolve(); }
}).populate({
path: 'city',
populate: {
path: 'country'
}
}).populate({
path: 'sections',
options: { sort: { weight: 1 } },
populate: [{
path: 'category',
},
{
path: 'event',
},
{
path: 'products',
populate: [{
path: 'category_id'
},
{
path: 'modifier'
},
{
path: 'city_id',
populate: {
path: 'country'
}
},
{
path: 'supplier'
},
{
path: 'brand'
}]
}]
}).exec();
})
}
下面是formatProduct帮助程序功能
exports.formatProduct = function (product, language) {
return new Promise(async (resolve, reject) => {
product.name = language === 'en' ? product.name_en : product.name_ar;
product.description = language === 'en' ? product.description_en : product.description_ar;
product.defaultImage = this.getOptimizationImageURl(product.defaultImage);
if (product.images && product.images.length) {
product.images = product.images.map((image) => {
let productImage = ''
if (image && typeof image === 'object') { productImage = image.Location }
else if (image && typeof image === 'string') { productImage = image }
if (productImage) {
productImage = this.getOptimizationImageURl(productImage);
}
return productImage;
});
}
if (product.defaultImage && product.images && product.images.length) {
product.images.forEach((image) => {
if (image === product.defaultImage) {
let index = product.images.indexOf(product.defaultImage);
if (index > -1) { product.images.splice(index, 1); }
product.images.push(product.defaultImage);
}
})
}
product.images_sizes = await this.getImagesSizes(product.images);
product.defaultImage_sizes = await this.getImageSizes(product.defaultImage);
if (product.category_id && product.category_id.length) {
product.category_id = product.category_id.map(async (category) => {
category.name = language === 'en' ? category.name_en : category.name_ar;
if (category.image && typeof category.image === 'object') {
category.image = category.image.Location;
}
else if (!category.image) {
category.image = '';
}
category.image_sizes = await this.getImageSizes(category.image);
return category
})
}
if (product.city_id && product.city_id.length) {
product.city_id = product.city_id.map((city) => {
city.name = language === 'en' ? city.name_en : city.name_ar;
city.country.name = language === 'en' ? city.country.name_en : city.country.name_ar;
return city;
})
}
product.quantity = 0;
if (product.modifier && product.modifier.length) {
product.modifier = product.modifier.filter(modifier => modifier.isActive === true).map(async (modifier) => {
if (modifier.isDefault) {
product.price = (modifier.isDiscount && modifier.discount > 0) ? modifier.price - modifier.discount : modifier.price;
product.isDiscounted = modifier.isDiscount;
product.discount = modifier.isDiscount ? modifier.discount : 0;
}
modifier.size = language === 'en' ? modifier.size_en : modifier.size_ar;
modifier.out_of_stock = modifier.quantity > 0 ? false : true;
product.quantity = product.quantity + modifier.quantity;
if (modifier.image && typeof modifier.image === 'object') { modifier.image = modifier.image.Location; }
else if (modifier.image && typeof modifier.image === 'string') { modifier.image = modifier.image }
else { modifier.image = '' }
modifier.image_sizes = await this.getImageSizes(modifier.image);
return modifier
})
}
product.out_of_stock = product.quantity > 0 ? false : true;
if (product.product_id && product.product_id.length) {
product.product_id = product.product_id.map((childProduct) => {
childProduct.name = language === 'en' ? childProduct.name_en : childProduct.name_ar;
childProduct.description = language === 'en' ? childProduct.description_en : childProduct.description_ar;
if (childProduct.images && childProduct.images.length) {
childProduct.images = childProduct.images.map((image) => {
if (image && typeof image === 'object') { return image.Location }
else if (image && typeof image === 'string') { return image }
else { return '' }
});
}
return childProduct;
})
}
if (product.brand) {
product.brand.name = language === 'en' ? product.brand.name_en : product.brand.name_ar;
product.brand.description = language === 'en' ? product.brand.description_en : product.brand.description_ar;
if (product.brand.image) {
if (typeof product.brand.image === 'object') { product.brand.image = product.brand.image.Location; }
else if (typeof product.brand.image === 'string') { product.brand.image = product.brand.image; }
else { product.brand.image = ''; }
product.brand.image_sizes = await this.getImageSizes(product.brand.image);
}
}
resolve(product);
})
}
Graphql查询请求
{
fetchSingleHomepage {
sections {
products {
id
images_sizes {
large
}
defaultImage_sizes {
small
}
}
}
}
}
当前回复
{
"data": {
"fetchSingleHomepage": {
"sections": [
{
"products": [
{
"id": "5cdaa658292a2c001819167c",
"name": "شوكولا كارميه",
"name_en": "Choco-Carmey",
"images_sizes": null,
"defaultImage_sizes": null
},
{
"id": "5cc960eedc1d46001804a648",
"name": "اسوارة بوليس",
"name_en": "bracelet- Police ",
"images_sizes": null,
"defaultImage_sizes": null
},
{
"id": "5ccb48a9a00f3a00181b60bc",
"name": "جبر خاطر ",
"name_en": "pureness",
"images_sizes": null,
"defaultImage_sizes": null
},
(在上面的images_sizes和defaultImage_sizes为null,这是意外的)
以下是预期的成功结果。
{
"data": {
"fetchSingleHomepage": {
"sections": [
{
"products": [
{
"id": "5cdaa658292a2c001819167c",
"images_sizes": [
{
"large": "http://dev-bucket.s3.eu-central-1.amazonaws.com/Products/5cdaa658292a2c001819167c/LARGE/L1557833304916"
},
{
"large": "http://dev-bucket.s3.eu-central-1.amazonaws.com/Products/5cdaa658292a2c001819167c/LARGE/L1557833304918"
},
{
"large": "http://dev-bucket.s3.eu-central-1.amazonaws.com/Products/5cdaa658292a2c001819167c/LARGE/L1557833304913"
}
],
"defaultImage_sizes": {
"small": "http://dev-bucket.s3.eu-central-1.amazonaws.com/Products/5cdaa658292a2c001819167c/SMALL/S1557833304913"
}
},