Home > front end >  Angular AWS Amplify Graphql Query Not Returning Expected Data Using API Service
Angular AWS Amplify Graphql Query Not Returning Expected Data Using API Service

Time:05-24

I am having a hard time trying to query the data I need using AWS Amplify Graphql API.

I have managed to write a test query in the AWS AppSync Console, but I do not know how to replicate this in code using the API Service.

Here is the query that I created in the AppSync Console and I want to call from my Angular app using the API service:

query MyQuery {
  listGymnastMeetSessions(filter: {meetSessionID: {eq: "4aea7082-60b8-4268-a816-00661e7a6e4d"}}) {
    items {
      gymnastID
      meetSessionID
      gymnast {
        firstName
        lastName
        meetScores(filter: {meetSessionId: {eq: "4aea7082-60b8-4268-a816-00661e7a6e4d"}}) {
          items {
            balanceBeam
            floor
            unevenBars
            floor
          }
        }
      }
    }
  }
}

Results of the above query returns what I am looking for:

"data": {
    "listGymnastMeetSessions": {
      "items": [
        {
          "gymnastID": "a4acbcb9-3b42-4a31-8448-d300e57b5b97",
          "meetSessionID": "3aea7082-60b8-4268-a816-00661e7a6e4d",
          "gymnast": {
            "firstName": "Sophia",
            "lastName": "Jones",
            "meetScores": {
              "items": [
                {
                  "balanceBeam": 9.5,
                  "floor": 9.5,
                  "unevenBars": 9.5
                }
              ]
            }
          }
        },
        {
          "gymnastID": "e212e9cf-6620-4b1f-a6b4-f0a820b265da",
          "meetSessionID": "3aea7082-60b8-4268-a816-00661e7a6e4d",
          "gymnast": {
            "firstName": "Olivia",
            "lastName": "Jones",
            "meetScores": {
              "items": [
                {
                  "balanceBeam": 9.15,
                  "floor": 9.05,
                  "unevenBars": 9.25
                }
              ]
            }
          }
        },
        {
          "gymnastID": "d97f5cea-a330-4717-85d7-b21ccc31a871",
          "meetSessionID": "3aea7082-60b8-4268-a816-00661e7a6e4d",
          "gymnast": {
            "firstName": "Emily",
            "lastName": "Jones",
            "meetScores": {
              "items": []
            }
          }
        }
      ]
    }
  }

Here is the API call i'm making from my Angular app using the API service:

this.api
    .ListGymnastMeetSessions({
      meetSessionID: { 
        eq: this.id 
      } 
    })
    .then((event) => {
      this.gymnastMeetSessions = event.items as GymnastMeetSessions[];
      console.log('gymnastMeetSessions ', this.gymnastMeetSessions); 
    });

Here is an example of what's returned from the API call:

0:
createdAt: "2022-05-16T13:19:06.364Z"
gymnast:
  createdAt: "2022-04-28T17:09:03.758Z"
  dateOfBirth: "2020-03-28"
  firstName: "Sophia"
  gender: "female"
  id: "b4acbcb9-3b42-4a31-8448-d300e57b5b97"
  lastName: "Jones"
  level: "Level 2"
  private: null
  teamId: "f1d61c2a-fbe9-4439-8870-fef776a1d5c9"
  updatedAt: "2022-04-28T17:09:03.758Z"
  __typename: "Gymnast"
  [[Prototype]]: Object
gymnastID: "b4acbcb9-3b42-4a31-8448-d300e57b5b97"
id: "013e5f11-a06a-4bc6-b337-8ea834d5ccd0"
meetSession:
  createdAt: "2022-05-13T20:10:57.081Z"
  division: "Senior"
  id: "3aea7082-60b8-4268-a816-00661e7a6e4d"
  level: "Level 2"
  meetId: "e0911c5c-1ffc-4acf-8b61-5c85016a1e16"
  sessionName: "Session 1"
  startDate: "2022-05-15"
  startTime: "13:00:00.000Z"
  updatedAt: "2022-05-13T20:10:57.081Z"
  __typename: "MeetSession"
  [[Prototype]]: Object
meetSessionID: "3aea7082-60b8-4268-a816-00661e7a6e4d"
updatedAt: "2022-05-16T13:19:06.364Z"
__typename: "GymnastMeetSessions"

You can view my schema.graphql file here.

Looking for some guidance on things I can try. Can someone point me in the right direction?

CodePudding user response:

You can write your own graphql statement to include the meetscores info. Assuming your GraphQL schema is correct, you can do something like this:

async _loadGymnastMeetSessions(
    filter: ModelGymnastMeetSessionsFilterInput,
    limit: number,
    nextToken: string
  ) {
    const statement = `query MyQuery {
  listGymnastMeetSessions(filter: {meetSessionID: {eq: "4aea7082-60b8-4268-a816-00661e7a6e4d"}}) {
    items {
      gymnastID
      meetSessionID
      gymnast {
        firstName
        lastName
        meetScores(filter: {meetSessionId: {eq: "4aea7082-60b8-4268-a816-00661e7a6e4d"}}) {
          items {
            balanceBeam
            floor
            unevenBars
            floor
          }
        }
      }
    }
  }
}`;
    const gqlAPIServiceArguments: any = {};
    if (filter) {
      gqlAPIServiceArguments.filter = filter;
    }
    if (limit) {
      gqlAPIServiceArguments.limit = limit;
    }
    if (nextToken) {
      gqlAPIServiceArguments.nextToken = nextToken;
    }
    const response = (await API.graphql(
      graphqlOperation(statement, gqlAPIServiceArguments)
    )) as any;
    return <ListGymnastMeetSessionsQuery>response.data.listGymnastMeetSessions;
}

Instead of explicitly adding the id to the query you can use your filter param that goes into the method, and $filter in your Graphql statement.

CodePudding user response:

I was able to do what I needed by created a new model in the graphql schema.

Here is what I did:


    type MeetSessionGymnastScore @model {
      id: ID!
      meetId: ID! @index(name: "byMeet", sortKeyFields: ["id"])
      meet: Meet @belongsTo(fields: ["meetId"])
      meetSessionId: ID! @index(name: "byMeetSession", sortKeyFields: ["id"])
      meetSession: MeetSession @belongsTo(fields: ["meetSessionId"])
      gymnastId: ID! @index(name: "byGymnast", sortKeyFields: ["id"])
      gymnast: Gymnast @belongsTo(fields: ["gymnastId"])
      meetScoreId: ID @index(name: "byMeetScore", sortKeyFields: ["id"])
      meetScore: MeetScore @belongsTo(fields: ["meetScoreId"])
    }

  • Related