简体   繁体   English

graphql-JS Express无法解析字段

[英]graphql-JS Express cannot resolve a field

When I execute the below code in node server. 当我在节点服务器中执行以下代码时。 I see an error that graphql-JS cannot resolve the field "product.productTax." 我看到一个错误,graphql-JS无法解析字段“ product.productTax”。

Using graphql-express, visual studio code, I was trying to exactly mimic the following code from http://graphql.org/graphql-js/object-types/ 使用graphql-express,Visual Studio代码,我试图完全模仿来自http://graphql.org/graphql-js/object-types/的以下代码

<!-- language: lang-js -->
var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');

// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
  type Product{
    product_id: Int!
    sku: String
    short_nm: String
    retl_price: Float!
    productTax(tax_rate: Float!): [Float!]
  }

  type Query {
    getproduct(product_id: Int, sku: String, short_nm: String, retl_price: Float): Product
  }
`);

// This class implements the Product GraphQL type
class Product {

  constructor(product_id, sku, short_nm, retl_price) {
    this.product_id = product_id;
    this.sku = sku;
    this.short_nm = short_nm;
    this.retl_price = retl_price;
  }

  productTax({tax_rate}){
    return this.retl_price * tax_rate / 100;
  }

}

// The root provides the top-level API endpoints
var root = {
  getproduct: function ({product_id, sku, short_nm, retl_price}) {
    return new Product(product_id, sku, short_nm, retl_price);
  }
}

var app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');

Response 响应 图形反应

In your schema you have 在您的架构中

    productTax(tax_rate: Float!): [Float!]

so graphql is expecting an array of float values. 因此graphql期望使用浮点值数组。 It seems that it should be 似乎应该

    productTax(tax_rate: Float!): Float!

since your productTax() method is returning one float value. 因为您的productTax()方法返回一个浮点值。

You can also add default tax rate. 您还可以添加默认税率。 I don't see you are passing it anyway, so 我没看到你通过它,所以

    productTax(tax_rate: Float = 20): Float!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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