Quick Start with CLI
🖥🤔⌨️👏🚀
Last updated
🖥🤔⌨️👏🚀
Last updated
This guide will introduce some basic code.store concepts like services and schema generation using CLI. The goal of this quick start is to get you up and running fast, so let's not waste any time and start!
If you haven't done this already, then click on this link.
We invite you to follow this guide and install our command-line interface:
Once you have your account and the CLI is installed, we can finally do some interesting stuff!
In order to work with the CLI, you have to connect it with your account by launching the following command:
This command is going to launch your default browser and ask for your login details. Once you are authenticated, we can go on and create a new service.
You can check the contents of your new service directory by running ls -lh ./
Let's get into the details of each file and directory.
The root directory contains two files and one folder:
package.json
– it is a standard NPM configuration file. Feel free and add any NPM packages you like using npm install {packageName}
;
codestore.yaml
– contains your service ID and will contain more configuration options in later versions;
src/
– this is where all the source code lives.
Let's dive into the src/
directory:
schema.graphql
– one of the most (if not the most) important files. It contains a GraphQL schema of your service;
data/
– this directory contains TypeORM entities. You can automatically generate TypeScript classes for your database tables using cs generate:models -p src/data
command;
resolvers/
– this is where your business logic lives. Resolvers serve two purposes: connect your GraphQL objects to data in the database and is a place where you implement any additional business logic.
When you create a new service, we are generating it with default schema.graphql
which contains the following code:
It is a very basic schema of an API that has a single Query
called helloWorld
(which doesn't accept arguments) and which returns a single output of type string
.
If you are not comfortable with GraphQL syntax, we are inviting you to read our GraphQL quick-start guide first and if you want to go deeper into that rabbit-hole 🐰then the official GraphQL documentation https://graphql.org/learn/.
We can test this query by running the following curl command in your terminal:
Hopefully, we should get "Hello, World!" response (something looking like that {"data":{"helloWorld":"Hello, World!"}}
) in our terminal 🤞
Let's take a look at the resolver (business logic) for this query which is located in the file src/resolvers/queries/helloWorld.ts
:
This is a basic code which does few things:
we are importing a logger module which is going to output messages to the local console or to our cloud log storage;
we are importing a Resolver, which is an Interface for a function type; it is not a necessary step but it is a useful one if you want to see type hints in your IDE;
if your code editor cannot find 'codestore-utils' then simply run npm install
in the service directory root.
When starting the service, code.store loads all files from src/resolvers/mutations
and src/resolvers/queries
directories and passes them to the GraphQL server. The filename of the resolver must be equal to the name of a query or a mutation. Each query and mutation in theschema.graphql
file must have a corresponding resolver in the filesystem.
This should give you a basic understanding of how GraphQL schema and resolvers work together and let's write something more meaningful in the next chapter.
The local development workflow is still in early beta and we are working hard to bring you the most stable and comfortable local development experience. If you notice any issues, please ping us in our community.
Local development comes in handy if you don't want to launch codestore push
after each modification and then wait until it finishes.
In order to use local development workflow:
install PostgreSQL database locally; you can do it quite easily either https://postgresapp.com/ or via Docker;
configure your local settings in codestore.yaml:
launch a local development server by running codestore dev
codestore dev
will try to run "npm install" to make sure that the dependencies of your service are installed but you can also launch it manually if you wish.
After launching codestore dev
you should see the following output:
Voila ! Let's try and test our local server:
You should get a "Hello, World!" message if everything works properly 🤞
We are using lots of curl
commands to query the web-services in this Quick Start, however, you can also GraphQL Playground by clicking on the link of the service, i.e. http://localhost:3000/graphql for the local environment, or https://api.code.store/{service URL hash}/graphql for remote environments.
We can finally write something more or less meaningful! For the sake of simplicity, we decided to implement a well-beaten example of a Blog-post. We are going to be using the local development server during this example, so make sure that you have a PostgreSQL database ready.
First of all, we should modify our schema.graphql
file:
Every GraphQL schema has a query type and may or may not have a mutation type. Read more about GraphQL in our quick start guide here, or in the official GraphQL documentation.
We have added two types and two queries in the schema and we can now go on and add the resolvers. Let's begin with the query allPosts
: create a file src/resolvers/queries/allPosts.ts
and initialize it with the following code:
Each GraphQL query or mutation must be mapped to a resolver. In order to find a resolver for a query/mutation, code.store is using a file structure mapping to find a corresponding resolver.
The rules are simple:
all queries should be placed into src/resolvers/queries/{queryName}.ts
files,
all mutations into src/resolvers/mutations/{mutationName}.ts
For example, resolver for a mutation createUser
should be placed into src/resolvers/mutations/createUser.ts
file.
Here we go, this is our first test resolver which is not yet connected to anything but which already can return the data! One last step before running the deployment command - we have to remove src/resolvers/queries/helloWorld.ts
or simply rename it to helloWorld.ts_
. And that's it, you can now run your application locally via codestore dev
and test it via CURL:
GraphQL queries and mutations in your schema.graphql
should match 1-to-1 the queries and migrations in the file system, i.e. if you have a query "test" in a schema and you don't have in the file system, the service will throw an error. The same in the opposite order, if you have a resolver in the file system but not in the schema.
Until now we were not using any database at all and the time has come to grab a beer create one. The cool thing is that code.store can generate the database automatically based on the GraphQL schema you provided! In order to generate the entities locally, we should run codestore generate:models -p src/data
command which will generate the entities based on your schema. As soon as it finishes the generation, let's modify our resolver and add some database queries:
For every type in your GraphQL schema, code.store performs two operations:
it generates database migrations (using TypeORM)
it generates TypeORM entities which it stores in src/data/entities/{TypeName}.ts
Few things have changed here:
we are importing TypeORM Repository and our generated Post entity
we are initializing the repository and performing a find query for our Post entity
As soon as we run the application with codestore dev
, we can test it again and see that it's returning an empty array as we haven't created anything yet 🤦🏽♀️.
Time to add the first mutation.
First of all, modify your schema.graphql to look like the following:
This is what has changed, we added a type Mutation to our GraphQL schema containing a mutation createPost
. Now we can implement our resolver:
Now launch codestore dev
again and run some queries (we are going to be using a http command which is similar to cURL but provides a better output):
So now we have a working service on our local machine and it would be great to have it deployed somewhere in the cloud. Let's do exactly that and deploy the service to the private environment by running codestore push
command. As soon as finished, you can launch the queries in your private environment!
Not sure if it was quick 😉 but we hope that after this guide you have a good understanding of how code.store works. Feel free to check the rest of the documentation and contact us in the community chat if you will have any questions!
Happy coding and may the force be with you! 🦾