[英]GraphQL Apollo: Variable was not provided error in apollo client query, although I think I set the variable
What is wrong with this basic apolloClient query?这个基本的 apolloClient 查询有什么问题?
import gql from 'graphql-tag'
export default (context, apolloClient, userId) => (
apolloClient.query({
query: gql`
query RootQueryType($userId: ID!) {
verifyUser(
id: $userId
) {
id,
token
}
},
`,
options: {
variables: { userId: userId }
}
}).then(({ data }) => {
return { loggedInUser: data }
}).catch((error) => {
console.log(error)
return { loggedInUser: {} }
})
)
I do get the error Error: GraphQL error: Variable "$userId" of required type "ID." was not provided.
我确实收到错误Error: GraphQL error: Variable "$userId" of required type "ID." was not provided.
Error: GraphQL error: Variable "$userId" of required type "ID." was not provided.
. . But I set the options and variables with this data.但是我用这些数据设置了选项和变量。 I don't see, what I am doing wrong.我不明白,我做错了什么。
The first userID should be id instead of userID , it should also be defined as id in the Apollo server to match your query.. 第一个userID应该是id而不是userID ,还应该在Apollo服务器中将其定义为id以匹配您的查询。
Change: 更改:
options: { variables: { userId: userId } }
To: 至:
options: {
variables: { id: userId }
}
I think you have the problem in the query definition: 我认为您在查询定义中存在问题:
gql ` query RootQueryType($userId: ID!) { verifyUser( userId: $userId // here you need the parameter to be userId ) { id, token } }`
I came across this issue.我遇到了这个问题。 Make sure your variable is确保你的变量是
"userId": "1"
not number "userId": 1
字符串"userId": "1"
不是数字"userId": 1
$
sign "userId": "1"
not "$userId": "1"
also not userId
没有$
符号"userId": "1"
不是"$userId": "1"
也不userId
I was setting as $userId: 1
but correct one was "userId": "1"
我设置为$userId: 1
但正确的是"userId": "1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.