简体   繁体   English

GraphQL 不允许查询不同的字段

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

I'm an absolute beginner in GraphQL.我是 GraphQL 的绝对初学者。 So apologies if the question is amateur.如果问题是业余的,那么很抱歉。 I'm writing this graphQL end point where it will filter person by name.我正在编写这个 graphQL 端点,它将按名称过滤人员。 Works good, but I want to be able to效果很好,但我希望能够

filter by multiple parameters like name, age, phone number按姓名、年龄、电话号码等多个参数过滤

or或者

use a combination of the parameters to search..使用参数的组合来搜索..

My current program:我目前的程序:

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");

Tried doing this:尝试这样做:

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

But it says Person can be defined only once.但它说 Person 只能定义一次。

You cannot "overload" a field definition.您不能“重载”字段定义。 You can either define a single field with nullable arguments您可以使用可为空参数定义单个字段

person(personId: Int, name: String)

or create differently named fields或创建不同命名的字段

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM