aws / aws-appsync-community

The AWS AppSync community
https://aws.amazon.com/appsync
Apache License 2.0
506 stars 32 forks source link

runtime.earlyReturn can not return null #280

Open sashee opened 1 year ago

sashee commented 1 year ago

It seems like there is no way to earlyReturn null. Both runtime.earlyReturn(null) and runtime.earlyReturn() throws an ErrorType: "Code", "message": "Runtime Error".

I made a very simple reproduction available here: https://github.com/sashee/appsync-earlyreturn-bug . Use terraform init and terraform apply to deploy.

Here, I have a very simple schema:

type Object {
    value: String!
}
type Query {
    getObject(skip: Boolean!): Object
}
schema {
    query: Query
}

And a query:

query MyQuery {
  getObject(skip: true) {
    value
  }
}

The getObject resolver uses a NONE data source:

import {util, runtime} from "@aws-appsync/utils";
export function request(ctx) {
    if (ctx.args.skip) {
        runtime.earlyReturn();
    }
    return {
        version : "2018-05-29",
        payload: {value: "test"},
    };
}
export function response(ctx) {
    if (ctx.error) {
        return util.error(ctx.error.message, ctx.error.type);
    }
    return ctx.result;
}

What I want to achieve is to return null for the field if some condition is true:

{
  "data": {
    "getObject": null
  }
}

runtime.earlyReturn() and runtime.earlyReturn(null) both throws a Code exception. runtime.earlyReturn({}) complains that the Object.value is null. I tried runtime.earlyReturn(undefined) but that also throws a runtime error.

Strangely, if I use runtime.earlyReturn("") then I get a response that looks like a memory address:

{
  "data": {
    "getObject": {
      "value": "[B@7d403483"
    }
  }
}

What is the correct way to return null for a field using earlyReturn?

DanielGibbsNZ commented 2 months ago

This appears to have been fixed now. Both runtime.earlyReturn(null) and runtime.earlyReturn() work for me and just return null without throwing any errors.