Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/timneutkens/micro-apollo
Example usage of Apollo GraphQL with ZEIT's micro
https://github.com/timneutkens/micro-apollo
Last synced: about 1 month ago
JSON representation
Example usage of Apollo GraphQL with ZEIT's micro
- Host: GitHub
- URL: https://github.com/timneutkens/micro-apollo
- Owner: timneutkens
- Created: 2017-06-25T12:47:59.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-06-25T16:45:34.000Z (over 7 years ago)
- Last Synced: 2024-12-10T13:52:37.635Z (about 2 months ago)
- Homepage:
- Size: 1000 Bytes
- Stars: 19
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Micro Apollo
Example usage of Apollo with ZEIT's micro
## Installation
```
npm install micro graphql graphql-server-micro graphql-tools
```## Usage
Create an `index.js` file with the following contents:
```js
const { parse } = require('url')const { microGraphql, microGraphiql } = require('graphql-server-micro')
const { makeExecutableSchema } = require('graphql-tools')const typeDefs = `
type Author {
id: ID! # the ! means that every author object _must_ have an id
firstName: String
lastName: String
posts: [Post] # the list of Posts by this author
}type Post {
id: ID!
title: String
author: Author
votes: Int
}# the schema allows the following query:
type Query {
posts: [Post]
}# this schema allows the following mutation:
type Mutation {
upvotePost (
postId: ID!
): Post
}# we need to tell the server which types represent the root query
# and root mutation types. We call them RootQuery and RootMutation by convention.
schema {
query: Query
mutation: Mutation
}
`;const schema = makeExecutableSchema({
typeDefs,
resolvers: {},
});module.exports = (req, res) => {
const url = parse(req.url)
if(url.pathname === '/graphiql') {
return microGraphiql({endpointURL: '/'})(req, res)
}return microGraphql({ schema })(req, res)
}
```Then run `micro index.js`
## Alternatives
[Micro with express-graphql](https://github.com/timneutkens/micro-graphql)