简体   繁体   English

GraphQL Apollo:在 apollo 客户端查询中未提供变量错误,尽管我认为我设置了变量

[英]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确保你的变量是

  • string "userId": "1" not number "userId": 1字符串"userId": "1"不是数字"userId": 1
  • without $ 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.

相关问题 Apollo客户端Graphql查询变量类型错误 - Apollo client Graphql query variable type error Apollo 客户端和 graphQl 查询错误:变量 '$where' 类型的期望值 'OrganizationWhereUniqueInput - Apollo client and graphQl query error : Variable '$where' expected value of type 'OrganizationWhereUniqueInput 获取所需类型“String”的错误变量“$name”。 没有提供。 在从 React-Apollo 发出 Graphql 请求时 - Getting Error Variable “$name” of required type “String!” was not provided. on firing Graphql request from React-Apollo Apollo GraphQL 变量参数名称 - Apollo GraphQL variable argument name 我如何将状态作为graqh ql查询的变量(Apollo) - How do i have a state as a variable for a graqh ql query (Apollo) 如果我在Apollo Client中使用dataIdFromObject设置唯一标识符,则必须使用graphql的ID类型 - Is graphql's ID type necessary if I've set an unique identifier with dataIdFromObject in Apollo Client 如何为Apollo Client指定GraphQL查询并获取属性出现适当字符串的项目 - How I can specify GraphQL query for Apollo Client and get items which property has an occurrence of the appropriate string 如何在Vanilla JS中使用Apollo Client创建GraphQL订阅 - How do I create a GraphQL subscription with Apollo Client in Vanilla JS 如何在将模式与 Apollo GraphQL 合并时将变量添加到片段 - How can I add a variable to a fragment while merging schemas with Apollo GraphQL 如何在React with Apollo中执行GraphQL Query onClick? - How do I perform GraphQL Query onClick in React with Apollo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM