2

I'm running an Angular 11 application that is integrated with AWS Amplify and Appsync using GraphQL and dynamoDB for the backend.

This is my Graphql schema:-

type School
  @model
  @auth(
    rules: [{ allow: owner, ownerField: "admins", operations: [update, read] }]
  ) {
  id: ID!
  name: String!
  admins: [Member]
  classes: [Class] @connection(name: "SchoolClasses")
  members: [Member] @connection(name: "SchoolMembers")
}

type Class
  @model
  @auth(
    rules: [{ allow: owner, ownerField: "admins", operations: [update, read] }]
  ) {
  id: ID!
  name: String!
  school: School @connection(name: "SchoolClasses")
  admins: [Member]
  members: [Member] @connection(name: "ClassMembers")
}

type Member @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  name: String!
  school: School @connection(name: "SchoolMembers")
  class: Class @connection(name: "ClassMembers")
}

This is my client definition:-

const client = new AWSAppSyncClient({
  url: awsconfig.aws_appsync_graphqlEndpoint,
  region: awsconfig.aws_appsync_region,
  auth: {
    type: awsconfig.aws_appsync_authenticationType,
    jwtToken: async () =>
      (await Auth.currentSession()).getAccessToken().getJwtToken(),
  },
  complexObjectsCredentials: () => Auth.currentCredentials(),
  cacheOptions: {
    dataIdFromObject: (obj: any) => `${obj.__typename}:${obj.myKey}`,
  },
});

This is my query method:-

    client
      .query({
        query: ListSchools,
      })
      .then((data: any) => {
        console.log('data from listSchools ', data);
        console.log(data.data.listSchools.items);
      });
  };

This is my query definition:-

import gql from 'graphql-tag';

export default gql`
  query ListSchools(
    $filter: ModelSchoolFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listSchools(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        name
        admins {
          id
          name
          createdAt
          updatedAt
          owner
        }
        classes {
          nextToken
        }
        members {
          nextToken
        }
        createdAt
        updatedAt
      }
      nextToken
    }
  }
`;

The output for data in the console looks like this:-

{
   "data":{
      "listSchools":{
         "items":[],
         "nextToken":null,
         "__typename":"ModelSchoolConnection"
      }
   },
   "loading":false,
   "networkStatus":7,
   "stale":false
}

As you can see, the items is an empty array. But currently I have 3 items in my dynamoDB table:-

enter image description here

What am I doing wrong?

I have checked the regions to see if it is querying a different region, but it is checking the correct region, so I should be seeing the results. Also, wouldn't it throw an error if we're querying the wrong table?

1 Answer 1

2

I figured it out. The issue was in the GraphQL Schema definition where I had set the @auth paramter to only allow a certain admin to access the list, that's why I was getting back an empty array. I removed the @auth parameter and it now gives back the proper list of items.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain what did you do? I am also stuck here. Did you totally remove the @auth{.....} block? Then it should give authentication error
Hey sorry buddy, this was a long time ago. I ended up moving away from the AWS Amplify ecosystem. It just was too restrictive for me. I ended up using a Django based GraphQL API which honestly felt so liberating. I don't exactly remember what I did, but going by what I've written here you could try removing any admin stuff within the auth. Or just closely examine the clauses you've added in the auth. Hopefully it works out.
Thanks bro, aws is really complicated, and (as I understood) also bugfull :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.