问题描述
在登录突变之后,返回JWT令牌和用户对象(ID和FirstName)并存储在缓存中(如下面的图像所示).
我想通过使用RoadFragment来从缓存中检索用户信息并将其传递给布局组件.
所以我试图在我的布局组件中使用读片段:class Layout extends Component { render() { const test = this.props.client.readFragment({ id: 1, // `id` is any id that could be returned by `dataIdFromObject`. fragment: gql` fragment user on User { id firstName } ` }); return ( <div> <AppBar title="myApp" /> <Sidebar // firstName={this.props.data.firstName} /> {this.props.children} </div> ); } } export default withApollo(withRouter(Layout));
此处的问题是我不知道如何通过ReadFragment结果传递数据(当我尝试控制台时"测试"结果未定义). 但是当我在片段查询中添加非现有字段时,我有以下消息:
此错误是正常的,但证明了ReadFragment可以读取用户信息.
所以如何在我的布局组件中使用来自ReadFragment的数据?
感谢您的帮助.
编辑: 这是我的apolroclient配置:
const httpLink = createHttpLink({ uri: "http://127.0.0.1/graphql" }); const errorLink = onError(({ graphQLErrors, networkError }) => { if (graphQLErrors) history.push("/erreur", { errors: graphQLErrors }); if (networkError) history.push("/erreur", { errors: networkError }); }); const authLink = setContext((_, { headers }) => { // get the authentication token from local storage if it exists const token = localStorage.getItem("token"); // return the headers to the context so httpLink can read them return { headers: { ...headers, authorization: token ? `Bearer ${token}` : "" } }; }); const link = ApolloLink.from([errorLink, authLink, httpLink]); const client = new ApolloClient({ link, cache: new InMemoryCache({ dataIdFromObject: object => object.id }) });
推荐答案
最后它现在正在运作.我已经更新了所有的NPM依赖关系,似乎是问题的来源. 对于那些有兴趣的人,这是代码:
const data = this.props.client.readFragment({ id: 1, // `id` is any id that could be returned by `dataIdFromObject`. fragment: gql` fragment user on User { id firstName } ` }); if(data) console.log(data.id);
此处的缺点是您需要DataIdfromObject返回的ID,在我的情况下是用户ID.
如果要使用您的数据,则只需控制数据变量而不是null,然后您可以使用您需要显示的信息.
问题描述
After a login mutation, a jwt token and a user object (id and firstName) are returned and stored in cache (as shown in the image below).
I want to retrieve the user informations from the cache by using readFragment and pass it to Layout component.
So I've tried to use read Fragment in my Layout component like this :
class Layout extends Component { render() { const test = this.props.client.readFragment({ id: 1, // `id` is any id that could be returned by `dataIdFromObject`. fragment: gql` fragment user on User { id firstName } ` }); return ( <div> <AppBar title="myApp" /> <Sidebar // firstName={this.props.data.firstName} /> {this.props.children} </div> ); } } export default withApollo(withRouter(Layout));
The issue here is that I don't know how to pass data from readFragment result (when I try to console.log "test" the result is undefined). But when I add a non existing field in the fragment query I have the following message :
This error is normal but proves that readFragment can read the user informations.
So how to use data from readFragment in my Layout component ?
Thanks for your help.
EDIT : Here is my ApolloClient configuration :
const httpLink = createHttpLink({ uri: "http://127.0.0.1/graphql" }); const errorLink = onError(({ graphQLErrors, networkError }) => { if (graphQLErrors) history.push("/erreur", { errors: graphQLErrors }); if (networkError) history.push("/erreur", { errors: networkError }); }); const authLink = setContext((_, { headers }) => { // get the authentication token from local storage if it exists const token = localStorage.getItem("token"); // return the headers to the context so httpLink can read them return { headers: { ...headers, authorization: token ? `Bearer ${token}` : "" } }; }); const link = ApolloLink.from([errorLink, authLink, httpLink]); const client = new ApolloClient({ link, cache: new InMemoryCache({ dataIdFromObject: object => object.id }) });
推荐答案
Finally it's working now. I've updated all my npm dependencies and it seems that it was the source of the issue. For those that are interested, here is the code :
const data = this.props.client.readFragment({ id: 1, // `id` is any id that could be returned by `dataIdFromObject`. fragment: gql` fragment user on User { id firstName } ` }); if(data) console.log(data.id);
The drawback here is that you need the id returned by dataIdFromObject, in my case it's the user id.
If you want to use your data you have just to control that the data variable is not null and then you can use the informations that you need to display.