数组中类型为graphql的对象的GraphQL字段 [英] GraphQL field of type graphql object within an array

查看:54
本文介绍了数组中类型为graphql的对象的GraphQL字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里定义了一个graphQL对象:

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

然后,我想在另一个GraphQL对象中使用此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 };

这可能吗?

推荐答案

您需要使用 GraphQLList 包装器,如下所示:

You need to wrap your existing type using the GraphQLList wrapper, like so:

products: { type: new GraphQLList(ProductType) }

这篇关于数组中类型为graphql的对象的GraphQL字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