我在GraphQL结果中返回查询名称的原因是什么?
将makeExecutableSchema与以下查询定义一起使用:我在GraphQL结果中返回查询名称的原因是什么?,graphql,graphql-js,apollo,apollo-server,Graphql,Graphql Js,Apollo,Apollo Server,将makeExecutableSchema与以下查询定义一起使用: # Interface for simple presence in front-end. type AccountType { email: Email! firstName: String! lastName: String! } # The Root Query type Query { # Get's the account per ID or with an authToken.
# Interface for simple presence in front-end.
type AccountType {
email: Email!
firstName: String!
lastName: String!
}
# The Root Query
type Query {
# Get's the account per ID or with an authToken.
getAccount(
email: Email
) : AccountType!
}
schema {
query: Query
}
和以下解析器:
export default {
Query: {
async getAccount(_, {email}, { authToken }) {
/**
* Authentication
*/
//const user = security.requireAuth(authToken)
/**
* Resolution
*/
const account = await accounts.find({email})
if (account.length !== 1) {
throw new GraphQLError('No account was found with the given email.', GraphQLError.codes.GRAPHQL_NOT_FOUND)
}
return account
}
}
}
当我查询时:
query {
getAccount(email: "test@testing.com") {
firstName
lastName
}
}
我在GraphiQL中得到以下结果:
{
"data": {
"getAccount": {
"firstName": "John",
"lastName": "Doe"
}
}
}
那么,我为什么要在结果中返回这个getAccount呢?因为getAccount不是查询名称。它只是根查询类型查询中的一个常规字段
结果与查询的形状完全相同是GraphQL的核心设计原则之一:
网站截图
GraphQL中的查询名称位于查询关键字之后:
好的,这是有道理的,但提出了另一个问题,即如何使用makeExecutableSchema创建多个查询名称。现在,我只是污染了后端的根查询。你会怎么做?或者您应该将查询定义为AccountType的一部分,在本例中是AccountType?事实上。我只是尝试通过扩展另一个用于扩展查询的文件中的extend将查询拉入类型定义来测试这一点,但在type\query\上得到了Cannot query field\getAccount\。尝试查询帐户{getAccountemail:test@testing.com{firstName lastName}`
query myQueryName {
getAccount(email: "test@testing.com") {
firstName
lastName
}
}