You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

服务端Apollo GraphQL Mutation报错:非空字段不能返回空,如何解决?

Hey there! Let's break down that Cannot return null for non-nullable field error you're facing with your Apollo GraphQL user registration flow. This is super common when starting out, and it almost always boils down to a mismatch between your schema definitions and what your resolver functions actually return.

Common Causes & Fixes for Your Scenario

Let's go through the most likely issues based on the code snippets you shared:

  1. Your createAccount mutation is missing a non-null return type (or your resolver isn't fulfilling it)
    Looking at your schema snippet, the createAccount mutation cuts off at email: St... — I'm guessing you intended it to accept email: String! as an argument, but you also need to define its return type. If you want it to return the newly created user (which is standard for registration), your mutation should look like:

    type Mutation {
      createAccount(name: String!, email: String!): User!
    }
    

    The User! means your resolver must return a valid User object (not null). If your resolver is returning null (maybe because you haven't hooked up a database yet, or your test logic isn't generating a user), that's exactly what triggers the error.

  2. Your resolver isn't returning a complete User object
    Even if you do return something from the resolver, if it's missing one of the non-null fields (id, name, email), GraphQL will throw this error. For testing purposes, you can hardcode a valid User object in your resolver to rule this out:

    // Example resolver for createAccount
    const resolvers = {
      Mutation: {
        createAccount: (parent, { name, email }) => {
          // Skip database calls for testing — return a mock user
          return {
            id: `test-user-${Date.now()}`, // Generate a unique ID
            name: name,
            email: email
          };
        }
      }
    };
    

    This ensures you're returning all required non-null fields, which should resolve the error if the issue was incomplete data.

  3. Async resolver logic isn't handling promises correctly
    If you're using async/await or promises in your resolver (e.g., to save to a database), make sure you're properly returning the resolved value. For example, if you forget to await a database call, your resolver might return a pending promise instead of a User object — which GraphQL interprets as null.

Quick Test to Confirm

Try running a mutation like this in your GraphQL playground:

mutation {
  createAccount(name: "Test User", email: "test@example.com") {
    id
    name
    email
  }
}

If you use the mock resolver I shared above, this should return a valid user without errors. If it still throws the error, double-check that your schema's User type doesn't have any typos, and that your resolver is correctly mapped to the createAccount mutation.


内容的提问来源于stack exchange,提问作者Collins Orlando

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:22:26