GraphQL Schema拼接未解析且返回Null问题求助
Hey there, let's work through this schema stitching null result issue together! I've run into similar problems before when combining schemas with Apollo, so here are the most common fixes to check:
Looking at your code snippet, you only provided typeDefs to makeExecutableSchema but no resolver logic. If your schema fields rely on fetching data from MongoDB (which they almost certainly do), the default resolver will only return a value if the parent object already has that field — which it won't if you're not querying the database.
Fix this by including your resolver implementations when creating each executable schema:
// Import your MongoDB models and resolver logic first const PostResolvers = { Query: { posts: async () => await PostModel.find(), post: async (_, { id }) => await PostModel.findById(id), }, }; const UserResolvers = { Query: { users: async () => await UserModel.find(), user: async (_, { id }) => await UserModel.findById(id), }, }; // Now create executable schemas with resolvers included const postSchema = makeExecutableSchema({ typeDefs: PostSchema, resolvers: PostResolvers }); const userSchema = makeExecutableSchema({ typeDefs: UserSchema, resolvers: UserResolvers });
If your queries are trying to fetch related data across schemas (like a Post's author from the User schema), you need to explicitly tell GraphQL how to connect those pieces. Without merge resolvers, it has no way to delegate the query to the correct schema, resulting in null.
For example, if your Post type has an authorId that links to a User, add this when stitching schemas:
const mergedSchema = stitchSchemas({ schemas: [postSchema, userSchema], // Extend the Post type to include the author field typeDefs: ` extend type Post { author: User } `, resolvers: { Post: { author: { // Define a fragment to get the necessary field from Post fragment: `fragment PostAuthorID on Post { authorId }`, resolve(parent, _, context, info) { // Delegate the query to the user schema to fetch the author return info.mergeInfo.delegateToSchema({ schema: userSchema, operation: 'query', fieldName: 'user', args: { id: parent.authorId }, context, info, }); }, }, }, }, });
Sometimes the issue isn't with schema stitching at all — it's that your resolvers aren't actually fetching data from MongoDB. Add console logs to your resolver functions to confirm you're getting results:
const PostResolvers = { Query: { posts: async () => { const fetchedPosts = await PostModel.find(); console.log('Fetched posts from DB:', fetchedPosts); // Check this output return fetchedPosts; }, }, };
If the log shows empty arrays or no documents, double-check your MongoDB connection string, model definitions, and that your database actually has data.
If your resolvers depend on context (like a MongoDB connection instance or auth data), make sure it's being passed to every delegated query. When using delegateToSchema, always include the context parameter as shown in the merge resolver example above. Also confirm your Apollo Server context setup is correct:
const server = new ApolloServer({ schema: mergedSchema, context: ({ req }) => { // Pass any necessary context here (e.g., DB connection, user auth) return { db: mongoose.connection }; }, });
Start with these checks — the missing resolvers or merge resolvers are the most likely culprits. Let me know if you still run into issues after testing these!
内容的提问来源于stack exchange,提问作者Richi

