[graphQL] watchQuery

holang_2i·2020년 5월 29일
0
post-thumbnail

그래프큐엘로 개발을 하는데 또 문제가 발생했다. update 를 하고 쿼리를 불러오는데 update 되기 전의 결과를 불러왔다.

똑같은 함수 똑같은 쿼리로 rest 는 잘 불러오는데, graphql 로 하면 왜 update 전 결과를 불러오나싶어서 혼자 고민......했다....

원인은 바로 apollo graphql에서 쿼리를 불러올 때가 문제였다.

나는 update를 해주고

await this.apollo.query ({
      query: selectCuAiListQuery,
      variables: { state: state }

    }).subscribe(({ data }) => {
   		console.log(data)
   }

이런식으로 쿼리를 조회했는데 바로 이게 문제였던거다 ㅎㅎ

https://stackoverflow.com/questions/49618392/when-to-use-watchquery-or-query-in-apollo-angular

이걸 참고해보면 .query 는 단순히 한번만 GET 하는 형식이라 볼 수 있다. 반면 .watchQuery는 update 되거나 create mutation 이 일어날때마다 지속적으로 계속 지켜본다? 감시한다? 이런 의미다 업데이트 된 결과값을 계속 리턴해줌!

 await this.apollo.watchQuery ({
      query: selectCuAiListQuery,
      variables: { state: state }
      
    }).valueChanges.subscribe(({ data }) => {
      console.log(data)
    });

요렇게 .watchQuery로 불러오고

const result = await this.apollo.mutate({
        mutation: updateCouponUploadAIMutation,
        variables: {
            data, cu_idx
        },
        refetchQueries:[{
          query: selectCuAiListQuery,
          variables: { state: state }
        }]
      }).toPromise();

mutation 되면 refetchQueries 로 다시 불러와주면 아주 편하다 바로바로 업데이트 된 결과를 리턴해줌 ㅠㅠ

알다가도 더 알아야되는 graphql...

profile
랑호

0개의 댓글