Warm tip: This article is reproduced from stackoverflow.com, please click
graphql typescript

How do I insert id into GraphQl query from a .ts method

发布于 2020-04-07 10:19:51

I have a method that that gets data from a GraphQl query. What I am trying to do is pass the variables token and passID into the graphQL query.

saveData ( auditid: any ) {
  this.storage.get('AccessToken').then((_token) => { 
    var passID = auditid.toString();
    var token = _token;

    this.apollo.watchQuery(
      {      
      query: gql`
      {
        gechecklist( id:$passID, accesstoken:$token )
        {
          Type,Name,
          Sections
        }
          ,ID
        }
      }
    `,
      fetchPolicy: 'cache-first',
    })
    .valueChanges.subscribe(( result: ApolloQueryResult<any> ) => {
      let sections = result.data.gechecklist[0].Sections;
      this.storage.set( name, JSON.stringify( sections )).then(() => {
      this.listKeys();
      });
    });
  });
}
Questioner
Thomas Degroot
Viewed
59
jaws 2020-02-01 01:46

watchQuery takes a variables parameter. See the query documentation.

var passID = auditid.toString();
var token = _token;

this.apollo.watchQuery({      
  query: gql`{
    gechecklist( id:$passID, accesstoken:$token )
    {
      Type,Name,
      Sections
    }
      ,ID
    }
  }`,
  variables: { passID, token },
  fetchPolicy: 'cache-first',
})