commit 0b745f24a10ba849623c752e4406aec0b5d10eb3
parent 2da58da1b252e1a6502319cc414d556453b96450
Author: Konstantin Tarkus <hello@tarkus.me>
Date: Fri, 26 Feb 2016 20:50:48 +0300
Add a sample GraphQL endpoint - localhost:3000/graphql
Diffstat:
8 files changed, 90 insertions(+), 10 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
### [Unreleased][unreleased]
+- Add a sample GraphQL endpoint [localhost:3000/graphql](https://localhost:3000/graphql)
- Change the default Node.js server port from `5000` to `3000`
- Add a JWT-based authentication cookies (see `src/server.js`)
- Add a reference implementation of Facebook authentication strategy (`src/core/passport.js`)
diff --git a/package.json b/package.json
@@ -7,16 +7,19 @@
"dependencies": {
"babel-polyfill": "^6.5.0",
"babel-runtime": "^6.5.0",
- "bluebird": "3.3.1",
+ "bluebird": "3.3.3",
"body-parser": "1.15.0",
"classnames": "2.2.3",
"cookie-parser": "1.4.1",
"eventemitter3": "1.1.1",
"express": "4.13.4",
+ "express-graphql": "0.4.9",
"express-jwt": "3.3.0",
"fastclick": "1.0.6",
"fbjs": "0.7.2",
"front-matter": "2.0.6",
+ "graphiql": "0.5.0",
+ "graphql": "0.4.18",
"history": "2.0.0",
"isomorphic-style-loader": "0.0.10",
"jade": "1.11.0",
@@ -25,7 +28,7 @@
"normalize.css": "3.0.3",
"passport": "0.3.2",
"passport-facebook": "2.1.0",
- "pg": "4.4.6",
+ "pg": "4.5.1",
"react": "0.14.7",
"react-dom": "0.14.7",
"react-routing": "0.0.7",
@@ -44,14 +47,14 @@
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"browser-sync": "^2.11.1",
- "core-js": "^2.1.0",
+ "core-js": "^2.1.1",
"css-loader": "^0.23.1",
"csscomb": "^3.1.8",
"del": "^2.2.0",
"eslint": "^2.2.0",
- "eslint-config-airbnb": "^6.0.0",
+ "eslint-config-airbnb": "^6.0.2",
"eslint-loader": "^1.3.0",
- "eslint-plugin-react": "^4.0.0",
+ "eslint-plugin-react": "^4.1.0",
"extend": "^3.0.0",
"file-loader": "^0.8.5",
"gaze": "^0.5.2",
@@ -62,7 +65,7 @@
"json-loader": "^0.5.4",
"mkdirp": "^0.5.1",
"ncp": "^2.0.0",
- "postcss": "^5.0.16",
+ "postcss": "^5.0.17",
"postcss-import": "^8.0.2",
"postcss-loader": "^0.8.1",
"postcss-scss": "^0.1.5",
@@ -73,7 +76,7 @@
"redbox-react": "^1.2.2",
"replace": "^0.3.0",
"url-loader": "^0.5.7",
- "webpack": "^1.12.13",
+ "webpack": "^1.12.14",
"webpack-hot-middleware": "^2.7.1",
"webpack-middleware": "^1.4.0"
},
diff --git a/src/config.js b/src/config.js
@@ -24,7 +24,7 @@ export const analytics = {
export const auth = {
- jwt: { secret: 'React Starter Kit' },
+ jwt: { secret: process.env.JWT_SECRET || 'React Starter Kit' },
// https://developers.facebook.com/
facebook: {
diff --git a/src/data/queries/me.js b/src/data/queries/me.js
@@ -0,0 +1,20 @@
+/**
+ * React Starter Kit (https://www.reactstarterkit.com/)
+ *
+ * Copyright © 2014-2016 Kriasoft, LLC. All rights reserved.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE.txt file in the root directory of this source tree.
+ */
+
+import UserType from '../types/UserType';
+
+export default {
+ type: UserType,
+ resolve({ request }) {
+ return request.user && {
+ id: request.user.id,
+ email: request.user.email,
+ };
+ },
+};
diff --git a/src/data/schema.js b/src/data/schema.js
@@ -0,0 +1,24 @@
+/**
+ * React Starter Kit (https://www.reactstarterkit.com/)
+ *
+ * Copyright © 2014-2016 Kriasoft, LLC. All rights reserved.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE.txt file in the root directory of this source tree.
+ */
+
+import {
+ GraphQLSchema as Schema,
+ GraphQLObjectType as ObjectType,
+} from 'graphql';
+
+const schema = new Schema({
+ query: new ObjectType({
+ name: 'Query',
+ fields: {
+ me: require('./queries/me').default,
+ },
+ }),
+});
+
+export default schema;
diff --git a/src/data/types/UserType.js b/src/data/types/UserType.js
@@ -0,0 +1,25 @@
+/**
+ * React Starter Kit (https://www.reactstarterkit.com/)
+ *
+ * Copyright © 2014-2016 Kriasoft, LLC. All rights reserved.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE.txt file in the root directory of this source tree.
+ */
+
+import {
+ GraphQLObjectType as ObjectType,
+ GraphQLID as ID,
+ GraphQLString as StringType,
+ GraphQLNonNull as NonNull,
+} from 'graphql';
+
+const UserType = new ObjectType({
+ name: 'User',
+ fields: {
+ id: { type: new NonNull(ID) },
+ email: { type: StringType },
+ },
+});
+
+export default UserType;
diff --git a/src/server.js b/src/server.js
@@ -13,10 +13,12 @@ import express from 'express';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import expressJwt from 'express-jwt';
+import expressGraphQL from 'express-graphql';
import jwt from 'jsonwebtoken';
import React from 'react';
import ReactDOM from 'react-dom/server';
import passport from './core/passport';
+import schema from './data/schema';
import Router from './routes';
import Html from './components/Html';
import assets from './assets';
@@ -68,6 +70,12 @@ server.get('/login/facebook/return',
// Register API middleware
// -----------------------------------------------------------------------------
server.use('/api/content', require('./api/content').default);
+server.use('/graphql', expressGraphQL(req => ({
+ schema,
+ graphiql: true,
+ rootValue: { request: req },
+ pretty: process.env.NODE_ENV !== 'production',
+})));
//
// Register server-side rendering middleware
diff --git a/tools/start.js b/tools/start.js
@@ -89,13 +89,12 @@ async function start() {
const bs = Browsersync.create();
bs.init({
...(DEBUG ? {} : { notify: false, ui: false }),
+
proxy: {
target: host,
middleware: [wpMiddleware, ...hotMiddlewares],
},
- notify: DEBUG,
-
// no need to watch '*.js' here, webpack will take care of it for us,
// including full page reloads if HMR won't work
files: ['build/content/**/*.*'],