Dam*_*her 12 graphql graphql-js
我是graphql的新手,我正在努力查询.我想通过他们的电子邮件地址返回用户
我有一个类型定义的调用V1User,它有以下字段id,email,password,role
在此查询中需要更改哪些内容才能根据电子邮件返回用户?
query GetAllV1User {
viewer {
allV1Users{
edges {
node {
id
email
role
createdAt
modifiedAt
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试过这个查询
query getV1UserQuery($email: String!) {
getV1User(email: $email) {
id
email
}
}
Run Code Online (Sandbox Code Playgroud)
有了这些参数
{"email": "test@test.com"}
Run Code Online (Sandbox Code Playgroud)
但是得到以下错误
{
"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"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的架构如下
Name Type Constraints
id ID NonNull Unique
modifiedAt DateTime NonNull
createdAt DateTime NonNull
role String NonNull
password String NonNull
email String NonNull Unique Indexed
Run Code Online (Sandbox Code Playgroud)
谢谢
你好
此查询解决了我的问题
query getUserForEmailAddressAndPassword($where: V1UserWhereArgs) {
viewer {
allV1Users(where: $where) {
edges {
node {
email
id
createdAt
password
modifiedAt
role
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
随着这些查询变量
{"where": {"email": {"eq" : "test@test.com"}, "password": {"eq":"te2st"}}}
Run Code Online (Sandbox Code Playgroud)
Asm*_*fri 15
您可以通过使用 where 子句和比较运算符来完成此操作。
query {
authors (where: {articles: {rating: {_gt: 4}}}) {
id
name
articles (where: {rating: {_gt: 4}}) {
id
title
rating
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不建议在过滤器子句中使用字符串“where”。不要试图模仿 SQL。你想用 where 子句过滤什么。如果它是电子邮件地址,则架构中的查询应包含用户作为字段和电子邮件作为该字段的参数。所以你发送的第一个例子是正确的方法。此外,避免使用诸如 getUsers 或 getUser 之类的动词来声明查询。模式应该只使用名词声明查询
Query
{
Users(email:String):[User!]
}
type User
{
id
email
createdAt
otherStuff
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11082 次 |
最近记录: |