尝试在React本机项目中设置graphql,但在查询调用时出错
我希望更好地理解我可能会出错的地方,或者是否有一种方法,我们可能不需要阿波罗来查询后端。当我在尝试在React本机项目中设置graphql,但在查询调用时出错,graphql,react-apollo,apollo-client,Graphql,React Apollo,Apollo Client,我希望更好地理解我可能会出错的地方,或者是否有一种方法,我们可能不需要阿波罗来查询后端。当我在http://localhost:3000/api/v1/graphql一切正常。在前端,根据我所见,我需要设置一个阿波罗客户作为 import { HttpLink } from 'apollo-link-http'; import { ApolloClient } from 'apollo-client'; import { InMemoryCache } from 'apollo-cache-in
http://localhost:3000/api/v1/graphql
一切正常。在前端,根据我所见,我需要设置一个阿波罗客户作为
import { HttpLink } from 'apollo-link-http';
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
const makeApolloClient = () => {
// create an apollo link instance, a network interface for apollo client
const link = new HttpLink({
uri: `http://10.0.0.151:3000/api/v1/graphql`
});
// create an inmemory cache instance for caching graphql data
const cache = new InMemoryCache();
// instantiate apollo client with apollo link instance and cache instance
const client = new ApolloClient({
link,
cache
});
return client;
};
export default makeApolloClient;
最后导入客户端并将其包装到我的应用程序中。这项工作正在如期进行
import makeApolloClient from './apollo';
const initialState = {};
const store = configureStore(initialState);
const client = makeApolloClient();
const App = () => {
return (
<ApolloProvider client={client}>
<Provider store={store}>
<NavigationContainer>
<Root />
</NavigationContainer>
</Provider>
</ApolloProvider>
);
};
看来我遇到的问题是进口。我使用的是来自“react apollo”的
import{ApolloProvider}代码>。当我将它从'@apollo/client'替换为import{ApolloProvider}时,一切都开始工作了代码>
import { useQuery, gql } from '@apollo/client';
const query = gql`
{
getAllUsers {
email
}
}
`;
const SigninScreen = ({ navigation, saveToken }) => {
const token = useSelector((state) => state.session.token);
const dispatch = useDispatch();
const { data, error, loading } = useQuery(query);
console.log('THE DSATA IS', data);
console.log('THE ERROR IS ', error);
console.log('THE LOADING IS ', loading);