|
| 1 | +--- |
| 2 | +sidebar_label: Docker |
| 3 | +sidebar_position: 2 |
| 4 | +description: Hasura with Docker for MongoDB |
| 5 | +keywords: |
| 6 | + - hasura |
| 7 | + - docs |
| 8 | + - databases |
| 9 | + - mongodb |
| 10 | + - docker |
| 11 | +--- |
| 12 | + |
| 13 | +import Thumbnail from '@site/src/components/Thumbnail'; |
| 14 | + |
| 15 | +# Get Started with Docker (Hasura & MongoDB) |
| 16 | + |
| 17 | +## Introduction |
| 18 | + |
| 19 | +This guide will help you get set up with the [Enterprise Edition](/enterprise/overview.mdx) of Hasura GraphQL Engine |
| 20 | +with our MongoDB integration using Docker Compose. This is the easiest way to set up Hasura Enterprise Edition and the |
| 21 | +MongoDB GraphQL Data Connector. |
| 22 | + |
| 23 | +:::info Supported versions: |
| 24 | + |
| 25 | +Hasura GraphQL Engine `v2.27.0` onwards |
| 26 | + |
| 27 | +::: |
| 28 | + |
| 29 | +:::tip Supported features |
| 30 | + |
| 31 | +Currently, Hasura supports read-only queries on MongoDB.<br/> Other limitations which are actively on the roadmap and |
| 32 | +will be supported soon include: |
| 33 | + |
| 34 | +- Embedded document queries and permissions. |
| 35 | +- Cross-Collection relationships (executed within the database). |
| 36 | +- Logical Models to support schema customization (removing the MongoDB validation schema as a dependency) and bespoke |
| 37 | + query execution. |
| 38 | + |
| 39 | +::: |
| 40 | + |
| 41 | +## Deploying Hasura Enterprise with Docker |
| 42 | + |
| 43 | +### Prerequisites |
| 44 | + |
| 45 | +This tutorial assumes that the following prerequisites have been met: |
| 46 | + |
| 47 | +- You have [Docker](https://docs.docker.com/install/) and [Docker Compose](https://docs.docker.com/compose/install/) |
| 48 | + working on your machine. |
| 49 | +- You have [MongoDB Compass](https://www.mongodb.com/products/compass) installed on your machine. |
| 50 | + |
| 51 | +### Step 1: Get the Docker Compose file |
| 52 | + |
| 53 | +The [install manifests](https://github.com/hasura/graphql-engine/tree/master/install-manifests) repo contains all |
| 54 | +installation manifests required to deploy Hasura anywhere. The Docker Compose manifest also contains a Postgres database |
| 55 | +in order to store the Hasura metadata and a Redis instance for caching. |
| 56 | + |
| 57 | +```bash |
| 58 | +# in a new directory run |
| 59 | +wget https://raw.githubusercontent.com/hasura/graphql-engine/master/install-manifests/enterprise/mongodb/docker-compose.yaml |
| 60 | +# or run |
| 61 | +curl https://raw.githubusercontent.com/hasura/graphql-engine/master/install-manifests/enterprise/mongodb/docker-compose.yaml -o docker-compose.yml |
| 62 | +``` |
| 63 | + |
| 64 | +:::info Four containers are created |
| 65 | + |
| 66 | +When you use these to launch the services, you'll see four containers running. The first two are Hasura GraphQL Engine |
| 67 | +and a Postgres Database for storing Hasura's metadata. The third container is the MongoDB GraphQL Connector agent. The |
| 68 | +forth container is a copy of [MongoDB Community Edition](https://hub.docker.com/_/mongo). |
| 69 | + |
| 70 | +::: |
| 71 | + |
| 72 | +### Step 2: Set the Hasura Enterprise Edition license key and the admin secret |
| 73 | + |
| 74 | +Edit the downloaded `docker-compose.yaml` and set the license key and admin secret. If you've been provided a license |
| 75 | +key by the Hasura team, you can enter it according to the directions below. |
| 76 | + |
| 77 | +```yaml {5-6} |
| 78 | +--- |
| 79 | +graphql-engine: |
| 80 | + image: hasura/graphql-engine:v2.27.0 |
| 81 | + environment: |
| 82 | + HASURA_GRAPHQL_EE_LICENSE_KEY: <license key> |
| 83 | + HASURA_GRAPHQL_ADMIN_SECRET: <your secretkey> |
| 84 | +``` |
| 85 | +
|
| 86 | +An [admin secret key](/deployment/securing-graphql-endpoint.mdx) is required to make sure that your GraphQL endpoint and |
| 87 | +the Hasura Console are not publicly accessible. |
| 88 | +
|
| 89 | +:::info I don't have a license key |
| 90 | +
|
| 91 | +If you don't already have a license key and are interested in trying out Hasura Enterprise Edition with MongoDB, you can |
| 92 | +start a free 30-day trial. Leave the `HASURA_GRAPHQL_EE_LICENSE_KEY` environment variable commented out we'll return to |
| 93 | +this in Step 6. |
| 94 | + |
| 95 | +::: |
| 96 | + |
| 97 | +:::caution Secure the admin secret |
| 98 | + |
| 99 | +The `HASURA_GRAPHQL_ADMIN_SECRET` should never be passed from the client to the Hasura GraphQL Engine as it would give |
| 100 | +the client full admin rights to your Hasura instance. See [Authentication & Authorization](/auth/overview.mdx) for |
| 101 | +information on setting up auth in Hasura. |
| 102 | + |
| 103 | +::: |
| 104 | + |
| 105 | +### Step 3: Run the containers |
| 106 | + |
| 107 | +The following command will create and run the containers in the Docker Compose manifest: |
| 108 | + |
| 109 | +```bash |
| 110 | +docker compose up -d |
| 111 | +``` |
| 112 | + |
| 113 | +### Step 4: Create a MongoDB database |
| 114 | + |
| 115 | +:::info I already have a MongoDB database |
| 116 | + |
| 117 | +This guide assumes you don't have a MongoDB instance already set up. If you do, you can skip to |
| 118 | +[Step 6](#step-6-load-the-hasura-console). |
| 119 | + |
| 120 | +::: |
| 121 | + |
| 122 | +Open MongoDB Compass and create a new connection using this connection string: |
| 123 | + |
| 124 | +``` |
| 125 | +mongodb://mongouser:mongopassword@localhost:27017/?authMechanism=DEFAULT |
| 126 | +``` |
| 127 | + |
| 128 | +Create a new database called demo using the MongoShell at the bottom of the MongoDB compass screen by entering the |
| 129 | +command: |
| 130 | + |
| 131 | +``` |
| 132 | +use demo; |
| 133 | +``` |
| 134 | + |
| 135 | +Via MongoShell, create a new `users` collection: |
| 136 | + |
| 137 | +```javascript |
| 138 | +db.createCollection('users', { |
| 139 | + validator: { |
| 140 | + $jsonSchema: { |
| 141 | + bsonType: 'object', |
| 142 | + required: ['name', 'age'], |
| 143 | + properties: { |
| 144 | + name: { |
| 145 | + bsonType: 'string', |
| 146 | + }, |
| 147 | + age: { |
| 148 | + bsonType: 'int', |
| 149 | + minimum: 18, |
| 150 | + }, |
| 151 | + email: { |
| 152 | + bsonType: 'string', |
| 153 | + pattern: '^.+@.+$', |
| 154 | + }, |
| 155 | + user_meta: { |
| 156 | + bsonType: 'object', |
| 157 | + properties: { |
| 158 | + user_role: { |
| 159 | + bsonType: 'string', |
| 160 | + }, |
| 161 | + email_verified: { |
| 162 | + bsonType: 'bool', |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | +}); |
| 170 | +``` |
| 171 | + |
| 172 | +:::info Why do I need a validation schema in my Collection? |
| 173 | + |
| 174 | +Currently, Hasura uses the validation schema to build your GraphQL schema. |
| 175 | + |
| 176 | +Future versions of the Hasura MongoDB connector will allow on-the-fly editing of the GraphQL schema, will continue to |
| 177 | +work with the MongoDB validation schema, but will not have it as a dependency. |
| 178 | + |
| 179 | +Read more about the |
| 180 | +[MongoDB validation schema](https://www.mongodb.com/docs/manual/core/schema-validation/specify-json-schema/#std-label-schema-validation-json) |
| 181 | +and how to set it up for your database. |
| 182 | + |
| 183 | +::: |
| 184 | + |
| 185 | +If you don't see your changes, you can refresh your databases on the left-hand sidebar. Once applied, you can view the |
| 186 | +`users` Collection in MongoDB Compass: |
| 187 | + |
| 188 | +<Thumbnail |
| 189 | + src="/img/databases/mongodb/mongo-collection.png" |
| 190 | + alt="Creating your first MongoDB Collection." |
| 191 | + width="1000px" |
| 192 | +/> |
| 193 | + |
| 194 | +### Step 5: Insert your first sample Documents |
| 195 | + |
| 196 | +Insert a few sample documents to query on afterwards. |
| 197 | + |
| 198 | +```javascript |
| 199 | +db.users.insertMany([ |
| 200 | + { |
| 201 | + name: 'John', |
| 202 | + age: 44, |
| 203 | + email: 'john@example.com', |
| 204 | + user_meta: { |
| 205 | + user_role: 'user', |
| 206 | + email_verified: true, |
| 207 | + }, |
| 208 | + }, |
| 209 | + { |
| 210 | + name: 'Jane', |
| 211 | + age: 24, |
| 212 | + email: 'jane@example.com', |
| 213 | + user_meta: { |
| 214 | + user_role: 'user', |
| 215 | + email_verified: true, |
| 216 | + }, |
| 217 | + }, |
| 218 | + { |
| 219 | + name: 'Emma', |
| 220 | + age: 36, |
| 221 | + email: 'emma@example.com', |
| 222 | + user_meta: { |
| 223 | + user_role: 'manager', |
| 224 | + email_verified: true, |
| 225 | + }, |
| 226 | + }, |
| 227 | + { |
| 228 | + name: 'Liam', |
| 229 | + age: 64, |
| 230 | + email: 'liam@example.com', |
| 231 | + user_meta: { |
| 232 | + user_role: 'manager', |
| 233 | + email_verified: true, |
| 234 | + }, |
| 235 | + }, |
| 236 | +]); |
| 237 | +``` |
| 238 | + |
| 239 | +You should see an output similar to this: |
| 240 | + |
| 241 | +<Thumbnail |
| 242 | + src="/img/databases/mongodb/mongo-documents.png" |
| 243 | + alt="Inserting your sample Documents in MongoDB." |
| 244 | + width="1000px" |
| 245 | +/> |
| 246 | + |
| 247 | +### Step 6: Load the Hasura Console |
| 248 | + |
| 249 | +Open the Hasura Console by navigating to `http://localhost:8080/console`. You will need to input your admin secret key |
| 250 | +as set in your Docker Compose file to log in. |
| 251 | + |
| 252 | +:::info 30-day free trial |
| 253 | + |
| 254 | +If you don't already have a license key, you can start a 30-day free trial by clicking the `ENTERPRISE` button in the |
| 255 | +top navigation. You can read more details [here](/enterprise/try-hasura-enterprise-edition.mdx). |
| 256 | + |
| 257 | +<Thumbnail |
| 258 | + src="/img/enterprise/Trials_Register_Button.png" |
| 259 | + alt="Enterprise Edition Trial register button" |
| 260 | + width="1146px" |
| 261 | +/> |
| 262 | + |
| 263 | +::: |
| 264 | + |
| 265 | +### Step 7: Connect to MongoDB |
| 266 | + |
| 267 | +Visit `Data` > `Manage` to connect your MongoDB database. **If you've connected using the Docker guide above, your |
| 268 | +MongoDB data connector should be pre-connected to your Hasura instance.** Select the `Connect Database` button and |
| 269 | +follow head to [Step 8](#step-8-tracking-collections). |
| 270 | + |
| 271 | +You can check this by selecting the `Data Connector Agents` dropdown from the Data Manager: |
| 272 | + |
| 273 | +If it is not connected and you're using the `docker-compose.yml` file above, you can use the following details to |
| 274 | +connect to your agent as in the screenshot below: |
| 275 | + |
| 276 | +- Agent Name: `mongodb` |
| 277 | +- URL: `http://mongo-data-connector:3000` |
| 278 | + |
| 279 | +Then click, `Connect`: |
| 280 | + |
| 281 | +<Thumbnail src="/img/databases/mongodb/data-connector.png" alt="Connecting to MongoDB - Connector" width="1000px" /> |
| 282 | + |
| 283 | +Connect your database using the following details: |
| 284 | + |
| 285 | +- Database Name: `mongodb` |
| 286 | +- Connection: `mongodb://mongouser:mongopassword@mongodb:27017` |
| 287 | +- db: `demo` |
| 288 | + |
| 289 | +<Thumbnail src="/img/databases/mongodb/connecting.gif" alt="Connecting to MongoDB - Connections" width="1000px" /> |
| 290 | + |
| 291 | +If you're using a MongoDB instance hosted on MongoDB Atlas or elsewhere, simply add the connection details for your |
| 292 | +instance and click `Connect Database`. |
| 293 | + |
| 294 | +### Step 8: Tracking Collections |
| 295 | + |
| 296 | +Once your database has been connected, select the database name from the left-hand sidebar. |
| 297 | + |
| 298 | +You should see your `users` Collection listed here. Select it, and select `Track Selected`. |
| 299 | + |
| 300 | +<Thumbnail src="/img/databases/mongodb/track-table.png" alt="Connecting to MongoDB - Track Tables." width="1000px" /> |
| 301 | + |
| 302 | +Your `users` Collection is now added to your GraphQL API! 🎉 |
| 303 | + |
| 304 | +:::info Make your Collection available to other roles |
| 305 | + |
| 306 | +By default, this Collection is only available to `admin` users. To make it available for more user groups, select the |
| 307 | +Collection name from the left-hand sidebar, and select `Permissions` to setup permission rules for the Collection. You |
| 308 | +can read more about permissions [here](/auth/authorization/index.mdx). |
| 309 | + |
| 310 | +::: |
| 311 | + |
| 312 | +### Step 9: Running your first query |
| 313 | + |
| 314 | +Select API from your header, this will take you to GraphiQL, our API testing utility. |
| 315 | + |
| 316 | +Entering the following query and running will return all your users: |
| 317 | + |
| 318 | +```graphql |
| 319 | +query allUsers { |
| 320 | + users { |
| 321 | + _id |
| 322 | + age |
| 323 | + email |
| 324 | + name |
| 325 | + user_meta { |
| 326 | + email_verified |
| 327 | + user_role |
| 328 | + } |
| 329 | + } |
| 330 | +} |
| 331 | +``` |
| 332 | + |
| 333 | +<Thumbnail |
| 334 | + src="/img/databases/mongodb/gql-query.png" |
| 335 | + alt="Connecting to MongoDB - Making a GraphQL query." |
| 336 | + width="1000px" |
| 337 | +/> |
| 338 | + |
| 339 | +Entering the following will only return users with the names 'John' or 'Jane': |
| 340 | + |
| 341 | +```graphql |
| 342 | +query userFiltered { |
| 343 | + users(where: { name: { _in: ["John", "Jane"] } }) { |
| 344 | + _id |
| 345 | + age |
| 346 | + email |
| 347 | + name |
| 348 | + user_meta { |
| 349 | + email_verified |
| 350 | + user_role |
| 351 | + } |
| 352 | + } |
| 353 | +} |
| 354 | +``` |
| 355 | + |
| 356 | +## Keep up to date |
| 357 | + |
| 358 | +Please watch this space to get the latest docs on how you can try these features out via the Console or by manipulating |
| 359 | +Metadata in JSON/YAML directly. |
| 360 | + |
| 361 | +If you'd like to stay informed about the status of MongoDB support, subscribe to our newsletter and join our discord! |
| 362 | + |
| 363 | +- [https://hasura.io/newsletter/](https://hasura.io/newsletter/) |
| 364 | +- [https://discord.com/invite/hasura](https://discord.com/invite/hasura) |
0 commit comments