|
| 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