nx/docs/shared/node-tutorial/2-project-graph.md
Zachary DeRose 1298a0f6db
docs(core): node and react tutorial rework (#12498)
* docs(react): update react tutorial text

* docs(react): fixes to computation cache lesson

* docs(react): reworking react tutorial

* docs(react): fixing broken links in tutorial

* docs(react): fixing broken more links in tutorial

* docs(react): fixing last broken link in tutorial

* docs(react): really fixing last broken link in tutorial

* fixing images in preview

* docs(react): cleaning up text and formatting issues

* docs(react): more fixes and cleanup

* docs(react): more fixes

* docs(react): fixing nx console broken links

* docs(react): adjusting ending summary cards

* docs(react): more typo fixes

* docs(react): incorporating victor and isaac's feedback

* docs(react): fixing broken link

* docs(react): a self-round of typo and formatting fixes

* docs(react): another round of formatting fixes

* docs(react): another small change

* docs(react): another typo fix

* docs(react): more typo fixed noticed working with node tutorial

* docs(react): making h1's consistent

* docs(react): fixing tab title for part 1

* docs(react): fixing the title

* docs(react): escaping colon in title

* docs(node): copying react tutorials as starting point

* docs(node): fixing map.json and links to other lessons

* docs(node): updating the copy-pasted react tutorial for the node example

* docs(node): more fixes after self-review

* docs(node): fixing another typo

* docs(node): Making h1's consistent

* docs(node): fixing tab title in step 1

* docs(node): fixing the title

* docs(node): escaping colon in title

* docs(core): nx graph => project graph

* docs(core): fixing titles

* docs(core): further shortening the text

* docs(core): formatting fixes

* docs(core): responding to victor comments

* docs(core): switching to new terminal code blocks

* docs(core): light and dark mode friendly images
2022-10-14 10:12:05 -07:00

128 lines
3.8 KiB
Markdown

# Node Tutorial - Part 2: Project Graph
Run the command: `npx nx graph`. A browser should open up with the following contents:
![Initial Project Graph](/shared/node-tutorial/initial-project-graph.png)
This is still different from the design from the start of Part 1:
![Our Workspace Requirements](/shared/node-tutorial/requirements-diagram.png)
The Project Graph is derived from the source code of your workspace. Make the following adjustments to your existing projects, so that our Project Graph will match the design:
### `products-data-client`
Update the contents of the generated `products-data-client.ts` file:
```typescript {% fileName="libs/products-data-client/src/lib/products-data-client.ts" %}
export interface Product {
id: string;
name: string;
price: number;
}
export interface ProductsDataClient {
getProducts(): Promise<Product[]>;
getProductById(id: string): Promise<Product | undefined>;
}
export const exampleProducts: Record<string, Product> = {
'1': { id: '1', name: 'Product 1', price: 100 },
'2': { id: '2', name: 'Product 2', price: 200 },
};
export function createProductsDataClient(): ProductsDataClient {
return {
getProducts() {
return Promise.resolve(Object.values(exampleProducts));
},
getProductById(id) {
return Promise.resolve(exampleProducts[id]);
},
};
}
```
### `products-cli`
Update the generated `main.ts` file of this project to import the `createProductsDataClient()` function.
Use the data client to print the product matching the id provided at the command-line. If no id was provided, print all products as an array:
```typescript {% fileName="apps/products-cli/src/main.ts" %}
import { createProductsDataClient } from '@my-products/products-data-client';
main();
async function main() {
const productsDataClient = createProductsDataClient();
const id = getProvidedId();
if (id != null) {
const product = await productsDataClient.getProductById(id);
console.log(JSON.stringify(product, null, 2));
} else {
const products = await productsDataClient.getProducts();
console.log(JSON.stringify(products, null, 2));
}
}
function getProvidedId() {
return process.argv[2];
}
```
### `products-api`
Update the generated `main.ts` file of this project to also import the `createProductsDataClient()` function.
Use the data client and Express to create an Express app with 2 GET request handlers:
```javascript {% fileName="apps/products-api/src/main.ts" %}
/**
* This is not a production server yet!
* This is only a minimal backend to get started.
*/
import * as express from 'express';
import { createProductsDataClient } from '@my-products/products-data-client';
const app = express();
const productsDataClient = createProductsDataClient();
app.get('/products', async (_req, res) => {
const products = await productsDataClient.getProducts();
res.send(products);
});
app.get('/products/:id', async (req, res) => {
const id = req.params.id;
const product = await productsDataClient.getProductById(id);
if (product == null) {
res.status(404).send();
return;
}
res.send(product);
});
const port = process.env.port || 3333;
const server = app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});
server.on('error', console.error);
```
Now run `npx nx graph` again:
{% side-by-side %}
![Matching Graph](/shared/node-tutorial/matching-graph.png)
![Our Workspace Requirements](/shared/node-tutorial/requirements-diagram.png)
{% /side-by-side %}
Your graph now matches the original design.
The Project Graph is more than just a visualization - Nx provides tooling to optimize your task-running and even automate your CI based on this graph. This will be covered in more detail in: [4: Workspace Optimization](/node-tutorial/4-workspace-optimization).
## What's Next
- Continue to [3: Task Running](/node-tutorial/3-task-running)