2017-11-13 5 views
0

nicht auflösen Wenn ich den folgenden Code in Node-Server ausführen. Ich sehe einen Fehler, dass graphql-JS das Feld "product.productTax" nicht auflösen kann.graphql-JS Express kann ein Feld

Mit graphql-express, Code Visual Studio, ich habe versucht, den folgenden Code aus 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'); 

Antwort Graphiql Response

Antwort

1

In Ihrem Schema Sie haben

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

so graphql ist ex pecting ein Array von Float-Werten. Es scheint, dass es

productTax(tax_rate: Float!): Float! 

seit Ihrem productTax() Methode ein Float-Wert zurückkehrt sein sollte.

Sie können auch den Standardsteuersatz hinzufügen. Ich sehe nicht, dass Sie es trotzdem passieren, also

productTax(tax_rate: Float = 20): Float! 
Verwandte Themen