亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

GraphQL 不允許查詢不同的字段

GraphQL 不允許查詢不同的字段

守候你守候我 2022-05-26 17:37:14
我是 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 languagevar 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 endpointvar 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 只能定義一次。
查看完整描述

1 回答

?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

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


person(personId: Int, name: String)

或創建不同名稱的字段


personById(id: Int!)

personByName(name: String!)


查看完整回答
反對 回復 2022-05-26
  • 1 回答
  • 0 關注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號