功能有效,但控制台提供无关错误:"Variable " $id " of required type " ID!" was not provided."



我的CRUD操作工作正常。但其中许多还会产生一个我无法追踪到的特定控制台错误。

[GraphQL error]: Message: Variable "$id" of required type "ID!" was not provided., Location: [object Object], Path: undefined

示例:我可以在不生成错误的情况下查看用户列表,也可以访问单个用户的"详细信息"视图而不生成错误。这两个在隔离状态下运行良好。但不知何故,从前者到后者的遍历会产生错误。

我在GraphQL Playground中测试了这两个查询(用户和用户),没有收到错误。我还尝试将ID作为类型"String"而不是类型"ID!"。(也没有!)。我尝试了其他将用户id提供给解析器的"破解"方法。我已经尝试在查询解析器中传递其他字段来代替"info"参数。

重申一下,孤立地查看任一页面(即:清除控制台并重新加载任一页面)不会触发错误。这似乎是关于从/usersuser?id=12345的旅程。

来自datamodel.prisma:

type User {
id: ID! @unique
createdAt: DateTime!
updatedAt: DateTime!
name: String!
email: String! @unique
password: String!
resetToken: String
resetTokenExpiry: Float
permissions: Permission
posts: [Post]! @relation(name: "PostsOfUser" onDelete: CASCADE)
comments: [Comment!]! @relation(name: "CommentsOfUser" onDelete: 
CASCADE)

来自schema.graphql中的查询对象:user(id: ID!): User

用户查询解析器:

async user(parent, { id }, { db, request }, info) {
const user = await db.query.user({ where: { id } }, info);
return user;

使用react apollo和graphql标记包的gql查询:

const SINGLE_USER_QUERY = gql`
query SINGLE_USER_QUERY($id: ID!){
user(id: $id) {
id
name
email
permissions
createdAt
updatedAt
}
}
`;

预期行为:用户可以单击用户列表中的单个用户,下面的页面将包含该用户的详细信息。

实际结果:它工作,但也会生成控制台错误:

[GraphQL error]: Message: Variable "$id" of required type "ID!" was 
not provided., Location: [object Object], Path: undefined
index.js:86 
[Network error]: ServerError: Response not successful: Received status code 400
index.js:2178 GraphQL error occurred [getDataFromTree] Error: 
Network error: Response not successful: Received status code 400
at new ApolloError (ApolloError.js:37)
at QueryManager.js:328
at QueryManager.js:700
at Array.forEach (<anonymous>)
at QueryManager.js:699
at Map.forEach (<anonymous>)
at QueryManager.push../node_modules/apollo-client/core/QueryManager.js.QueryManager.broadcastQueries (QueryManager.js:694)
at QueryManager.js:277

请为参数添加一个if条件,并检查它是否为true。然后在其中添加get(REST API)或query(GrahQL)函数,这样就不会显示这个错误。请参阅以下示例

async user(parent, { id }, { db, request }, info) {
if ( id ) {
//enter your code
const user = await db.query.user({ where: { id } }, info);
return user;
}
}

相关内容

最新更新