Resources:
# AppSync/GraphQL setup
  AppSyncAPI:
    Type: AWS::AppSync::GraphQLApi
    Properties:
      Name: ${self:custom.prefix}-appsync
      AuthenticationType: AMAZON_COGNITO_USER_POOLS
      UserPoolConfig:
        UserPoolId: !Ref CognitoUserPool
        AwsRegion: ${self:provider.region}
        DefaultAction: ALLOW
      AdditionalAuthenticationProviders:
        - AuthenticationType: API_KEY

  AppSyncAPISchema:
    Type: AWS::AppSync::GraphQLSchema
    DependsOn:
      - AppSyncAPI
    Properties:
      ApiId: !GetAtt AppSyncAPI.ApiId
      Definition: |
        ${file(./resources/appsync/schema.graphql)}

# Lambda datasource for AppSync
  AppSyncLambdaRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      Policies:
        - PolicyName: WriteCloudWatchLogs
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: "arn:aws:logs:*:*:*"
        - PolicyName: AppSyncLambdaDynamoDBInvokeRole
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - dynamodb:BatchGetItem
                  - dynamodb:BatchWriteItem
                  - dynamodb:PutItem
                  - dynamodb:DeleteItem
                  - dynamodb:GetItem
                  - dynamodb:Scan
                  - dynamodb:Query
                  - dynamodb:UpdateItem
                Resource:
                  - !GetAtt WorkDynamoDbTable.Arn
                  - !GetAtt MaterialDynamoDbTable.Arn
                  - !GetAtt MechanismDynamoDbTable.Arn

  AppSyncLambdaServiceRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - appsync.amazonaws.com
            Action:
              - sts:AssumeRole

  BatchUpdateLambdaInvokePolicy:
    Type: AWS::IAM::Policy
    Properties:
      PolicyName: BatchUpdateLambdaInvokePolicy
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Action:
              - lambda:InvokeFunction
            #BatchUpdate funkcija definēta serverless.yml
            #Funkcijas nosaukums normalizēts + "LambdaFunction"
            Resource: !GetAtt BatchUpdateLambdaFunction.Arn
      Roles:
        - !Ref AppSyncLambdaServiceRole

  BatchUpdateLambdaDataSource:
    Type: AWS::AppSync::DataSource
    Properties:
      ApiId: !GetAtt AppSyncAPI.ApiId
      Name: "BatchUpdateLambdaDataSource"
      Description: "Batch update datasource via Lambda."
      Type: AWS_LAMBDA
      ServiceRoleArn:
        !GetAtt AppSyncLambdaServiceRole.Arn
      LambdaConfig:
        LambdaFunctionArn: !GetAtt BatchUpdateLambdaFunction.Arn

# DynamoDB datasource for AppSync
  AppSyncDynamoDBServiceRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - appsync.amazonaws.com
            Action:
              - sts:AssumeRole

  AppSyncDynamoDBInvokePolicy:
    Type: AWS::IAM::Policy
    Properties:
      PolicyName: AppSyncDynamoDBInvokePolicy
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Action:
              - dynamodb:BatchGetItem
              - dynamodb:BatchWriteItem
              - dynamodb:PutItem
              - dynamodb:DeleteItem
              - dynamodb:GetItem
              - dynamodb:Scan
              - dynamodb:Query
              - dynamodb:UpdateItem
            Resource:
              - !GetAtt WorkDynamoDbTable.Arn
              - !GetAtt MaterialDynamoDbTable.Arn
              - !GetAtt MechanismDynamoDbTable.Arn
      Roles:
        - !Ref AppSyncDynamoDBServiceRole

  AppSyncDynamoDBWorkSource:
    Type: AWS::AppSync::DataSource
    Properties:
      ApiId: !GetAtt AppSyncAPI.ApiId
      Name: "AppSyncDynamoDBWorkSource"
      Description: "A DataSource via DynamoDB for Work table."
      Type: AMAZON_DYNAMODB
      DynamoDBConfig:
        AwsRegion: ${self:provider.region}
        TableName: ${self:provider.environment.WORK_TABLE}
      ServiceRoleArn:
        !GetAtt AppSyncDynamoDBServiceRole.Arn

  AppSyncDynamoDBMaterialSource:
    Type: AWS::AppSync::DataSource
    Properties:
      ApiId: !GetAtt AppSyncAPI.ApiId
      Name: "AppSyncDynamoDBMaterialSource"
      Description: "A DataSource via DynamoDB for Material table."
      Type: AMAZON_DYNAMODB
      DynamoDBConfig:
        AwsRegion: ${self:provider.region}
        TableName: ${self:provider.environment.MATERIAL_TABLE}
      ServiceRoleArn:
        !GetAtt AppSyncDynamoDBServiceRole.Arn

  AppSyncDynamoDBMechanismSource:
    Type: AWS::AppSync::DataSource
    Properties:
      ApiId: !GetAtt AppSyncAPI.ApiId
      Name: "AppSyncDynamoDBMechanismSource"
      Description: "A DataSource via DynamoDB for Mechanism table."
      Type: AMAZON_DYNAMODB
      DynamoDBConfig:
        AwsRegion: ${self:provider.region}
        TableName: ${self:provider.environment.MECHANISM_TABLE}
      ServiceRoleArn:
        !GetAtt AppSyncDynamoDBServiceRole.Arn

