hi guys, some of you have had issues importing the...
# prisma-whats-new
e
hi guys, some of you have had issues importing the aws-sdk to a custom resolver?
import aws from 'aws-sdk'
I got it is undefined and I installed it already. Thanks in advance
m
hey @ed I am not 100% sure, but
aws-sdk
may not have default expose… 🤞 (very unlikely). You can still, though, try importing a concrete module of
aws-sdk
, S3, for example.
Copy code
import { S3 } from 'aws-sdk'
e
thanks @matic, So, how can we set our custom credentials?
m
credentials? Like
keys
etc…? afaik each module should support custom configuration. S3 for example, handles it like this:
Copy code
const s3client = new S3({
  accessKeyId: process.env.S3_ACCESS_KEY_ID,
  secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
  params: {
    Bucket: process.env.S3_BUCKET,
  },
})
ok, no, I think that the problem should be somewhere else -
aws-sdk
does expose a global module https://github.com/aws/aws-sdk-js
@ed can you share other parts of your code?
e
@matic importing only the s3 module and putting the credentials inside worked, however importing the whole module returns undefined, I dont think thats an aws-sdk issue due to I did the same code but using the global module in frontend and worked great, maybe graphcool is blocking that import. Thank you so much for the help fast parrot
m
@ed no problem, I am happy to help! Could you still share the code which caused the problem, so we can try to debug it and find if there’s an underlying problem? 🙂
e
Copy code
import { fromEvent, FunctionEvent } from 'graphcool-lib'
import { GraphQLClient } from 'graphql-request'
import AWS from 'aws-sdk'
import { v4 } from 'uuid'

interface EventData {
  fileType: string
}
export default async (event: FunctionEvent<EventData>) => {
  try {
    AWS.config.update({
        accessKeyId: __ACCESS_KEY__,
        secretAccessKey: __SECRET_KEY__,
        region: __REGION__
    })
    const graphcool = fromEvent(event)
    const api = graphcool.api('simple/v1')
    const { fileType } = event.data
    const s3 = new AWS.S3()
    const key = v4()
    const params = {
      Bucket: `cosporter`,
      Key: key,
      Expires: 60,
      ContentType: fileType && fileType
    }
    const url = await s3.getSignedUrl('putObject', params)
    return { data: {url, key} }
  } catch (e) {
    return { error: `An unexpected error occured. ${e}` }
  }
}
💚 1
So, that code doesnt work as
AWS
is undefined, even if I do console.log(AWS) i got undefined
m
@ed in the graphcool-framework functions you need to do
import * as aws from 'aws-sdk'
👍 1
see the example above
e
thanks @max that works too