Hi guys, does anyone know how to install third par...
# prisma-whats-new
a
Hi guys, does anyone know how to install third party node modules from console graphcool project?
m
@Akshay most of the common modules are already available like
moment
and
bcrypt
you just have to import them into your functions
I found that you had to use
import * as moment from 'moment'
syntax
a
Thanks @max Is there a list of common imported functions. is ‘graphcool-lib’ there as well? And what if i need more custom libraries? I know I can do all this from cli but it would be great if we can have use custom modules inside console as well
I tried the moment import but it does not work. It says unexpected reserved word in error. I replaced import with require but it says moment is undefined on logging
m
@Akshay so it's serverless so you either need to call another function or include all the code in one file. Probably best to split it out. Here's an example using `moment`:
Copy code
import { fromEvent, FunctionEvent } from 'graphcool-lib'
import { GraphQLClient } from 'graphql-request'
import * as moment from 'moment'

interface EventData {
  id: string
}

interface User {
  id: string
}

interface AccountActivationCode {
  id: string
  createdAt: Date
  user: User
}

interface ActivatedUser {
  id: string
  accountActivated: boolean
}

export default async (event: FunctionEvent<EventData>) => {
  console.log(event)

  try {
    const accountActivationCodeId = event.data.id;
    const api = fromEvent(event).api('simple/v1')

    // use the ID to get the AccountActivationCode node
    const accountActivationCode: AccountActivationCode = await getAccountActivationCode(api, accountActivationCodeId)
      .then(r => r.AccountActivationCode)

    // check if it exists
    if (!accountActivationCode || !accountActivationCode.id) {
      return { error: 'User not activate not activated 1' }
    }

    // check the time stamp - 12 hours to verify an email
    const now = moment();
    const createdAt = moment(accountActivationCode.createdAt);
    if ( moment(now).isBefore(createdAt.subtract(12,'hours')) ) {
      return { error: 'User not activate not activated 2' }
    }

    // everything checks out then set accountActivated on user to true and return true
    const activatedUser: ActivatedUser = await activateUserAccount(api, accountActivationCode.user.id)

    // check if user exists and was updated
    if (!activatedUser || !activatedUser.id) {
      return { error: 'User not activate not activated 3' }
    }

    return { data: { result: true } }
  } catch (e) {
    console.log(e)
    return { error: 'An unexpected error occured during email verification.' }
  }
}

async function getAccountActivationCode(api: GraphQLClient, id: string): Promise<{ AccountActivationCode }> {
  const query = `
    query getAccountActivationCode($id: ID!) {
      AccountActivationCode(id: $id) {
        id
        createdAt
        user {
          id
        }
      }
    }
  `
  const variables = { id }
  return api.request<{AccountActivationCode}>(query, variables)
}

async function activateUserAccount(api: GraphQLClient, id: string): Promise<ActivatedUser> {
  const mutation = `
    mutation updateUser($id: ID!, $accountActivated: Boolean!) {
      updateUser(id: $id, accountActivated: $accountActivated) {
        id
      }
    }
  `
  const variables = { id, accountActivated: true }
  return api.request<{updateUser: ActivatedUser}>(mutation, variables)
  .then(r => r.updateUser)
}
not sure if there is a list of supported modules. I never had a problem...
@Akshay oh it's been a while since I used the framework but I think you might need to include
moment
in your package.json file under dependencies.
That could possibly be how gc framework grabs the module for you
Copy code
"dependencies": {
    "@types/aws-sdk": "^2.7.0",
    "@types/bcryptjs": "^2.4.1",
    "@types/form-data": "^2.2.0",
    "@types/validator": "^6.3.0",
    "Base64": "^1.0.1",
    "aws-sdk": "^2.173.0",
    "bcryptjs": "^2.4.3",
    "form-data": "^2.3.1",
    "graphcool-lib": "^0.1.4",
    "graphql-request": "^1.4.0",
    "isomorphic-fetch": "^2.2.1",
    "moment": "^2.19.1",
    "uuid": "^3.1.0",
    "validator": "^9.0.0"
  },
a
yes but the thing is I am working from the dashboard @max ,where is the package .json there? I can do everything like this from code generated from graphcool-init
m
oh there isn't because I was using the cli
the cli workflow is a lot nicer as you can save your config to github
a
yes. thanks for the trouble anyways. it will be a nice feature to have it in console dashboard as well
m
good luck