# Resolvers for Appsync
  QueryWork: ${file(resources/appsync/templates/Query/getWork.yaml)}
  QueryWorks: ${file(resources/appsync/templates/Query/listWorks.yaml)}
  QueryUserWorks: ${file(resources/appsync/templates/Query/listUserWorks.yaml)}
  QueryMaterial: ${file(resources/appsync/templates/Query/getMaterial.yaml)}
  QueryMaterials: ${file(resources/appsync/templates/Query/listMaterials.yaml)}
  QueryUserMaterials: ${file(resources/appsync/templates/Query/listUserMaterials.yaml)}
  QueryMechanism: ${file(resources/appsync/templates/Query/getMechanism.yaml)}
  QueryMechanisms: ${file(resources/appsync/templates/Query/listMechanisms.yaml)}
  QueryUserMechanisms: ${file(resources/appsync/templates/Query/listUserMechanisms.yaml)}
  ConnectWorkMaterial: ${file(resources/appsync/templates/Query/connectWorkMaterial.yaml)}
  ConnectWorkMechanism: ${file(resources/appsync/templates/Query/connectWorkMechanism.yaml)}
  MutationCreateWork: ${file(resources/appsync/templates/Mutation/createWork.yaml)}
  MutationUpdateWork: ${file(resources/appsync/templates/Mutation/updateWork.yaml)}
  MutationDeleteWork: ${file(resources/appsync/templates/Mutation/deleteWork.yaml)}
  MutationBatchCreateWork: ${file(resources/appsync/templates/Mutation/batchCreateWork.yaml)}
  MutationBatchUpdateWork: ${file(resources/appsync/templates/Mutation/batchUpdateWork.yaml)}
  MutationBatchDeleteWork: ${file(resources/appsync/templates/Mutation/batchDeleteWork.yaml)}
  MutationCreateMaterial: ${file(resources/appsync/templates/Mutation/createMaterial.yaml)}
  MutationUpdateMaterial: ${file(resources/appsync/templates/Mutation/updateMaterial.yaml)}
  MutationDeleteMaterial: ${file(resources/appsync/templates/Mutation/deleteMaterial.yaml)}
  MutationBatchCreateMaterial: ${file(resources/appsync/templates/Mutation/batchCreateMaterial.yaml)}
  MutationBatchUpdateMaterial: ${file(resources/appsync/templates/Mutation/batchUpdateMaterial.yaml)}
  MutationBatchDeleteMaterial: ${file(resources/appsync/templates/Mutation/batchDeleteMaterial.yaml)}
  MutationCreateMechanism: ${file(resources/appsync/templates/Mutation/createMechanism.yaml)}
  MutationUpdateMechanism: ${file(resources/appsync/templates/Mutation/updateMechanism.yaml)}
  MutationDeleteMechanism: ${file(resources/appsync/templates/Mutation/deleteMechanism.yaml)}
  MutationBatchCreateMechanism: ${file(resources/appsync/templates/Mutation/batchCreateMechanism.yaml)}
  MutationBatchUpdateMechanism: ${file(resources/appsync/templates/Mutation/batchUpdateMechanism.yaml)}
  MutationBatchDeleteMechanism: ${file(resources/appsync/templates/Mutation/batchDeleteMechanism.yaml)}