Skip to content

Commit 2b09b15

Browse files
feat: add Apollo GraphQL workspace (#95)
1 parent caa5db9 commit 2b09b15

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+20337
-0
lines changed

apollo-graphql/.gitignore

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# compiled output
4+
dist
5+
tmp
6+
out-tsc
7+
8+
# dependencies
9+
node_modules
10+
11+
# IDEs and editors
12+
/.idea
13+
.project
14+
.classpath
15+
.c9/
16+
*.launch
17+
.settings/
18+
*.sublime-workspace
19+
20+
# IDE - VSCode
21+
.vscode/*
22+
!.vscode/settings.json
23+
!.vscode/tasks.json
24+
!.vscode/launch.json
25+
!.vscode/extensions.json
26+
27+
# misc
28+
/.sass-cache
29+
/connect.lock
30+
/coverage
31+
/libpeerconnection.log
32+
npm-debug.log
33+
yarn-error.log
34+
testem.log
35+
/typings
36+
37+
# System Files
38+
.DS_Store
39+
Thumbs.db
40+
41+
.nx/cache
42+
.nx/workspace-data
43+
44+
vite.config.*.timestamp*
45+
vitest.config.*.timestamp*

apollo-graphql/.prettierignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Add files here to ignore them from prettier formatting
2+
/dist
3+
/coverage
4+
/.nx/cache
5+
/.nx/workspace-data

apollo-graphql/.prettierrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

apollo-graphql/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Tutorial: Using Apollo GraphQL in an Nx Workspace
2+
3+
Source code for the Apollo GraphQL article on the Nx blog:
4+
- Tutorial: https://nx.dev/blog/using-apollo-graphql-in-an-nx-workspace
5+
6+
## What's inside?
7+
8+
This example contains a GraphQL api app, a React frontend app, code generation with GraphQL Codegen that started from the `node` preset.
9+
10+
```
11+
npx create-nx-workspace@latest --preset=node nx-apolloe
12+
```
13+
14+
It contains
15+
16+
- an `api` application using Apollo Server: `apps/api`
17+
- a `frontend` application using React: `apps/frontend`
18+
- two libraries: `libs/models-graphql` which contains the GraphQL schema and generated TypeScript models and `libs/feature-sets` which has container components that fetch data for the frontend.
19+
- Uses Graphql Codegen to generate TypeScript code from GraphQL schemas and operations
20+
21+
Follow through the tutorial linked above for more details.
22+
23+
## How to run it
24+
25+
Clone it locally, install all dependencies using `npm install`. You can then run commands Like
26+
27+
- `npx nx serve api` to serve the backend
28+
- `npx nx serve frontend` to serve the frontend
29+
- `npx nx run-many -t codegen` to generate code using GraphQL codegen
30+
- you can use `npx nx graph` to visualize the structure

apollo-graphql/apps/api/codegen.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { CodegenConfig } from '@graphql-codegen/cli';
2+
3+
const config: CodegenConfig = {
4+
schema: 'libs/models-graphql/src/lib/schema.graphql',
5+
generates: {
6+
'apps/api/src/__generated__/resolvers.ts': {
7+
plugins: ['add', 'typescript-resolvers'],
8+
config: {
9+
useIndexSignature: true,
10+
content: 'import * as types from "@nx-apollo/models-graphql"',
11+
namespacedImportName: 'types',
12+
},
13+
},
14+
},
15+
};
16+
export default config;

apollo-graphql/apps/api/project.json

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "api",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "apps/api/src",
5+
"projectType": "application",
6+
"tags": [],
7+
"targets": {
8+
"build": {
9+
"executor": "@nx/esbuild:esbuild",
10+
"outputs": ["{options.outputPath}"],
11+
"defaultConfiguration": "production",
12+
"options": {
13+
"platform": "node",
14+
"outputPath": "dist/apps/api",
15+
"format": ["esm"],
16+
"bundle": false,
17+
"main": "apps/api/src/main.ts",
18+
"tsConfig": "apps/api/tsconfig.app.json",
19+
"assets": ["apps/api/src/assets"],
20+
"generatePackageJson": true,
21+
"esbuildOptions": {
22+
"sourcemap": true,
23+
"outExtension": {
24+
".js": ".js"
25+
}
26+
}
27+
},
28+
"configurations": {
29+
"development": {},
30+
"production": {
31+
"esbuildOptions": {
32+
"sourcemap": false,
33+
"outExtension": {
34+
".js": ".js"
35+
}
36+
}
37+
}
38+
}
39+
},
40+
"serve": {
41+
"executor": "@nx/js:node",
42+
"defaultConfiguration": "development",
43+
"dependsOn": ["build"],
44+
"options": {
45+
"buildTarget": "api:build",
46+
"runBuildTargetDependencies": false
47+
},
48+
"configurations": {
49+
"development": {
50+
"buildTarget": "api:build:development"
51+
},
52+
"production": {
53+
"buildTarget": "api:build:production"
54+
}
55+
}
56+
},
57+
"codegen": {
58+
"command": "npx graphql-codegen --config {projectRoot}/codegen.ts"
59+
}
60+
}
61+
}

apollo-graphql/apps/api/src/__generated__/resolvers.ts

+114
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apollo-graphql/apps/api/src/assets/.gitkeep

Whitespace-only changes.

apollo-graphql/apps/api/src/main.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { ApolloServer } from '@apollo/server';
2+
import { startStandaloneServer } from '@apollo/server/standalone';
3+
import { Set } from '@nx-apollo/models-graphql';
4+
import { readFileSync } from 'fs';
5+
import { join } from 'path';
6+
import { Resolvers } from './__generated__/resolvers';
7+
8+
// Note: this uses a path relative to the project's
9+
// root directory, which is the current working directory
10+
// if the server is executed using `npm run`.
11+
const typeDefs = readFileSync(
12+
join('libs/models-graphql/src/lib', 'schema.graphql'),
13+
{ encoding: 'utf-8' }
14+
);
15+
16+
const sets: Set[] = [
17+
{
18+
id: 1,
19+
name: 'Voltron',
20+
numParts: 2300,
21+
year: '2019',
22+
},
23+
{
24+
id: 2,
25+
name: 'Ship in a Bottle',
26+
numParts: 900,
27+
year: '2019',
28+
},
29+
];
30+
31+
// Resolvers define how to fetch the types defined in your schema.
32+
// This resolver retrieves books from the "books" array above.
33+
const resolvers: Resolvers = {
34+
Query: {
35+
allSets: () => sets,
36+
},
37+
Mutation: {
38+
addSet: (parent, args) => {
39+
const newSet = {
40+
id: sets.length + 1,
41+
name: args.name,
42+
year: args.year,
43+
numParts: +args.numParts,
44+
};
45+
46+
sets.push(newSet);
47+
48+
return newSet;
49+
},
50+
},
51+
};
52+
53+
// The ApolloServer constructor requires two parameters: your schema
54+
// definition and your set of resolvers.
55+
const server = new ApolloServer({
56+
typeDefs,
57+
resolvers,
58+
});
59+
60+
// Passing an ApolloServer instance to the `startStandaloneServer` function:
61+
// 1. creates an Express app
62+
// 2. installs your ApolloServer instance as middleware
63+
// 3. prepares your app to handle incoming requests
64+
const { url } = await startStandaloneServer(server, {
65+
listen: { port: 4000 },
66+
});
67+
68+
console.log(`🚀 Server ready at: ${url}`);
69+
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"lib": ["es2020"],
5+
"target": "es2020",
6+
"module": "esnext",
7+
"moduleResolution": "node",
8+
"esModuleInterop": true,
9+
"outDir": "../../dist/out-tsc",
10+
"types": ["node"]
11+
},
12+
"include": ["src/**/*.ts"],
13+
"exclude": []
14+
}

apollo-graphql/apps/api/tsconfig.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"files": [],
4+
"include": [],
5+
"references": [
6+
{
7+
"path": "./tsconfig.app.json"
8+
}
9+
],
10+
"compilerOptions": {
11+
"esModuleInterop": true
12+
}
13+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Frontend</title>
6+
<base href="/" />
7+
8+
<meta name="viewport" content="width=device-width, initial-scale=1" />
9+
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
10+
<link rel="stylesheet" href="/src/styles.css" />
11+
</head>
12+
<body>
13+
<div id="root"></div>
14+
<script type="module" src="/src/main.tsx"></script>
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)