Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo Query with Variable

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:)

like image 265
Christian Weisenburger Avatar asked Jul 25 '18 15:07

Christian Weisenburger


People also ask

How do you query using Apollo client?

Executing a query To run a query within a React component, call useQuery and pass it a GraphQL query string. When your component renders, useQuery returns an object from Apollo Client that contains loading , error , and data properties you can use to render your UI.


1 Answers

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

like image 108
Mikael Lirbank Avatar answered Oct 19 '22 02:10

Mikael Lirbank