簡體   English   中英

GraphQL 不允許查詢不同的字段

[英]GraphQL doesn't allow querying on different fields

我是 GraphQL 的絕對初學者。 如果問題是業余的,那么很抱歉。 我正在編寫這個 graphQL 端點,它將按名稱過濾人員。 效果很好,但我希望能夠

按姓名、年齡、電話號碼等多個參數過濾

或者

使用參數的組合來搜索..

我目前的程序:

var express = require("express");
var express_graphql = require("express-graphql");
var { buildSchema } = require("graphql");
var Sequelize = require("sequelize")
var db = new Sequelize(
  "users",
  "root",
  "pass",
  {
    host: "localhost",
    dialect: "mysql"
  }
);

var calls = db.define(
  "calls",
  {
    person_id: {
      type: Sequelize.INTEGER,
      allowNull: false,
      primaryKey: true
    },
    name: {
      type: Sequelize.STRING,
      allowNull: false
    },
    age: {
      type: Sequelize.INTEGER,
      allowNull: false
    },
    address: {
      type: Sequelize.STRING,
      allowNull: false
    },
    company: {
      type: Sequelize.String,
      allowNull: false
    },
    email: {
      type: Sequelize.STRING,
      allowNull: false
    }
  },
  {
    tableName: "person"
  }
);

// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
  type Query {
    Person(person_id: Integer!): [Person]
  }
  type Person {
    person_id: Integer
    name: String
    age: Integer
    address: String
    company: String
    email: String 
  }
`);

var getperson = function(args) {
  if (args.person_id) {
    var p_name = args.person_id;
    return calls.findAll({
      where: {
        name: p_name
      }
    });
  }
};

// The root provides a resolver function for each API endpoint
var root = {
  person: getperson
};

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

嘗試這樣做:

 type Query {
    Person(person_id: Integer!): [Person]
    Person(name: String!): [Person]
  }

但它說 Person 只能定義一次。

您不能“重載”字段定義。 您可以使用可為空參數定義單個字段

person(personId: Int, name: String)

或創建不同命名的字段

personById(id: Int!)
personByName(name: String!)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM