Git Product home page Git Product logo

Comments (16)

malz avatar malz commented on July 30, 2024 2

I've been playing around with this for a little while now, as well as diving into other projects that do something similar like serverless-offline which seems to use babel to get the output path it looks like. They set the 'location' at startup time. I think this could be done similarly when loading in the serverless config.

This would also enable the opportunity to put in the serverless directory in the config as well. Let me know your thoughts on this. The other way to do this is to use variables however that doesn't seem like as clean of a solution to me as handling would need to be added in lambdaSource.js to cater for it.

from aws-utils.

malz avatar malz commented on July 30, 2024 1

Since serverless-offline appends the webpack location to the function handler (see: index.js) using Babel I believe, putting an environment variable in the handler would break functionality with serverless-offline and serverless-webpack.

from aws-utils.

SET001 avatar SET001 commented on July 30, 2024 1

@cbaron but good that you mentioned readme, from where it should be buildPrefix, not modulePrefix. My problem is solved )

from aws-utils.

cbaron avatar cbaron commented on July 30, 2024

@amalow -- Hello!

So glad that people are using the emulator! I'll bring up your use case with the team and look at invoking a lambda method implemented in TS, but I'm curious about a few things.

Its unclear what you mean by Typescript (therefore webpack) -- could you give me some more information about how you're integrating TS with Webpack?

Why is manually prepending "./webpack/service" not the best way to do it?

If we are running a lambda method in javascript on AWS, I would expect you to have some process which compiles Typescript to Javascript. I would expect that this javascript should be invoked by the emulator, as it will also be invoked by lambda. But perhaps I'm missing something?

from aws-utils.

malz avatar malz commented on July 30, 2024

Hi @cbaron

We use serverless-webpack to handle automatically transpiling our typescript code into Javascript, by default this plugin writes out to ./webpack/service however that can be configured in the project's webpack config so prepending would not catch all cases if someone had customised their configuration. I'm thinking the best way to do this is to add an option in the project's serverless.yml that indicates the executable code directory is different to the default?

Perhaps this is out of the scope of this plugin I'm not too sure.

from aws-utils.

cbaron avatar cbaron commented on July 30, 2024

Got it. Since we're using serverless in our emulator, you should be able to use you're own variables when defining the location of your lambda data source.

Let me know if that solves your use case!

from aws-utils.

cbaron avatar cbaron commented on July 30, 2024

I'm confused how variables will not give you a clean solution. Can you give me an explicit example of your ideal use case so that we can implement a solution?

from aws-utils.

malz avatar malz commented on July 30, 2024

I'm happy to put up a PR with an implementation. The variable method I'm suggesting would be something like:

custom: appsync-offline: port: 60004 modulePrefix: '.webpack/service' In serverless.yml

and then in lambdaSource.js

const lambdaSource = async (
  {
    dynamodbEndpoint,
    dynamodbTables,
    serverlessConfig: { functions, custom },
    serverlessDirectory,
  },
  fn,
  { payload },
) => {
  const fnConfig = functions[fn];
  const [handlerPath, handlerMethod] = fnConfig.handler.split('.');
  let modulePrefix = '';
  if (custom['appsync-offline'] && custom['appsync-offline']['modulePrefix']) {
    modulePrefix = custom['appsync-offline']['modulePrefix'];
  }

  const fullPath = path.join(serverlessDirectory, modulePrefix, handlerPath);

  // rest unchanged
}

Let me know what you think of this solution, I'm happy to put a PR up with it if it's acceptable.

from aws-utils.

cbaron avatar cbaron commented on July 30, 2024

Hmm, If I understand your use case correctly, it can be solved by utilizing environment variables in serverless.

Please let me know if I'm missing something.

from aws-utils.

cbaron avatar cbaron commented on July 30, 2024

Ahh, got it, your PR suggestion looks good to me.

from aws-utils.

malz avatar malz commented on July 30, 2024

Closing this as PR is merged in.

from aws-utils.

SET001 avatar SET001 commented on July 30, 2024

I may not correct undetstand how it suppose to work but with this config:

custom:
  accountId: 565955521116
  appsync-offline:
    port: 60004
    modulePrefix: 'dist/service/'
functions:
  getReservations:
    handler: handler.getReservations
  getTransactions:
    handler: handler.getTransactions

i'm getting error in emulator:

✖ error Query.getReservations failed
✖ error undefined

still, the only way how it does work for me is directly setting path to compiled code:

functions:
  getReservations:
    handler: dist/service/src/handler.getReservations
  getTransactions:
    handler: dist/service/src/handler.getTransactions

which is not good for me because I can't deploy serverless.yml in such state and I can't push it to git repo...

from aws-utils.

cbaron avatar cbaron commented on July 30, 2024

appsync-offline only handles modulePrefix -- it does not know what to do with port

Perhaps you need something like:

serverless-offline
  port: 60004

@amalow -- care to chime in ?

from aws-utils.

SET001 avatar SET001 commented on July 30, 2024

I don't actually use appsync-offline. Should I?

from aws-utils.

cbaron avatar cbaron commented on July 30, 2024

Its not necessary. After looking at your .yml file, I think you should try:

custom:
  accountId: 565955521116
  appsync-emulator:
    modulePrefix: dist/service
functions:
  getReservations:
    handler: handler.getReservations
  getTransactions:
    handler: handler.getTransactions

Please look at the README! There is documentation surrounding this feature.

from aws-utils.

SET001 avatar SET001 commented on July 30, 2024

no, it does not work. The error is the same:

● start graphql
  {
    getReservations {
      name
      transactions(limit: 1) {
        id
        cost
      }
    }
  }
  
● start Resolve: Query.getReservations [getReservations]
ℹ info Rendered Request:

  { version: '2017-02-28',
    operation: 'Invoke',
    payload: { field: 'getReservations', arguments: {}, source: {} } }
ℹ info Dispatch to source
  { name: 'getReservations', type: 'AWS_LAMBDA' }
✖ error Query.getReservations failed
✖ error undefined

I'm getting this error after running graphQl query from graphql playground

from aws-utils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.