带变量的Apollo查询

时间:2018-07-25 15:40:55

标签: graphql apollo react-apollo

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

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上找不到解决方案-如果有人可以帮助我,那就太好了:)

1 个答案:

答案 0 :(得分:9)

应该是这样的:

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