Skip to content

Commit 90c8017

Browse files
committed
chore: Adding a new demo for GraphQL Federation use-case
Signed-off-by: Laurent Broudoux <[email protected]>
1 parent 27d1b44 commit 90c8017

20 files changed

+13118
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
sources:
2+
- name: Pastries
3+
handler:
4+
openapi:
5+
source: ./contracts/apipastries-openapi.yaml
6+
#endpoint: http://localhost:8080/rest/API+Pastries/0.0.1
7+
endpoint: http://localhost:8585/rest/API+Pastries/0.0.1
8+
- name: Chiefs
9+
handler:
10+
grpc:
11+
source: ./contracts/chiefs-service.proto
12+
#endpoint: localhost:9090
13+
endpoint: localhost:8686
14+
- name: Stores
15+
handler:
16+
graphql:
17+
source: ./contracts/stores-graph.graphql
18+
#endpoint: http://localhost:8080/graphql/Store+Graph/1.0
19+
endpoint: http://localhost:8585/graphql/Store+Graph/1.0
20+
21+
22+
transforms:
23+
- filterSchema:
24+
filters:
25+
- Query.stores
26+
27+
additionalTypeDefs: |
28+
extend type Store {
29+
pastrySells: [Sells!]! @resolveTo(
30+
sourceName: "Stores",
31+
sourceTypeName: "Query",
32+
sourceFieldName: "pastrySells",
33+
requiredSelectionSet: "{ id }",
34+
sourceArgs: {
35+
storeId: "{root.id}"
36+
}
37+
)
38+
}
39+
extend type Sells {
40+
pastry: Pastry @resolveTo(
41+
sourceName: "Pastries",
42+
sourceTypeName: "Query",
43+
sourceFieldName: "GetPastryByName",
44+
requiredSelectionSet: "{ pastryId }",
45+
sourceArgs: {
46+
name: "{root.pastryId}"
47+
}
48+
)
49+
}
50+
extend type Pastry {
51+
chief: io__microcks__chiefs__v1__Chief @resolveTo(
52+
sourceName: "Chiefs"
53+
sourceTypeName: "Query",
54+
sourceFieldName: "io_microcks_chiefs_v1_ChiefsService_GetChief",
55+
requiredSelectionSet: "{ chiefId }",
56+
sourceArgs: {
57+
input: { id: "{root.chiefId}" }
58+
}
59+
)
60+
}

