Javascript 如何从AWS Auth.currentSession()返回的承诺中获取返回值
我正在尝试从AWS获得的jwt令牌创建一个graphql客户端 我想要的:在varJavascript 如何从AWS Auth.currentSession()返回的承诺中获取返回值,javascript,react-native,apollo-client,Javascript,React Native,Apollo Client,我正在尝试从AWS获得的jwt令牌创建一个graphql客户端 我想要的:在varListfulClient listfulClient ApolloClient { "cache": InMemoryCache { "addTypename": true, "config": Object { "addTypename": true, "dataIdFromOb
ListfulClient
listfulClient ApolloClient {
"cache": InMemoryCache {
"addTypename": true,
"config": Object {
"addTypename": true,
"dataIdFromObject": [Function defaultDataIdFromObject],
"resultCaching": true,
"typePolicies": Object {},
},
"data": Root {
"canRead": [Function anonymous],
"data": Object {},
我得到的是:一个承诺
listfulClient Promise {
"_U": 0,
"_V": 0,
"_W": null,
"_X": null,
}
aws amplify库中的函数Auth.currentSession()
似乎返回了一个承诺。但是,我似乎无法解析该值以生成所需的apollo客户端。我尝试将我的函数gqlClient
转换为一个异步函数,但当我尝试在gqlClient中使用它时,甚至当我将函数设置为异步时,也会出现错误意外的保留字“wait”。
。
你能帮帮我吗?我为此奋斗了几个小时
function gqlClient() {
// return the promise from currentSession and
// return the client with token data if
// the user is logged in
return Auth.currentSession().then(function(data) {
// Set the appropriate headers for the environment
const jwt = data.accessToken.jwtToken;
const headers = {};
const authLink = setContext((_, { headers }) => ({
headers: {
...headers,
authorization: jwt ? `Bearer ${jwt}` : "",
},
}));
const httpLink = createHttpLink({
uri: 'http://localhost:8080/query',
});
return new ApolloClient({
connectToDevTools: true,
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
});
})
.catch(function(err){
// something def went wrong
console.log(err);
return new ApolloClient({cache: new InMemoryCache(),});
});
};
我想创建apollo客户端,它将在我的react组件中使用
function App() {
// retrieve current session
var ListfulClient = gqlClient();
console.log("listfulClient", ListfulClient)
let [fontsLoaded] = useFonts({
Arimo_400Regular,
Arimo_400Regular_Italic,
Arimo_700Bold,
Arimo_700Bold_Italic,
});
if (!fontsLoaded) {
return <AppLoading />;
} else {
return (
<ApolloProvider client={ListfulClient}>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Listful" options={{ headerShown: false }} component={HomeTabs} />
<Stack.Screen name="Settings" component={SettingsScreen} />
</Stack.Navigator>
</NavigationContainer>
</ApolloProvider>
);
}
}
函数应用程序(){
//检索当前会话
var ListfulClient=gqlClient();
log(“listfulClient”,listfulClient)
让[fontsLoaded]=使用字体({
Arimo_,
Arimo_400常规_斜体,
阿里莫乌,
Arimo_700;粗体_斜体,
});
如果(!fontsLoaded){
返回;
}否则{
返回(
);
}
}
您必须这样做,
对auth-link使用异步函数并从那里传递令牌。并通过链接将其传递给客户端
const httpLink = createHttpLink({
uri: 'http://localhost:8080/query',
});
const authLink = setContext(async (_, { headers }) => {
const data = await Auth.currentSession();
const jwt = data.accessToken.jwtToken;
return {
headers: {
...headers,
authorization: jwt ? `Bearer ${jwt}` : "",
},
};
});
const client=new ApolloClient({
connectToDevTools: true,
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
});
在App.js中
var ListfulClient = client;
这将确保令牌在每个请求中都被传递。这很有效!你能解释一下这部分代码吗,我不知道这里发生了什么:
constauthlink=setContext(async({headers})=>{
,所以基本上这将调用一个asyn函数,它是每次从客户端发送请求时调用getAuth的函数。