繁体   English   中英

数组中类型为graphql的对象的GraphQL字段

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM