├── .gitignore ├── README.md ├── index.js ├── package.json ├── schema.js └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Your First GraphQL Server](https://medium.com/@clayallsopp/your-first-graphql-server-3c766ab4f0a2) 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('babel/register'); 2 | require('./server.js'); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "graphql-intro", 3 | "version": "1.0.0", 4 | "description": "A simple GraphQL server", 5 | "main": "index.js", 6 | "dependencies": { 7 | "babel": "5.8.23", 8 | "body-parser": "1.13.2", 9 | "express": "4.13.1", 10 | "graphql": "0.4.4" 11 | }, 12 | "devDependencies": {}, 13 | "scripts": { 14 | "test": "echo \"Error: no test specified\" && exit 1" 15 | }, 16 | "author": "Clay Allsopp ", 17 | "license": "MIT" 18 | } 19 | -------------------------------------------------------------------------------- /schema.js: -------------------------------------------------------------------------------- 1 | import { 2 | GraphQLObjectType, 3 | GraphQLSchema, 4 | GraphQLInt 5 | } from 'graphql'; 6 | 7 | let count = 0; 8 | 9 | let schema = new GraphQLSchema({ 10 | query: new GraphQLObjectType({ 11 | name: 'RootQueryType', 12 | fields: { 13 | count: { 14 | type: GraphQLInt, 15 | description: 'The count!', 16 | resolve: function() { 17 | return count; 18 | } 19 | } 20 | } 21 | }), 22 | mutation: new GraphQLObjectType({ 23 | name: 'RootMutationType', 24 | fields: { 25 | updateCount: { 26 | type: GraphQLInt, 27 | description: 'Updates the count', 28 | resolve: function() { 29 | count += 1; 30 | return count; 31 | } 32 | } 33 | } 34 | }) 35 | }); 36 | 37 | export default schema; -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import schema from './schema'; 3 | // new dependencies 4 | import { graphql } from 'graphql'; 5 | import bodyParser from 'body-parser'; 6 | 7 | let app = express(); 8 | let PORT = 3000; 9 | 10 | // parse POST body as text 11 | app.use(bodyParser.text({ type: 'application/graphql' })); 12 | 13 | app.post('/graphql', (req, res) => { 14 | // execute GraphQL! 15 | graphql(schema, req.body) 16 | .then((result) => { 17 | res.send(JSON.stringify(result, null, 2)); 18 | }); 19 | }); 20 | 21 | let server = app.listen(PORT, function () { 22 | var host = server.address().address; 23 | var port = server.address().port; 24 | 25 | console.log('GraphQL listening at http://%s:%s', host, port); 26 | }); --------------------------------------------------------------------------------