Create a Serverless Function and test it locally.
Instructor: [0:00] The recommended way to extend a GraphQL API or a GraphQL end point radar is to use serverless functions. To create one, we need to first of all install the CLI command for creating a serverless function.
[0:14] I'm just going to paste this right here. What it basically does is that it runs on npm command to install Azure Functions called tools globally, which is basically a CLI tool that helps you generate function templates and then also, run your functions when you finish creating them.
[0:34] If you want to run them locally to test them, this is the tool you need. I am not going to install the CLI tool because I already have it installed. Once you have this installed, the next thing we should do is to actually create a serverless function using this CLI tool.
[0:51] Type funkinit, dot, where funk is basically the tool we just installed. Once you install it, funk becomes available in your path. Then the init command creates a new function. Then the dot basically says, "Hey create this function inside the folder we are currently in."
[1:12] Once I hit enter it will ask me which language I want to write this function in. Since we're working with JavaScript, I'll go with two and then hit enter as well. Then it'll ask you which flavor of JavaScript you want, JavaScript or Typescript.
[1:26] I'm going to keep this simple and go with JavaScript. This would generate some configuration files for use. If you hit LS, you should see that it's created a host.jason in looker.settings.json and the package.json.
[1:43] The configuration files we've seen are just metadata for our functions. We actually need to create a .JS file which is going to serve as an executable code for our serverless function project. To do that type funk new, hit enter. This would go ahead to put all the possible templates that you could create serverless function with.
[2:06] In this case, we just want to use the HTTP trigger, which is the number eight. If you hit eight and press enter, now you have to give this function a name. In this case, want to name this function insert user, and hit enter.
[2:22] This would create a boilerplate code for us. If we take a look at the editor we have here, in the insert user folder you can see the index.js file which has the generated code for you.
[2:36] Now, what this code does is that it receives a request, of course. This request, it has to have a name on either the body or the query of the request. If it does, would send out a response which is a greeting saying, hello, with whatever you passed in. If it doesn't, would send out an error asking you to please pass in a name to the query string or in the request pool.
[3:05] Now, let's see what happens when we run this function. If you run func_start, hit enter, give it a few seconds it should get your URL. Now, copy this URL, go to the browser and hit enter. Now, you should see we're getting this error which we have here.
[3:28] I want to make this request succeed. We have to pass in a name just like the function said. Passing a name, hit enter you should get a greeting that says, "Hello, Chris."
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!