简体   繁体   中英

GraphQL field of type graphql object within an array

I have a graphQL object defined here:

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 };

And then, I want to use this ProductType inside another GraphQL object, but I need that object to be an array structure, something like this:

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 };

Is this possible?

您需要使用GraphQLList包装器包装现有类型,如下所示:

products: { type: new GraphQLList(ProductType) }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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