While working on the server, I successfully populated user-data and printed it to the console without any issues. However, I am facing a challenge in accessing the data on the client side or even through the Playground
of GraphQL
.
Below is my Schema setup:
const { model, Schema } = require("mongoose");
const postSchema = new Schema({
body: String,
user: {
type: Schema.Types.ObjectId,
ref: "User",
},
});
module.exports = model("Post", postSchema);
const userSchema = new Schema({
username: String,
});
module.exports = model("User", userSchema);
const { gql } = require("apollo-server");
module.exports = gql`
type Post {
id: ID!
body: String!
user: [User]!
}
type User {
id: ID!
username: String!
}
type Query {
getPosts: [Post]!
getPost(postId: ID!): Post!
}
`;
Query: {
async getPosts() {
try {
const posts = await Post.find()
.populate("user");
console.log("posts: ", posts[0]);
// This works and returns the populated user with the username
return posts;
} catch (err) {
throw new Error(err);
}
},
}
Despite successful population on the server, I am unable to access this data on the client side or Playground.
query getPosts {
getPosts{
body
user {
username
}
}
}
I need assistance in figuring out how to access this data from the client perspective. Your help is greatly appreciated.
Thank you!