GraphQL Cannot Return 'Null' for 'Non-Nullable Field' <Type.Field>

Viewed 474

I understand what the error is saying and tried debugging every possible solution.

The exact error in my case is:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Order.total.",
      "locations": [
        {
          "line": 5,
          "column": 5
        }
      ],
      "path": [
        "createOrder",
        "total"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Cannot return null for non-nullable field Order.total.",

In my Type Definitions, I have made the Order.total field nullable and the problem still persists. Order Type:

# Mutation
createOrder(orderInput: OrderInput!): Order!
# Custom Input
input OrderInput {
        username: String!
        total: Float
        pizzas: [ID]!
        collectAt: String!
        createdAt: String!
}

The create order resolver has been set up just the same as all other resolvers which work perfectly. Create Order Resolver:

createOrder: async (
        _,
        { orderInput: { username, total, pizzas, collectAt, createdAt } }
    ) => {
        try {
            // console.log(pizzas);
            const newOrder = Order({
                username,
                total,
                pizzas,
                collectAt,
                createdAt
            });

            const res = await newOrder.save();
            return res;
        } catch (err) {
            throw new Error("Caught An Error => " + err);
        }
    }

I know some people may request what ORM I'm using in this case, I'm using mongoose. Here is the Order Model:

const orderSchema = new Schema({
    username: String,
    total: Number,
    pizzas: [
        {
            type: Schema.Types.ObjectId,
            ref: "pizzas"
        }
    ],
    collectAt: String,
    createdAt: String
});

Thanks in advance.

0 Answers
Related