为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 122 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号