0
I don’t understand AWS Authorization yet. I’m getting this error.
GraphQLAPI – ensure credentials error No Cognito Identity pool provided for unauthenticated access
I thought by providing an API key this would just work like when you request from any other API.
This is my schema:
type art
@model
@auth(
rules: [
{ allow: public, operations: [read], provider: iam }
{ allow: public, provider: apiKey }
]
) {
id: ID!
title: String! @index(name: "artByTitle", queryField: "artByTitle")
artist: String
medium: String!
height: Int!
width: Int!
depth: Int!
framed: Boolean!
priceInCents: Int!
red: Int!
green: Int!
blue: Int!
filename: String!
quantity: Int!
category: artCategory @hasOne
}
type artCategory
@model
@auth(
rules: [
{ allow: public, operations: [read], provider: iam }
{ allow: public, provider: apiKey }
]
) {
name: String!
@index(name: "artCategoryByName", queryField: "artCategoryByName")
}
This is my request:
Note that this is done with an .mjs file so the imports are a little weird.
import * as path from "path";
import { createRequire } from 'module';
import { Amplify, API, graphqlOperation, Logger } from 'aws-amplify';
import { GRAPHQL_AUTH_MODE } from '@aws-amplify/api';
const awsPath = path.resolve('../../src/aws-exports.js');
const awsconfig = createRequire(awsPath);
//import { createArt } from "../../src/graphql/mutations.ts";
Amplify.configure(awsconfig);
const createArt = /* GraphQL */ `
mutation CreateArt(
$input: CreateArtInput!
$condition: ModelArtConditionInput
) {
createArt(input: $input, condition: $condition) {
id
title
artist
medium
height
width
depth
framed
priceInCents
red
green
blue
filename
quantity
category {
name
id
createdAt
updatedAt
__typename
}
createdAt
updatedAt
artCategoryId
__typename
}
}
`;
export async function addArtToAPI(art) {
// art = JSON.stringify(art);
console.log("art: ", art);
console.log("Sending to API");
try {
await API.graphql(graphqlOperation(createArt, {
variables:
{
input:
{
title: art.title,
artist: art.artist,
medium: art.medium,
height: art.height,
width: art.width,
depth: art.depth,
framed: art.framed,// Convert string to boolean
priceInCents: art.price, // Convert price to cents
red: art.red,
green: art.green,
blue: art.blue,
filename: art.filename,
quantity: art.quantity,
category: art.category,
}
}
}),
{
headers: {
'x-api-key': MY.API.KEY
}
}
);
const createdArt = response.data.createArt;
console.log('Created Art:', createdArt);
} catch (error) {
console.log("Unable to Create Art: ", error);
}
};
Is my schema wrong? Or am I supposed to include more info in the headers?
Please use examples if you understand the problem because I’m finding AWS so confusing.
I worked with ChatGPT to get this far but now I’m stuck because it’s giving me standard answers without any examples.