【问题标题】:GraphQL field of type graphql object within an array数组中类型为 graphql 对象的 GraphQL 字段
【发布时间】:2018-08-15 02:04:30
【问题描述】:

我在这里定义了一个 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 };

这可能吗?

【问题讨论】:

    标签: node.js graphql graphql-js


    【解决方案1】:

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

    products: { type: new GraphQLList(ProductType) }
    

    【讨论】:

      猜你喜欢
      • 2018-04-07
      • 2019-06-24
      • 1970-01-01
      • 2019-10-15
      • 2020-05-29
      • 2019-07-09
      • 1970-01-01
      • 2019-05-17
      • 2020-09-16
      相关资源
      最近更新 更多