├── .gitignore ├── .env.example ├── README.md ├── package.json └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .env -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | ENDPOINT=https://your-db.documents.azure.com:443/ 2 | KEY=your-key 3 | DATABASE=test 4 | CONTAINER=todos -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GraphQL + Cosmos 2 | 3 | 1. Clone this repo 4 | 2. Run `npm install` 5 | 3. In the Azure portal, create a database called `test` and a container called `todos` 6 | 4. Copy `.env.example` to `.env` 7 | 5. Replace the variables in `.env` with your own endpoint and key 8 | 6. Run `node index.js` 9 | 7. Visit `http://localhost:4000/graphiql` 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "graphql-cosmos-demo", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "@azure/cosmos": "3.5.2", 14 | "apollo-server": "2.9.13", 15 | "dotenv": "8.2.0", 16 | "graphql": "14.5.8" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config() 2 | const { ApolloServer, gql } = require('apollo-server') 3 | const { CosmosClient } = require('@azure/cosmos') 4 | 5 | const client = new CosmosClient({ 6 | endpoint: process.env.ENDPOINT, 7 | key: process.env.KEY 8 | }) 9 | 10 | const container = client 11 | .database(process.env.DATABASE) 12 | .container(process.env.CONTAINER) 13 | 14 | const typeDefs = gql` 15 | enum State { 16 | COMPLETE 17 | INCOMPLETE 18 | IN_PROGRESS 19 | } 20 | 21 | type Todo { 22 | id: ID! 23 | title: String 24 | state: State 25 | } 26 | type Query { 27 | todos: [Todo] 28 | todo(id: ID!): Todo 29 | } 30 | type Mutation { 31 | createTodo(title: String!, state: State!): Todo 32 | } 33 | ` 34 | 35 | const resolvers = { 36 | Query: { 37 | todos: async () => { 38 | const response = await container.items.query('SELECT * from c').fetchAll() 39 | return response.resources 40 | }, 41 | todo: async (root, { id }) => { 42 | const response = await container.item(id, undefined).read() 43 | return response.resource 44 | } 45 | }, 46 | Mutation: { 47 | createTodo: async (root, args) => { 48 | const response = await container.items.create(args) 49 | return response.resource 50 | } 51 | } 52 | } 53 | 54 | const server = new ApolloServer({ typeDefs, resolvers }) 55 | server.listen().then(({ url }) => { 56 | console.log(`🚀 Server ready at ${url} 🚀`) 57 | console.log(`Visit ${url}graphiql to load the playground`) 58 | }) 59 | --------------------------------------------------------------------------------