带变量的Apollo查询 [英] Apollo Query with Variable

查看:132
本文介绍了带变量的Apollo查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是基本的阿波罗查询请求

Just a basic apollo query request

this.client.query({
  query: gql`
    {
      User(okta: $okta){
        id
      }
    }`
}).then(result => {
  this.setState({userid: result.data.User});
  console.log(this.state.userid.id)
}).catch(error => {
  this.setState({error: <Alert color="danger">Error</Alert>});
});

问题是,如何/在何处设置$ okta变量.

The question is, how/where to set the $okta variable.

在Stackoverflow或Google上找不到解决方案-如果有人可以帮助我,那就太好了:)

Didn't find a solution on Stackoverflow or Google - would be great if someone could help me:)

推荐答案

应该是这样的:

const query = gql`
  query User($okta: String) {
    User(okta: $okta){
      id
    }
  }
`;

client.query({
  query: query,
  variables: {
    okta: 'some string'
  }
})

有关Apollo客户端的文档以及所有详细信息,可以在以下位置找到: https://www.apollographql.com/docs/react/api/apollo-client.html#ApolloClient.query

The documentation for Apollo client with all the details can be found here: https://www.apollographql.com/docs/react/api/apollo-client.html#ApolloClient.query

这篇关于带变量的Apollo查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