trilogy-group / tu2k22-zhiyuangao

0 stars 0 forks source link

GraphQL #30

Closed gao-ti closed 2 years ago

gao-ti commented 2 years ago
gao-ti commented 2 years ago

1. Lambda authorizer for authenticating AppSync GraphQL request

arn:aws:lambda:us-east-1:280022023954:function:zhiyuangao-graphql

const axios = require("axios");

exports.handler = async (event, context, callback) => {

  console.log("%% auth event %%", event);
  console.log("%% auth context %%", context);
  console.log("%% auth callback %%", callback);

  const op = event.requestContext.queryString;

  const ops = [
      "getSectors", "getStocks", "loginUser", "createUser",
      "getSectorById", "getStockById", 
  ]

  console.log("OP is", op)

  ops.forEach( (operation) => {
    if (op.includes(operation)) {
        console.log("$$ match success ", operation);

        callback(
          null,
          { "isAuthorized": true }
        )
    }
  })

  const token = event.authorizationToken
  console.log("### Token", token);
  let isAuthorized = false;

  let headers = {
    "Accept": "application/json",
    "Content-Type": "application/json",
    "Authorization": "Token " + token
  }

  try {
    const result = await axios.get("https://8080-trilogygrou-tu2k22zhiyu-sq8b22pxndn.ws.legacy.devspaces.com/api/v1/users/profile", { headers: headers });

    console.log(result);

    if (result.status == 200) {
        isAuthorized = true;
    }

  } catch(error) {
    console.log(error);
  }
  const response = {
    "isAuthorized": isAuthorized,
    "resolverContext": {
      "token": token
    }
  }
  console.log("Resposne is::", response);

  callback(null,response);
};

resolver

const axios = require('axios');

exports.handler = async (event, context, callback) => {
    console.log("context", context);
    console.log("event", event);

    const baseUrl = "https://8080-trilogygrou-tu2k22zhiyu-sq8b22pxndn.ws.legacy.devspaces.com/api/v1";

    console.log("EVENT", event);

    let headers = "";

    try {
       headers = {Authorization: `Token ${event.resolverContext.token}`}
    } catch (err) {
        console.log("Non token request recvd", err);
    }

    switch(event.field) {
        case 'getSectors':
        {
            const {data} = await axios.get(`${baseUrl}/sectors/`)           
            return data;
        }
        case 'getStocks':
        {
            const {data} = await axios.get(`${baseUrl}/stocks/`)           
            return data;
        }
        case 'getSectorById':
        {
            const {data} = await axios.get(`${baseUrl}/sectors/${event.arguments.id}/`)           
            return data;

        }
        case 'getStockById':
        {
            const {data} = await axios.get(`${baseUrl}/stocks/${event.arguments.id}/`)      
            return data;

        }
        case 'getUser':
        {
           const {data} = await axios.get(`${baseUrl}/users/profile/`, {headers})           
           return data;
        }
       case 'createUser':
        {
           const {data} = await axios.post(`${baseUrl}/auth/signup/`, event.arguments.input)           
           return data;

        }

        case 'createSector':
        {
           const {data} = await axios.post(`${baseUrl}/sectors/`, event.arguments.input, {headers})           
           return data;
        }

        case 'createStock':
        {
            const {data} = await axios.post(`${baseUrl}/stocks/`, event.arguments.input, {headers})           
            return data;

        }
        case 'loginUser':
        {
            const {data} = await axios.post(
                `${baseUrl}/auth/login/`,
                event.arguments.input,
            );           
            return data;

        }
    }

    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };

    return response;
};
gao-ti commented 2 years ago

2. Create Mutations for creating user, stock and sector using lambda resolver and existing backend

image image

gao-ti commented 2 years ago

3. Create Queries for fetching user, stock and sector using lambda resolver and existing backend

https://us-east-1.console.aws.amazon.com/appsync/home?region=us-east-1#/3fxgqt3tenaodd6lw5idpoqdde/v1/queries

image

vinayak-sachdeva commented 2 years ago

Reviewed!