Amplify-cli: GraphQL query returns `null` for nested objects

Created on 23 Jan 2020  路  3Comments  路  Source: aws-amplify/amplify-cli

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}")
}

```gql
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. 
```json
{
    "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? :)

graphql-transformer question

Most helpful comment

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.

All 3 comments

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.

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.

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;
Was this page helpful?
0 / 5 - 0 ratings