I am trying to query for a user's specific data using AWS Cognito, Appsync, and Graphql. Before I changed my Schema rules to include @auth, I was able to access all data from all users, but that is not what I want. However, when I query for data using Graphql, I am getting an auauthorized error. errorType: "Unauthorized", message: "Not Authorized to access listMembers on type Query", … }
// Old Schema.graphql
type Member {
id: ID!,
name: String,
owner: String
}
// New Schema.graphql
type Member
@model
@auth(rules: [{ allow: owner }, { allow: private, operations: [read] }]) {
id: ID!
name: String
owner: String
}
In my Members.js component, I call for the list of members and ensure I have set that the owner property is available, but I still get the unauthorized error. Additionally, when I go into AWS AppSync Console and try to query for a list of members, I get the same error.
I know I have members in my DynamoDB, because they are present, but when I query for them, that's when I get that error.
This is what my Member.js component looks like:
// Members.js
import { useState, useEffect } from 'react'
import { API, Auth } from 'aws-amplify'
import { listMembers } from '../../graphql/queries'
const [members, updateMembers] = useState([])
const [myMembers, updateMyMembers] = useState([])
export const Members = () => {
/* fetch member's when component loads */
useEffect(() => {
fetchMembers()
}, [])
async function fetchMembers() {
/* query the API, ask for 100 items */
let postData = await API.graphql({
query: { query: listMemberes, variables: { limit: 100 }},
variables: { limit: 100 },
})
let membersArray = postData.data.listMembers.items
updateLoading(false)
/* update the members array in the local state */
setMemberState(membersArray)
}
async function setMemberState(membersArray) {
const user = await Auth.currentAuthenticatedUser()
const myMemberData = membersArray.filter((p) => p.owner === user.username)
console.log('membersArray:', membersArray)
updateMyMembers(myMemberData)
updateMembers(membersArray)
}
}

