Graphql - 如何执行where子句

时间:2018-02-11 12:13:53

标签: graphql graphql-js

我是graphql的新手,我正在努力解决一个问题。 我想通过他们的电子邮件地址返回用户

我有一个类型定义的调用V1User,它有以下字段 ID, 电子邮件, 密码, 作用

此查询需要更改哪些内容才能根据电子邮件返回用户?

    query GetAllV1User {
  viewer {
     allV1Users{
      edges {
        node {
          id
          email
          role
          createdAt
          modifiedAt
        }
      }
    }
  }
}

我试过了这个查询

    query getV1UserQuery($email: String!) {
  getV1User(email: $email) {
    id
    email
  }
}

使用这些参数

{"email": "test@test.com"}

但是得到以下错误

    {
  "errors": [
    {
      "message": "Unknown argument \"email\" on field \"getV1User\" of type \"Query\".",
      "locations": [
        {
          "line": 2,
          "column": 13
        }
      ],
      "name": "GraphQLError"
    },
    {
      "message": "Field \"getV1User\" argument \"id\" of type \"ID!\" is required but not provided.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "name": "GraphQLError"
    }
  ]
}

我的架构如下

Name        Type        Constraints 
id          ID          NonNull Unique  
modifiedAt  DateTime    NonNull 
createdAt   DateTime    NonNull 
role        String      NonNull 
password    String      NonNull 
email       String      NonNull Unique Indexed  

由于

您好

此查询解决了我的问题

query getUserForEmailAddressAndPassword($where: V1UserWhereArgs) {
  viewer {
    allV1Users(where: $where) {
      edges {
        node {
          email
          id
          createdAt
          password
          modifiedAt
          role
        }        
      }
    }
  }
}

连同这些查询变量

{"where": {"email": {"eq" : "test@test.com"}, "password": {"eq":"te2st"}}}

2 个答案:

答案 0 :(得分:0)

我不建议在您的过滤器子句中使用字符串“ where”。不要尝试模仿SQL。您正在尝试使用where子句进行过滤。如果这是电子邮件地址,则架构中的查询应包含user作为字段,而email作为该字段的参数。 因此,您发送的第一个示例是正确的方法。 另外,避免使用动词(如getUsers或getUser)声明查询。模式应仅使用名词声明查询

Query
{
   Users(email:String):[User!]
}

type User 
{
    id
    email
    createdAt
    otherStuff
}

答案 1 :(得分:0)

相关问题