Go back to 8base Academy
October 1, 2019

Generating Serverless Functions in the CLI

Command-Line (CLI)
Serverless Functions
Sebastian Scholl

* This is an automated transcript. Please excuse inaccuracies.

Hey everyone, so in this video I'd like to go over generators, specifically how do we use generators and the CLI to scaffold serverless functions as well as projects. Let's jump right into it and start playing in our command-line interface. So, here in the CLI I've already configured it to communicate with the appropriate workspace where I deploy these functions. However, just as a quick reminder you can do that by running 8base config which is then is going to go retrieve all the workspaces that you have available and you can select which one you want to be configured alright.  


So, the first thing that we're going to want to do is look at scaffolding a new project, but only provisioning it with certain functions. So, for example if I were to run 8 base it and then give it a name like “Jen demo”. If I ran this it would actually give me 4 functions by default of each type; so, it would give me a resolver, a trigger, a web hook and a task. However, if I wanted to, I could do two cool things first thing I'd like to say - F and then give it a list of different types of functions. So, for example, let's say I wanted a resolver called, “Hello World”. Not that Hello World is not used enough, however we're going to use it for now. And I also wanted another function which was going to be a let's say a web hook called “Machine Callback”. I could just do F, give it that definition and then the one thing I'm also going to specify here is by default all these functions are going to be generated in typescript. But if I want to change my syntax to be JavaScript, I could just do it like that, run the command and cool. As we can see it generated a new project for me with the two types of functions that I wanted, as well as it did them in JavaScript instead of typescript.  


So, let's open this project now and take a little peek at what was generated for us. So first off, I'm going to move into the directory called, “Gen Demo”, then I'm going to open it in Sublime. So, if I do that I go in here we can see that it created directories for me called resolvers and webhooks, depending on the function type which in we have a Hello World directory which has the handler as well as the schema, since the resolver and then some mock file in a mocks directory. So, first off if we go to our handler, we can see that's just a simple asynchronous function that we generated which returns what the resolver received from the data on the data argument. And if we were to look at our schema, we can see that it created the Hello World schema with for both the result type or response type as well as the function definition which is a query. And then in the request, it gave us the structure that it expects in production for the arguments are the functions argument right. So, this would be the data that gets sent to the request.  


What's cool about this is, that right now if we were to take this command from the document or from the encode documentation we could actually run and execute this function. So, I run that, it's invoking it and it just invoked it locally. So, all of this is set up ready for you start developing with and this is the same for the web hook which has a handler, web hooks, are since they're real HTTP calls, they have to have a certain type of response object which can read more about in the documentation- and all the definitions for these functions were created in our 8base.EML file.  


So, this generator was used when the project was created. However, for a project that's already been created how do we use generators to add functions to that project? To start, let's go back to our command line and start writing out the generator command. So, let's clear a screen and I'm just going to bring this down to the middle of the screen and so let's run okay. So, 8base generate and function- not function excuse me. First, let's look at the types of generators that we have. So, if we run help, we can see that we have okay we can generate a mock, a resolver, task trigger, webhook, these are the ones that we're going to pay attention to right now. So, let's see what happens if we wanted to say okay well let's generate a web hook and pass in help again, so cool. On the web hook we can specify a few things like the different options and needs, what's going to be the path, what's going to be the method as well as do we want to include mocks or not and as well as what's syntax, we want it in.  


Let's do this example once again with a resolver. So, if we wanted to say, “Hey, 8base generated a new resolver and we want it to be in Type Script. So, we're going to say the syntax is TS and the resolver’s name is going to be “get all users by ID”, let's say. So, we run that and cool, it updated our 8base EML file and it generated the graph QL file the Mach file as well as the handler. So, if we go back to when we reload here we can see that, hey this updated our get all users by ID and inside here, we can see that now we have a Type Script generated resolver function right with a typed response, we also have our schema graph QL file which we could now change or edit as well as the mock file which allows us to executed immediately locally. So, if we were to copy this and paste into our terminal, it invokes that function that we just generated.  

And what's cool about this too is, like let's say we wanted to run 8base deploy right. So, this will just take a minute. But essentially what it's doing or it's compiling our type from script functions, as well as JavaScript functions and deploying them to our workspace. Just as they were generated, they are production ready; meaning that they can run and execute. Naturally you probably want to change your functions to actually be domain-specific. However, we like to think that immediately is when you generate a function it's ready to be used. So, let's look at what happens once we run deploy.  


So, after about our new project deployed to the workspace that. We had set up we now know that our functions were deployed because we could actually invoke them from the command line. So, if we ran our same function to get all users by ID that we just created and as well as passed the same mark that we have locally, but called invoke instead of invoke local- we can run that command and see that it invoked and returned the exact same response that we would have expected, based on the mock that we're giving it. And if we go over to our workspace and reload it, we can now see that if we go to the “logic view” which is right here we've deployed three custom functions; two resolvers and one webhook.  


Here we can see a little bit of information about the function that's been deployed based on its type for example this is the endpoint to invoke the web hook machine callback that we created. However, if we do go to the logs, we can also see the last time that we invoked this function as well as how much memory consumed, how long it took and all that other information.  


So, I hope this was a helpful way to get an understanding of how to use generators from the command line to really quickly generate, deploy as well as a having opportunity to jump in and change custom functions and get them working in your 8base workspace. I'm going to include some links to the documentation in the description of this video, so you have some more resources to jump deeper into. However, if you have any questions, please leave them in the comments section of this video and I hope you have a great rest of your day. Thank you.

Share this post on social media!

Ready to try 8base?

We're excited about helping you achieve amazing results.