gpt4 book ai didi

javascript - GraphQL 不允许查询不同字段

转载 作者:行者123 更新时间:2023-12-02 21:23:42 24 4
gpt4 key购买 nike

我是 GraphQL 的绝对初学者。如果这个问题是业余的,我很抱歉。我正在编写这个 graphQL 端点,它将按姓名过滤人员。效果很好,但我希望能够

filter by multiple parameters like name, age, phone number

use a combination of the parameters to search..

我当前的计划:

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!)

关于javascript - GraphQL 不允许查询不同字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60791801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com