Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL query with optional root type fails due to non-nullable field errors when resolver returns null #379

Open
niqo01 opened this issue Dec 2, 2024 · 0 comments

Comments

@niqo01
Copy link

niqo01 commented Dec 2, 2024

Issue Description:

When my resolver returns null due to the absence of data, AWS AppSync correctly sets the root query result to null. However, the errors array includes validation errors for missing non-nullable fields within the type, despite the entire query result being null. This behavior seems inconsistent, as no subfields should be validated when the root query result is null.

Expected Behavior:

If the resolver returns null for the root type, AppSync should:
• Return null for the query result.
• Exclude validation errors for non-nullable subfields, as they are irrelevant when the root type is null.

Observed Behavior:

• The query result is correctly null.
• The errors array contains messages about missing non-nullable subfields, even though the root type is null.

Reproduce instruction

# Types
type User  {
  id: ID!
  firstName: String!
}

type Query {
  getUser(id: ID!): User 
}

Resolver:

import { util } from "@aws-appsync/utils";
import { select, createPgStatement, toJsonObject } from "@aws-appsync/utils/rds";
function request(ctx) {
  const { id } = ctx.args;
  const where = {
    id: {
      eq: id
    }
  };
  const statement = select({
    table: "users",
    columns: "*",
    where,
    limit: 1
  });
  return createPgStatement(statement);
}
function response(ctx) {
  const { error, result } = ctx;
  if (error) {
    return util.appendError(
      error.message,
      error.type,
      result
    );
  }
  return toJsonObject(result)[0][0];
}
export {
  request,
  response
};

Query an non existing user id through app sync console:

query GetUser {
  getUser(id: "1") {
    firstName
    id
  }
}

Response:

{
  "data": {
    "getUser": null
  },
  "errors": [
    {
      "path": [
        "getUser"
      ],
      "data": null,
      "errorType": "InternalFailure",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "An internal failure occurred while resolving this field."
    },
    {
      "path": [
        "getUser",
        "firstName"
      ],
      "locations": null,
      "message": "Cannot return null for non-nullable type: 'String' within parent 'User' (/getUser/firstName)"
    },
    {
      "path": [
        "getUser",
        "id"
      ],
      "locations": null,
      "message": "Cannot return null for non-nullable type: 'ID' within parent 'User' (/getUser/id)"
    }
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant