└── readme.md /readme.md: -------------------------------------------------------------------------------- 1 | # Micro Apollo 2 | 3 | Example usage of Apollo with ZEIT's micro 4 | 5 | ## Installation 6 | 7 | ``` 8 | npm install micro graphql graphql-server-micro graphql-tools 9 | ``` 10 | 11 | ## Usage 12 | 13 | Create an `index.js` file with the following contents: 14 | 15 | ```js 16 | const { parse } = require('url') 17 | 18 | const { microGraphql, microGraphiql } = require('graphql-server-micro') 19 | const { makeExecutableSchema } = require('graphql-tools') 20 | 21 | const typeDefs = ` 22 | type Author { 23 | id: ID! # the ! means that every author object _must_ have an id 24 | firstName: String 25 | lastName: String 26 | posts: [Post] # the list of Posts by this author 27 | } 28 | 29 | type Post { 30 | id: ID! 31 | title: String 32 | author: Author 33 | votes: Int 34 | } 35 | 36 | # the schema allows the following query: 37 | type Query { 38 | posts: [Post] 39 | } 40 | 41 | # this schema allows the following mutation: 42 | type Mutation { 43 | upvotePost ( 44 | postId: ID! 45 | ): Post 46 | } 47 | 48 | # we need to tell the server which types represent the root query 49 | # and root mutation types. We call them RootQuery and RootMutation by convention. 50 | schema { 51 | query: Query 52 | mutation: Mutation 53 | } 54 | `; 55 | 56 | const schema = makeExecutableSchema({ 57 | typeDefs, 58 | resolvers: {}, 59 | }); 60 | 61 | module.exports = (req, res) => { 62 | const url = parse(req.url) 63 | if(url.pathname === '/graphiql') { 64 | return microGraphiql({endpointURL: '/'})(req, res) 65 | } 66 | 67 | return microGraphql({ schema })(req, res) 68 | } 69 | ``` 70 | 71 | Then run `micro index.js` 72 | 73 | ## Alternatives 74 | 75 | [Micro with express-graphql](https://github.com/timneutkens/micro-graphql) 76 | --------------------------------------------------------------------------------