【问题标题】:Trying to pass parameter with graph ql query getting an error尝试使用 graphql 查询传递参数时出错
【发布时间】:2019-12-11 11:19:44
【问题描述】:

我有下面的 graphl api 查询,我使用它从反应前端应用程序发送数据和对象以获得所需的结果

    {
  allSectionRequests(data:{
    requestStageName:"Request Submitted"
  }){
    section
    type
   }
 }

然后我尝试从下面的反应中传递变量

     export const GET_SECTIONREQUESTS = gql`
query AllSectionRequests($data: sectionRequestParamsInput){
    allSectionRequests(data: $data){
    section
    type       
  }
 }
`;

我已经附上了我需要发送到下面的graphql api的图像

下面是我将在组件内调用查询然后将data对象传递给graphql api的反应代码

  const { data: dashBoardData, loading: dashBoardDataLoading, error: dashBoardDataError } = useQuery(GET_SECTIONREQUESTS, {
variables: { data: { requestStageName: 'Request Submitted' } },
});

我在下面遇到这样的错误

The variable **data** type is not compatible with the type of the argument **data**.
↵Expected type: SectionRequestParamsInput.

我不确定这段代码哪里做错了,谁能帮忙解决这个问题。

非常感谢

【问题讨论】:

  • SectionRequestParamsInput 是什么?
  • graphql api 期望的输入对象
  • 在您的查询中sectionRequestParamsInput 以小写s 开头;大写有帮助吗?

标签: javascript reactjs graphql react-apollo apollo-client


【解决方案1】:

我已通过以下解决方案纠正了我的问题

 export const GET_SECTIONREQUESTS = gql`
   query AllSectionRequests($sectionRequestParamsInput: SectionRequestParamsInput){
    allSectionRequests(data: $sectionRequestParamsInput){
    id
    section
    type
    createdBy
    description
    status
    age    
  }
 }
`;

然后像这样改变我的输入参数

 const { data: dashBoardData, loading: dashBoardDataLoading, error: dashBoardDataError } = useQuery(GET_SECTIONREQUESTS, {
variables: { sectionRequestParamsInput: { requestStageName: 'Request Submitted' } },
});

我希望这对任何正在寻找带有传入参数的 graphql api 查询的人有所帮助。

【讨论】:

    猜你喜欢
    • 2019-02-24
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2023-01-26
    • 2021-12-12
    • 2018-04-10
    • 1970-01-01
    • 2020-08-01
    相关资源
    最近更新 更多