2016-11-01 7 views
1

I次のクエリを持っている:Apollo GraphQL:apolloClient.queryを使用してクエリを2回呼び出す

client.query({ query: query1 }) 
client.query({ query: query2 }) 
Apollo docsから

const GET_MY_USERINFOFORIMS_QUERY = gql` 
query($userID: String!){ 
    myUserDataForIMs(userID:userID){ 
    name_first 
    name_last 
    picture_medium 
    } 
} `; 

const withUserInfoForIMs = graphql(GET_MY_USERINFOFORIMS_QUERY, { 
    options({ userID }) { 
     return { 
      variables: { userID: `${userID}`} 
     }; 
    } 
    , 
    props({ data: { loading, myUserDataForIMs } }) { 
     return { loading, myUserDataForIMs }; 
    }, 
    name: 'GET_MY_USERINFOFORIMS_QUERY', 
}); 

、それは私がこのような何かをやって、apolloClient.queryを使用して、コンポーネントの内部から2回このクエリを呼び出すことができるかもしれように見えます

毎回異なるユーザーIDを渡してクエリを2回呼び出す方法はありますか?

答えて

1

見つけました。 :)

 const localThis = this; 
     this.props.ApolloClientWithSubscribeEnabled.query({ 
      query: GET_MY_USERINFOFORIMS_QUERY, 
      variables: {userID: fromID}, 
     }).then((result) => { 
      localThis.setState({ fromAvatar: result.data.myUserDataForIMs[0].picture_thumbnail }); 
     }); 

     this.props.ApolloClientWithSubscribeEnabled.query({ 
      query: GET_MY_USERINFOFORIMS_QUERY, 
      variables: {userID: toID}, 
     }).then((result) => { 
      localThis.setState({ toAvatar: result.data.myUserDataForIMs[0].picture_thumbnail }); 
     }); 

さらに効率的な方法がある場合は、投稿してください。

0

あなたはデータと一緒にあなたのコンポーネントの小道具にアポロのrefetch()メソッドを渡すことによってこれを行うことができます。そして、どこかのコンポーネントでは、あなたが「手動」のデータを再フェッチすることができます...

const withUserInfoForIMs = graphql(GET_MY_USERINFOFORIMS_QUERY, { 
    options({ userID }) { 
     return { 
      variables: { userID: `${userID}`} 
     }; 
    }, 
    props({ data: { refetch, loading, myUserDataForIMs } }) { 
     return { refetch, loading, myUserDataForIMs }; 
    }, 
    name: 'GET_MY_USERINFOFORIMS_QUERY', 
}); 

theUserWasChangedSomehow(userID) { 
    this.props.refetch({ userID }); 
} 
関連する問題