带变量的 Apollo 查询 [英] Apollo Query with Variable
本文介绍了带变量的 Apollo 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
只是一个基本的 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屋!
查看全文