'AWS APPSYNC how to apply aws_api_key authorization

How to apply @aws_api_key authorization, I applied like its said in the docs, but still getting unauthorized error

type Todo @aws_api_key @model
{
  id: ID!
  name: String!
  description: String
}

Graphql query

query toTods {
  listTodos {
    items {
      id
      name
    }
  }
}

gives me unauthorized error, I was able to achieve desired results by going to the APPSYNC console and editing the schema

type Query {
    getTodo(id: ID!): Todo
    listTodos(filter: ModelTodoFilterInput, limit: Int, nextToken: String): ModelTodoConnection
        @aws_api_key
}

type ModelTodoConnection @aws_api_key {
    items: [Todo]
    nextToken: String
}

But If I do a amplify push I loose all these changes. How can I specify all this in the parent Type Todo



Solution 1:[1]

Suppose you running a lambda in background and that lambda is calling some appssync API so you can apply @aws_api_key in type of response return by that graphql API. this give access to that lambda to call that graphql API. eg;

type Myclass @aws_api_key {
id: ID!
name: String
students: [Student]
batch: Int
}

extend type Mutation {
   getClass(id: Int!): Myclass @aws_api_key
} 

extend type Subscription {
onGetClass: Myclass @aws_subscribe(mutations: ["getClass"])
}

AWS API key is one of the authorization method through which we can call grahql API. This way help us to make some API called though API gateway and some to call through access token(from aws cognito or auth0,etc).This avoid case of creating new repo for open API(which best work for guest users)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1