GraphQL阿波罗:变量未提供阿波罗客户端查询错误,虽然我觉得我设置变量

问题描述 投票:0回答:2

什么是错的这个基本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: {} }
  })
)

我得到的错误Error: GraphQL error: Variable "$userId" of required type "ID!" was not provided.。但我设置的选项和变量与此数据。我不知道,我做错了。

javascript graphql apollo-client
2个回答
1
投票

第一个用户ID应该是ID,而不是用户ID,还应该定义为阿波罗服务器,以符合您查询ID ..

更改:

options: {
  variables: { userId: userId }
}

至:

options: {
  variables: { id: userId }
}

0
投票

我认为你必须在查询定义的问题:

gql `
query RootQueryType($userId: ID!) {
verifyUser(
userId: $userId // here you need the parameter to be userId
) {
    id,
    token
  }
}`
© www.soinside.com 2019 - 2024. All rights reserved.