13

Plugging into the Dependency Graph Construction for Nx

 3 years ago
source link: https://blog.nrwl.io/plugging-into-the-dependency-graph-construction-for-nx-bc79814f5521
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Plugging into the Dependency Graph Construction for Nx

0*pQl4n3BTEmK8gC6H.png?q=20
plugging-into-the-dependency-graph-construction-for-nx-bc79814f5521
nx.dev

At its core, Nx is agnostic to what programming languages you want to include in your workspace. Every workspace has the advantage of all the same affected commands, computation caching, and consistency available to Nx workspaces. Before Nx 12, adding implicit dependencies was the only way to detect dependencies in the project graph written with other languages. Nx now provides an API to extend the project graph to add nodes and dependencies for projects not built with TypeScript or JavaScript. This post shows you how a Go project in an Nx workspace can be added to the dependency graph by extending the Nx Project Graph.

Project Graph Plugins

Nx creates a graph of all the dependencies between projects in your workspace using two sources of information:

  1. TypeScript import statements referencing a particular project’s path alias
  2. Manually defined implicit dependencies in the nx.json

These are essentially project graph plugins built into Nx. To extend to the other languages, the Project Graph API allows for plugins to add new nodes and edges to the graph.

The project graph processor has three main methods:

  • addNode — adds a new node to the project graph.
  • addDependency — adds a new dependency/edge to the project graph.
  • getProjectGraph — returns a new project graph that has been processed.

Project Graph Plugins are chained together to produce the final project graph. Each plugin may have a Project Graph Processor which iterates upon the project graph.

Adding Go projects to the dependency graph

To illustrate adding a Go project in the Nx dependency graph, let’s look at the following example from: https://github.com/nrwl/nx-go-project-graph-plugin

This repo is an example implementation of a project graph plugin. In this example, we will be adding dependencies between the Go projects in this repo to Nx’s Project Graph.

Let’s first look at the nx.json file

The repository has an existing workspace with 2 Go projects and one Go library. Without implicitDependencies, the dependencies between these projects aren’t displayed on the Nx dependency graph. The project graph plugin API provides the functionality to add these projects in a few steps.

The plugin is a JavaScript function exported as processProjectGraph that

  • Creates a ProjectGraphBuilder from the current graph which helps iteratively mutate the graph.
  • Runs a Go script that outputs only internal Go projects and their dependencies as JSON.
  • Maps the Go project names to the Nx project names.
  • Calls the ProjectGraphBuilder.addDependency function to add the dependencies of each project to the ProjectGraph.
  • Returns ProjectGraphBuilder.getProjectGraph() which is the mutated ProjectGraph.

Running nx dep-graphwith the plugin enabled produces a more accurate dependency graph.

0*OkiKKJAsPucGat8U?q=20
plugging-into-the-dependency-graph-construction-for-nx-bc79814f5521

Upon removing the dependency from the cmd-hello executable on the pkg-hello library, the updated dependency graph is accurately reflected.

0*kG4emeCTzg4_y0di?q=20
plugging-into-the-dependency-graph-construction-for-nx-bc79814f5521
1*sUx_ThrH56g52MkpwMWVaA.gif?q=20
plugging-into-the-dependency-graph-construction-for-nx-bc79814f5521
Adding Go projects to the Nx Dependency Graph

Displaying Go projects inside an Nx workspace is just one example. The new plugin API can be used for other use cases, such as drawing edges between nodes in the graph based on organization-wide conventions. We’re excited to see what new plugins and visualizations will be created using the new Project Graph API. To see the full example, visit https://github.com/nrwl/nx-go-project-graph-plugin. For more details on how to do this with other projects, check out our new docs on Extending the Project Graph.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK