[英]GraphQL field of type graphql object within an array
我在這里定義了一個graphQL對象:
const graphql = require('graphql');
const { GraphQLObjectType } = require('graphql');
const ProductType = new GraphQLObjectType({
name: 'Product',
description: 'Product GraphQL Object Schemal Model',
fields: {
productId: { type: graphql.GraphQLString },
SKU: { type: graphql.GraphQLString },
quantity: { type: graphql.GraphQLFloat },
unitaryPrice: { type: graphql.GraphQLFloat },
subTotal: { type: graphql.GraphQLFloat },
discount: { type: graphql.GraphQLFloat },
totalBeforeTax: { type: graphql.GraphQLFloat },
isTaxAplicable: { type: graphql.GraphQLBoolean },
unitaryTax: { type: graphql.GraphQLFloat },
totalTax: { type: graphql.GraphQLFloat }
}
});
module.exports.ProductType = { ProductType };
然后,我想在另一個GraphQL對象中使用此ProductType,但是我需要將該對象作為數組結構,如下所示:
const graphql = require('graphql');
const { GraphQLObjectType } = require('graphql');
const { ProductType } = require('./product');
const ShoppingCartType = new GraphQLObjectType({
name: 'ShoppingCart',
description: 'Shopping Cart GraphQL Object Schema Model',
fields: {
cartId: { type: graphql.GraphQLString },
userId: { type: graphql.GraphQLString },
products: [{ type: ProductType }]
}
});
module.exports.ShoppingCartType = { ShoppingCartType };
這可能嗎?
您需要使用GraphQLList包裝器包裝現有類型,如下所示:
products: { type: new GraphQLList(ProductType) }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.