amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

GraphQL query returns `null` for nested objects

Open GoodbyePlanet opened this issue 6 years ago • 4 comments

Hi all, I have the following schema

type Project
  @model(subscriptions: { onUpdate: ["onUpdateProject"] })
  {
  id: ID!
  title: String
   ...
  description: String!
  organization: Organization @connection(name: "OrganizationProjects")
}
type Organization
  @model
  {
  id: ID!
  name: String!
  logo: String
  url: String
  projects: [Project] @connection(name: "OrganizationProjects")
...
}
type User
  @model(subscriptions: null)
  {
  id: ID!
  username: String!
  email: String!
  phoneNumber: String!
...
  donations: [Donation] @function(name: "genericResolver-${env}")
}
type Donation {
  amount: Int
  donator: User
  project: Project
  status: DonationStatus
}

In User model I have donations filed and I'm using @function directive to get data for this field.

Now I can see in CloudWatch that I am returning data that I want from genericResolver lambda.

{
    "amount": 2000,
    "project": {
        "id": "8c9c7885-7913-46f4-b7d6-1d3c154c694c",
        "description": "Description...",
        "organization": {
            "id": "1d35063a-0dc8-4b1c-8c8d-01fbff71e69f",
            "name": "SOS",
            "logo": "446efdf2-fb0d-4d7d-be33-de574032f90e.png",
            "url": null,
        },
    },
    "status": "COMPLETED"
}

Also generated query looks ok to me

export const getUser = `query GetUser($id: ID!) {
  getUser(id: $id) {
    id
    username
    email
    phoneNumber
    donations {
      amount
      project {
        id
        title
        description
        organization {
          id
          name
          logo
          url
        }
      }
      status
    }
}
`;

But in AppSync console when I run this query

query getUserById {
  getUser(id: "9f57a087-2938-4a7e-b463-25f70f70f120") {
    id
    username
    email
    donations {
      amount
      project {
        id
        title
        organization {
          id
        }
      }
      status
    }
  }
}

I always get null for organization object.

What am I doing wrong here, any suggestions? :)

GoodbyePlanet avatar Jan 23 '20 15:01 GoodbyePlanet

I haven't attempted your specific use case, but it is likely that the @connection on your Project.organisation is creating a resolver for this field, but because your project object does not include the foreign key to the organisation, it is not found and set to null.

RossWilliams avatar Jan 23 '20 17:01 RossWilliams

I haven't attempted your specific use case, but it is likely that the @connection on your Project.organisation is creating a resolver for this field, but because your project object does not include the foreign key to the organisation, it is not found and set to null.

Thanks for answering, I'll check that out.

GoodbyePlanet avatar Jan 23 '20 18:01 GoodbyePlanet

THANKS A LOT THAT WAS EXACTLY IT.

And thanks to @GoodbyePlanet for asking while I doubting that anybody would care to answer <3

After scratching our heads over it the whole day, this line made the difference:

      donation.project['projectOrganizationId'] = order.project.organization.id;

AndreasEK avatar Jan 23 '20 20:01 AndreasEK

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[bot] avatar May 26 '21 17:05 github-actions[bot]