graphql-federation-demo/README.md

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
## Microcks in GraphQL Federation use-case
2+
3+
This is the demo that was played during the GraphQL SF Meetup #4 on Sept, 5th 2024. You can find the companion [slides here](https://docs.google.com/presentation/d/1_nAPuj7SrCiQF9isEvrtpVQS2VEzjEfwjVQVAXLDk_I/edit?usp=share_link).
4+
5+
### 1st step - Discover Microcks
6+
7+
Start Microcks standalone by going to `/standalone` folder and using the Docker Compose file:
8+
9+
```sh
10+
cd standalone
11+
docker compose up -d
12+
```
13+
14+
After few seconds, Microcks should now run on `http://localhost:8080`. If this port is already used on your machine, edit the `docker-compose.yml` file to change the port number.
15+
16+
#### Import the Pastry OpenAPI resources
17+
18+
Import `contracts/apipastries-openapi.yaml` in Microcks as a primary artifact (default import). Then, import the other files below as secondary artifacts by ticking the **Just merge examples with artifact** checkbox:
19+
* `datasets/apipastries-postman-collection.json`
20+
* `datasets/apipastries-examples.yaml`
21+
22+
Check the **API | Services** menu to play around with this new simulation.
23+
24+
#### Import the Chief Grpc Service resources
25+
26+
Import `contracts/chiefs-service.proto` in Microcks as a primary artifact (default import). Then, import the other files below as secondary artifacts by ticking the **Just merge examples with artifact** checkbox:
27+
* `datasets/chiefs-examples.yaml`
28+
29+
Check the **API | Services** menu to play around with this new simulation. You should also be able to issue those `grpcurl` commands:
30+
31+
```sh
32+
grpcurl -plaintext -d '{}' localhost:9090 io.microcks.chiefs.v1.ChiefsService/ListChiefs
33+
34+
grpcurl -plaintext -d '{"id": 1}' localhost:9090 io.microcks.chiefs.v1.ChiefsService/GetChief
35+
36+
grpcurl -plaintext -d '{"id": 2}' localhost:9090 io.microcks.chiefs.v1.ChiefsService/GetChief
37+
38+
grpcurl -plaintext -d '{"id": 3}' localhost:9090 io.microcks.chiefs.v1.ChiefsService/GetChief
39+
```
40+
41+
#### Import the Stores GraphQL API resources
42+
43+
Import `contracts/stores-graph.Graphql` in Microcks as a primary artifact (default import). Then, import the other files below as secondary artifacts by ticking the **Just merge examples with artifact** checkbox:
44+
* `datasets/stores-examples.yaml`
45+
* `datasets/stores-recording.json`
46+
47+
Check the **API | Services** menu to play around with this new simulation. You should also be able to issue those `curl` commands:
48+
49+
```sh
50+
echo '{ "query":
51+
"query {
52+
stores
53+
}"
54+
}' | tr -d '\n' | curl \
55+
-X POST \
56+
-H "Content-Type: application/json" \
57+
-s -d @- \
58+
http://localhost:8080/graphql/Store+Graph/1.0
59+
60+
61+
echo '{ "query":
62+
"query pastrySells($id: ID!) {
63+
pastrySells(storeId: $id) {
64+
pastryId
65+
sellsCount
66+
monthYear
67+
storeId
68+
}
69+
}",
70+
"variables": {
71+
"id": "0"
72+
}
73+
}' | tr -d '\n' | curl \
74+
-X POST \
75+
-H "Content-Type: application/json" \
76+
-s -d @- \
77+
http://localhost:8080/graphql/Store+Graph/1.0
78+
79+
80+
echo '{ "query":
81+
"query pastrySells($id: ID!) {
82+
pastrySells(storeId: $id) {
83+
pastryId
84+
sellsCount
85+
monthYear
86+
storeId
87+
}
88+
}",
89+
"variables": {
90+
"id": "1"
91+
}
92+
}' | tr -d '\n' | curl \
93+
-X POST \
94+
-H "Content-Type: application/json" \
95+
-s -d @- \
96+
http://localhost:8080/graphql/Store+Graph/1.0
97+
```
98+
99+
### 2nd step - Automate everything
100+
101+
Start cleaning up the Microcks standalone server you've previously launched. No worries: we'll re-create everything automatically.
102+
103+
```sh
104+
cd standalone
105+
docker compose down
106+
```
107+
108+
Then, create a new instance of Microcks that will be automatically populated will the resources you'll previously loaded manually. From root directory of this project:
109+
110+
```sh
111+
$ ./microcks.sh
112+
113+
==== OUTPUT ===
114+
[+] Running 2/0
115+
✔ Container graphql-meetup-microcks-1 Created 0.0s
116+
✔ Container graphql-meetup-importer-1 Created 0.0s
117+
Attaching to graphql-meetup-importer-1, graphql-meetup-microcks-1
118+
graphql-meetup-microcks-1 | 21:22:10,314 |-INFO in ch.qos.logback.classic.LoggerContext[default] - This is logback-classic version 1.4.14
119+
[...]
120+
graphql-meetup-microcks-1 | 21:22:11.083 INFO 1 --- [080-exec-8] i.g.microcks.service.ServiceService : Having imported 1 services definitions into repository
121+
graphql-meetup-importer-1 | Microcks has discovered 'Store Graph:1.0'
122+
graphql-meetup-importer-1 exited with code 0
123+
```
124+
125+
A new instance of Microcks is now available on `http://localhost:8585`. If this port is already used on your machine, edit the `microcks.yaml` file to change the port number.
126+
127+
Explore this new instance and check the loaded content! Leave your Microcks instance up and runninf in this terminal.
128+
129+
### 3rd step - Play with federation!
130+
131+
In another terminal, we're going to launch a GraphQL Mesh Gateway for doing federation of the 3 simulations powered by Microcks.
132+
133+
You may want to review this mesh configuration if you changed any default port, just edit the `.meshrc.yaml` file and adapt the `endpoints` address.
134+
135+
Open a new terminal window and from the root of this demo project, launch the `install` and then `start` commands:
136+
137+
```sh
138+
npm install
139+
```
140+
141+
and then:
142+
143+
```sh
144+
$ npm run start
145+
146+
==== OUTPUT ====
147+
> start
148+
> mesh dev
149+
150+
[2024-09-10T16:20:38.433Z] INFO 🕸️ Mesh - Server Starting GraphQL Mesh...
151+
[2024-09-10T16:20:38.433Z] INFO 🕸️ Mesh - Server Serving GraphQL Mesh: http://0.0.0.0:4000
152+
[2024-09-10T16:20:38.443Z] INFO 🕸️ Mesh - Pastries Generating GraphQL schema from OpenAPI schema
153+
[2024-09-10T16:20:38.506Z] INFO 🕸️ Mesh - Pastries Processing annotations for the execution layer
154+
[2024-09-10T16:20:38.553Z] INFO 🕸️ Mesh Generating index file in TypeScript
155+
[2024-09-10T16:20:38.603Z] INFO 🕸️ Mesh Writing index.ts for ESM to the disk.
156+
[2024-09-10T16:20:38.604Z] INFO 🕸️ Mesh Writing index.ts for CJS to the disk.
157+
158+
```
159+
160+
A new browser tab should have been opened autoamtically, pointing on `http://localhost:4000/graphql`. On this GraphiQL interface, you may issue some requests and see the effects of federation but using powerful simulations instead of real endpoints.
161+
162+
You can issue this GraphQL query for example:
163+
164+
```graphql
165+
query MyQuery {
166+
stores {
167+
id
168+
location
169+
name
170+
pastrySells {
171+
sellsCount
172+
monthYear
173+
pastryId
174+
pastry {
175+
name
176+
price
177+
size
178+
description
179+
chief {
180+
name
181+
region
182+
}
183+
}
184+
}
185+
}
186+
}
187+
```
188+
189+
When you want to finish, just hit `Ctrl+C` in opened terminals and remove existing container resources using `docker compose -f microcks.yaml down`.
190+
191+
That's it!

0 commit comments

Comments
 (0)