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

本文是小编为大家收集整理的关于带有变量的Apollo查询的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

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变量.

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

推荐答案

应该是这样的:

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.query

本文地址:https://www.itbaoku.cn/post/1938059.html

问题描述

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>});
});

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

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

推荐答案

It should be something like this:

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

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

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