Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cloud/lib/authentication/cognitoCdkConstruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { ServicePurpose } from "../../enums/ServicePurpose";
import { applyTagsToResource } from "../../utils/functions";
import { CognitoIdentityPoolCdkConstruct } from "./cognitoIdentityPoolCdkConstruct";
import { CognitoUserPoolCdkConstruct } from "./cognitoUserPoolCdkConstruct";
import { UserPool } from "aws-cdk-lib/aws-cognito";

interface CognitoProps {
envName: EnvName;
}

export class CognitoCdkConstruct extends Construct {
public userPool: UserPool;

constructor(
scope: Construct,
id: string,
Expand All @@ -25,6 +28,8 @@ export class CognitoCdkConstruct extends Construct {
{ envName }
);

this.userPool = userPool;

const { identityPool, identityPoolRoleAttachment } =
new CognitoIdentityPoolCdkConstruct(
this,
Expand Down
58 changes: 58 additions & 0 deletions cloud/lib/cognitoAppsyncConstruct/cognitoAppsyncConstruct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Construct } from "constructs";
import * as path from "path";
import * as appsync from "@aws-cdk/aws-appsync-alpha";
import * as lambda from "aws-cdk-lib/aws-lambda";
import { IUserPool } from "aws-cdk-lib/aws-cognito";
import { CfnOutput } from "aws-cdk-lib";
import * as iam from "aws-cdk-lib/aws-iam";

interface Props {
userPool: IUserPool;
}

export class CognitoAppsyncConstruct extends Construct {
constructor(scope: Construct, id: string, props: Props) {
super(scope, id);

const api = new appsync.GraphqlApi(this, "CognitoAppsyncApp", {
name: "CognitoAppsyncApi",
schema: appsync.Schema.fromAsset(
path.join(__dirname, "graphql/schema.graphql")
),
xrayEnabled: true,
});

new CfnOutput(this, "GraphQLAPIURL", {
value: api.graphqlUrl,
});

const usersLambda = new lambda.Function(this, "UsersLambda", {
runtime: lambda.Runtime.NODEJS_14_X,
code: lambda.Code.fromAsset(path.join(__dirname, "lambda")),
handler: "main.handler",
environment: {
USER_POOL_ID: props.userPool.userPoolId,
},
memorySize: 1024,
initialPolicy: [
new iam.PolicyStatement({
actions: ["cognito-idp:ListUsers"],
effect: iam.Effect.ALLOW,
resources: [props.userPool.userPoolArn],
}),
],
});

const lambdaDs = api.addLambdaDataSource("lambdaDatasource", usersLambda);

lambdaDs.createResolver({
typeName: "Query",
fieldName: "users",
});

// lambdaDs.createResolver({
// typeName: "Mutation",
// fieldName: "deleteUser",
// });
}
}
9 changes: 9 additions & 0 deletions cloud/lib/cognitoAppsyncConstruct/graphql/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type User {
email: String!
updated_at: String!
}

type Query {
users: [User!]!
}

36 changes: 36 additions & 0 deletions cloud/lib/cognitoAppsyncConstruct/lambda/listUsers/listUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { CognitoIdentityServiceProvider } from "aws-sdk";

const UserPoolId = process.env.USER_POOL_ID;

export const listUsers: (
selectionSetList: string[]
) => Promise<CognitoIdentityServiceProvider.UsersListType> = async (
selectionSetList
) => {
if (!UserPoolId) {
throw new Error(`"USER_POOL_ID" not provided`);
}

const cognitoIdentityServiceProvider = new CognitoIdentityServiceProvider();

const result: CognitoIdentityServiceProvider.ListUsersResponse =
await new Promise((resolve, reject) => {
cognitoIdentityServiceProvider.listUsers(
{
UserPoolId,
AttributesToGet: selectionSetList,
},
(err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
}
);
});

console.log("RESULT ATTRIBUTES", result);

return result.Users || [];
};
26 changes: 26 additions & 0 deletions cloud/lib/cognitoAppsyncConstruct/lambda/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { listUsers } from "./listUsers/listUsers";

interface User {
email: string;
}

type AppSyncEvent = {
info: {
fieldName: string;
selectionSetList: string[];
};
// arguments: {
// userId: string;
// user: User;
// };
};

exports.handler = async (event: AppSyncEvent) => {
console.log("EVENT", event);
switch (event.info.fieldName) {
case "users":
return listUsers(event.info.selectionSetList);
default:
return null;
}
};
19 changes: 14 additions & 5 deletions cloud/lib/janush-auto-generated-app-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Construct } from "constructs";

import { EnvName } from "../enums/EnvName";
import { CognitoCdkConstruct } from "./authentication/cognitoCdkConstruct";
import { CognitoAppsyncConstruct } from "./cognitoAppsyncConstruct/cognitoAppsyncConstruct";

interface SingleEnvironmentProps {
envName: EnvName;
Expand All @@ -15,15 +16,23 @@ export class JanushAutoGeneratedAppStack extends Stack {
{ envName, ...stackProps }: StackProps & SingleEnvironmentProps
) {
super(scope, id, stackProps);

// The code that defines your stack goes here

// ========================================================================
// Authentication: Amazon Cognito
// ========================================================================

new CognitoCdkConstruct(this, `${envName}-CognitoUserPool`, {
envName,
const cognitoCdkConstruct = new CognitoCdkConstruct(
this,
`${envName}-CognitoUserPool`,
{
envName,
}
);

// ========================================================================
// Users Management: AppSync
// ========================================================================
new CognitoAppsyncConstruct(this, `${envName}-CognitoAppSync`, {
userPool: cognitoCdkConstruct.userPool,
});
}
}
Loading