从graphql中的解析程序访问返回数据

问题描述 投票:0回答:1

我想从我的解析器访问国家/地区字段。查询正在返回该国家/地区,但由于Product是一个列表,因此我只能访问按查询返回的items内部的对象。有什么方法可以访问查询中返回的全部数据,也可以将其作为解析器函数的参数进一步传递给下一个参数

// schema

type ProductCollectionPage {
    items: [Product!]!
}

//解析器

const resolvers = {
    Product: {
        variants: async (obj: any, args: any, { dataSources }: any): Promise<IProductVariantPage> => {
            const { id } = obj;
            // want to access country here
            return (dataSources.xyz as XyzRepository).retriveProducts(country, id);
        }
    },
    Query: {
        products: async (
            obj: any,
            { id }: { id: string },
            { dataSources }: any
        ): Promise<
            any
        > => {

            const locationDetails = await (dataSources.abc as InventoryLocationsRepository).retrieveInventoryLocation(id);
            const country = locationDetails.country;
            const response = await (dataSources.abc as XyzRepository).retriveProductIds(country);
            // response.list === [{id: 1}, {id:2}]
            return {
                country,
                items: response.list
            }
        }
    }
};
graphql
1个回答
0
投票

因为数组是javascript中的对象,然后您只需将其他属性分配给response.list

response.list.country = country;
© www.soinside.com 2019 - 2024. All rights reserved.