Trying to query MongoDB with Apollo server and present it with React. Currently not getting anything back.
Data in MongoDB looks like this :
db.technologyblog.find()
{ "_id" : ObjectId("63fd4e0968322cbedac05c4f"), "technologyblog" : "Technology Blog", "gitrepository" : "Git Info", "education" : "University" }
Book.js
import mongoose from "mongoose";
export const computerExperience = mongoose.model( 'Profile', { profile: String}, "profile");
export const topinfo = mongoose.model( 'Technologyblog', { technologyblog: String, gitrepository: String, education: String}, "technologyblog");
typeDefs.js
import gql from 'graphql-tag';
export const typeDefs = gql`
type Query {
topinfo:[Topinfo]
}
type Topinfo{
technologyblog: String
gitrepository: String
education: String
}
`;
resolvers.js
import { topinfo} from './models/Book.js'
export const resolvers = {
Query: {
topinfo: async() => await topinfo.find({}),
}
};
It all works well when I query from here : http://localhost:4000/
query test {
topinfo {
education
gitrepository
technologyblog
}
}
This is the presentation React layer above is my Apollo server setup.
Books.js
import { gql, useQuery } from '@apollo/client';
const BOOKS_QUERY = gql`
query Topinfos {
topinfo{
technologyblog
gitrepository
education
}
}
`;
export default function Books(){
const { data, error, loading } = useQuery(BOOKS_QUERY);
if ( error ) {
console.error('BOOKS_QUERY error', error);
}
return <div>
{loading && 'Loading...'}
{error && 'Error (check console logs)'}
{ data }
</div>;
}