[英]ApolloProvider useQuery combination does not fetch data from AWS AppSync, in React project
由於兼容問題,我使用 ApolloClient 來構建我的 AWS AppSync API 客戶端,這樣我就可以使用 ApolloProvider 將客戶端傳遞給不同的子組件。
在子組件中,我調用 useQuery 來獲取數據,但問題是 output 總是給我默認的“未定義”值。
import awsconfig from '../aws-exports';
import { AUTH_TYPE } from 'aws-appsync';
import {
ApolloProvider,
ApolloClient,
ApolloLink,
HttpLink,
InMemoryCache,
} from "@apollo/client";
import { createAuthLink } from "aws-appsync-auth-link";
import { getUserDb, listUserDbs } from '../graphql/queries';
const link = ApolloLink.from([
createAuthLink({
url: awsconfig.aws_appsync_graphqlEndpoint,
region: awsconfig.aws_appsync_region,
auth: {
type: AUTH_TYPE.API_KEY,
apiKey: awsconfig.aws_appsync_apiKey,
},
}),
new HttpLink({ uri: awsconfig.aws_appsync_graphqlEndpoint }),
]);
const client = new ApolloClient({
link,
cache: new InMemoryCache(),
credentials: 'include'
});
function WithProvider (){
return (
<ApolloProvider client={client}>
<App />
</ApolloProvider>
)
}
export default WithProvider;
import { listUserDbs } from '../graphql/queries';
const GET_USER_PROFILE = gql(listUserDbs)
function App() {
const { loading, error, out_data, client } = useQuery(GET_USER_PROFILE);
if (loading) return <p>Loading...</p>;
if (error) return <p>Error :(</p>;
client
.query({
query: gql`
query ListUserDbs(
$filter: Table
) {
listUserDBS(filter: $filter) {
items {
sub
}
nextToken
}
}
`
})
.then(result => console.log(result));
const { loading, error, out_data, client } = useQuery(LIST_USER_PROFILE);
問題可能來自 useQuery 但我不知道如何解決。
也許您需要data
而不是out_data
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.