aws / aws/aws-appsync-community

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

Open
#379 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
507
Forks
37
PR merge metrics
No merged PRs in 30d

Description

**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)"
}
]
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue in the AppSync console with the provided User and Query schema, JavaScript resolver, and GetUser query for a nonexistent ID. Compare the returned data and errors, then determine how root-level null results are handled; done means the response keeps getUser as null without non-nullable subfield errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, javascript
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.