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

Mock: Cannot return null for non-nullable field [Object.id] #2248

Closed
kldeb opened this issue Sep 11, 2019 · 6 comments · Fixed by #2267
Closed

Mock: Cannot return null for non-nullable field [Object.id] #2248

kldeb opened this issue Sep 11, 2019 · 6 comments · Fixed by #2267
Assignees
Labels
bug Something isn't working mock Issues tied to the mock functionality

Comments

@kldeb
Copy link
Contributor

kldeb commented Sep 11, 2019

Describe the bug
When running amplify mock there are connections that are not required that are causing the following error to appear when I run the queries, for example:

Cannot return null for non-nullable field Utility.id.

The id field on the connected object is non-nullable but the object itself is nullable. This works fine when querying AppSync.

Here's the relevant snippet from my schema:

type Utility
  @model
{
  id: ID!
  name: String!
}

type Account
  @model
{
  id: ID!
  [...]
  utility: Utility @connection
[...]
}

The mutation to create the record:

mutation CreateAccount {
  createAccount(input: {
    id: "e4b91a75-7843-442d-aab9-e94bc81c4cb7",
  }) {
    id
    utility {
      id
    }
  }
}

The query that will produce the error:

query GetAccount {
  getAccount(id: "e4b91a75-7843-442d-aab9-e94bc81c4cb7") {
    id
    utility {
      id
    }
  }
}

To Reproduce
Steps to reproduce the behavior:

  1. Create a schema with a connection that is nullable
  2. Run amplify mock
  3. Create a record without that field set so the connected object is null
  4. Query that field
  5. See error

Expected behavior
Show null in query results like AppSync.

Desktop (please complete the following information):

  • OS: macOS
  • Browser Firfox
  • Version Amplify 3.0.0
@UnleashedMind UnleashedMind added mock Issues tied to the mock functionality pending-triage Issue is pending triage labels Sep 12, 2019
@yuth yuth added bug Something isn't working and removed pending-triage Issue is pending triage labels Sep 12, 2019
yuth added a commit to yuth/amplify-cli that referenced this issue Sep 12, 2019
Simulator converted falsy results to an empty object which prevented returning null for fields in
connection type and caused graphql validation error when query included a non nullable field

fix aws-amplify#2248
kaustavghosh06 pushed a commit that referenced this issue Sep 12, 2019
…te (#2267)

Simulator converted falsy results to an empty object which prevented returning null for fields in
connection type and caused graphql validation error when query included a non nullable field

fix #2248
@jbertman
Copy link

Was there a regression that happened to reintroduce this bug? I am currently experiencing this with the following schema:

type Policy
@model(queries: { get: "getPolicy", list: "listPolicies" })
@auth(
    rules: [
        { allow: owner }
        { allow: groups, groups: ["Admin"] }
        { allow: private, provider: iam, operations: [read] }
    ]
) {
    id: ID!
    <SNIP>
    # This is broken in amplify mock
    credential: Credential @connection
}
type Credential
@model
@auth(
  rules: [
    { allow: owner }
    { allow: groups, groups: ["Admin"] }
    { allow: private, provider: iam, operations: [read] }
  ]
) {
  id: ID!
  <SNIP>
  name: String!
}

When pushed, the mutation for createPolicy works without issue (on AppSync). But with amplify mock I get Cannot return null for non-nullable field Credential.id. when trying to CRUD a policy that includes the credential field.

@Nxtra
Copy link

Nxtra commented Jan 21, 2021

@kldeb @jbertman Did you manage to work out a solution for this? I think I ran into the same problem: #6450

@kldeb
Copy link
Contributor Author

kldeb commented Jan 21, 2021

@Nxtra I gave up using mock for everything except lambda testing. I hope to use it again when it catches up to the features that my amplify project uses.

@lostcodingsomewhere
Copy link

I too am running into this issue -- currently on version 4.43.0

@lostcodingsomewhere
Copy link

lostcodingsomewhere commented Feb 16, 2021

For anyone running into this issue (on v4.43.0), here's a workaround (pretty hackey but will suffice for now):

replace all of your API.graphql with the following helper:

const graphqlAPI = async (
   options,
   additionalHeaders?: {
      [key: string]: string;
   }
): Promise<any> => {
   try {
      return await API.graphql(options, additionalHeaders);
   } catch (err) {
      const realErrors = err.errors.filter(
         (e) => !e.message.startsWith('Cannot return null for non-nullable')
      );
      if (realErrors.length > 0) {
         console.log(`THROWING GRAPHQL ERROR`);
         throw err;
      } else {
         const realDataResult = { data: err.data };
         return realDataResult;
      }
   }
};

@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working mock Issues tied to the mock functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants