GraphQL Products API
Simple GraphQL API that serves the product data in products.json
.
Requirements for local development
- Docker
- NodeJS >= 18.15.0
- yarn
Setup
- Clone this repository
git@github.com:dulcehc/graphql_api.git
- Make sure you have the .env file, please see the env.example for the values you need to add.
a) Running API app from local
-
Within the project directory, install dependencies with:
yarn
-
Execute
docker-compose up
to spin up the database service. -
Update .env file for DATABASE_URL, make sure to have localhost as the host and use the external port for the DB.
DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:${DB_PORT_EXT}/${POSTGRES_DB}?schema=public"
-
Execute
yarn run db:migrate:db
to set the database schema. After you make any changes to the schema, execute the migration again. -
Execute
yarn db:seed:dev
to populate the DB with the initial products from the JSON file. -
Execute
yarn run dev
to start the Graphql API. -
Open the browser http://localhost:9090 to star making queries and mutations.
b) API deployment
-
Stop any previous containers with
docker compose down
-
Make sure to have the correct DB host for the container, in this case is
db
, the name of the service.
DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:${DB_PORT_EXT}/${POSTGRES_DB}?schema=public"
-
Execute
docker compose build
and thendocker compose up
-
Open the browser http://localhost:9090 to star making queries and mutations.
Query examples
Once you open the graphical interface:
a) Find product by id
query GetProductByCode($code: String!) {
findProductByCode(code: $code) {
id,
description,
image
price
quantity
code
}
}
In the variables section, add the code value that you want to search.
b) Update a product, in this example, just changing the price.
mutation Mutation($code: String!, $price: Float) {
updateProduct(code: $code, price: $price) {
id,
code,
description,
price
}
}
If you want to see the database, you can execute locally yarn run dev:studio
.