diff --git a/package.json b/package.json index 157295de8a..69c69f3cb0 100644 --- a/package.json +++ b/package.json @@ -44,12 +44,12 @@ "express": "^4.17.1", "express-busboy": "^7.0.1", "express-fileupload": "^1.1.6", - "express-graphql": "^0.7.1", + "express-graphql": "^0.9.0", "express-validation": "^1.0.3", "file-loader": "^1.1.11", "flatley": "^5.2.0", - "graphql": "^14.5.8", - "graphql-tools": "^4.0.6", + "graphql": "^15.0.0", + "graphql-tools": "^4.0.7", "html-webpack-plugin": "^3.2.0", "http-status": "^1.4.2", "ignore-styles": "^5.0.1", diff --git a/src/graphql/init.js b/src/graphql/init.js new file mode 100644 index 0000000000..42c7bebec1 --- /dev/null +++ b/src/graphql/init.js @@ -0,0 +1,43 @@ +const graphQLHTTP = require('express-graphql'); +const { GraphQLObjectType, GraphQLString, GraphQLSchema } = require('graphql'); + +const queryType = { + name: 'Query', + fields: {}, +}; + +function init() { + const userType = new GraphQLObjectType({ + name: 'User', + fields: { + id: { type: GraphQLString }, + email: { type: GraphQLString }, + }, + }); + + queryType.fields.user = { + type: userType, + args: { + id: { type: GraphQLString }, + }, + resolve: (_, { id }) => { + return { + id, + email: 'test', + }; + }, + }; + + const query = new GraphQLObjectType(queryType); + const schema = new GraphQLSchema({ query }); + + return graphQLHTTP({ + schema, + graphiql: true, + context: ({ req }) => ({ + user: req.user, + }), + }); +} + +module.exports = init; diff --git a/src/index.js b/src/index.js index 83c54b80a1..31d733f4aa 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ const express = require('express'); +const graphQLHTTP = require('express-graphql'); const connectMongoose = require('./mongoose/connect'); const expressMiddleware = require('./express/middleware'); @@ -7,6 +8,7 @@ const registerUser = require('./auth/register'); const registerUpload = require('./uploads/register'); const registerCollections = require('./collections/register'); const registerGlobals = require('./globals/register'); +const initGraphQL = require('./graphql/init'); const sanitizeConfig = require('./utilities/sanitizeConfig'); class Payload { @@ -20,6 +22,7 @@ class Payload { this.registerUpload = registerUpload.bind(this); this.registerCollections = registerCollections.bind(this); this.registerGlobals = registerGlobals.bind(this); + this.initGraphQL = initGraphQL.bind(this); // Setup & initialization connectMongoose(this.config.mongoURL); @@ -46,6 +49,9 @@ class Payload { // Bind static this.express.use(this.config.staticURL, express.static(this.config.staticDir)); + + // Init GraphQL + this.express.use(this.config.routes.graphQL, this.initGraphQL()); } } diff --git a/src/utilities/sanitizeConfig.js b/src/utilities/sanitizeConfig.js index 581605b3c0..da7a439f09 100644 --- a/src/utilities/sanitizeConfig.js +++ b/src/utilities/sanitizeConfig.js @@ -4,6 +4,7 @@ const sanitizeConfig = (config) => { sanitizedConfig.routes = { admin: (config.routes && config.routes.admin) ? config.routes.admin : '/admin', api: (config.routes && config.routes.api) ? config.routes.api : '/api', + graphQL: (config.routes && config.routes.graphQL) ? config.routes.graphQL : '/graphql', }; sanitizedConfig.components = { ...(config.components || {}) }; diff --git a/yarn.lock b/yarn.lock index 7434444f72..c69b999aa6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1499,7 +1499,7 @@ accept-language-parser@^1.5.0: resolved "https://registry.yarnpkg.com/accept-language-parser/-/accept-language-parser-1.5.0.tgz#8877c54040a8dcb59e0a07d9c1fde42298334791" integrity sha1-iHfFQECo3LWeCgfZwf3kIpgzR5E= -accepts@^1.3.5, accepts@~1.3.5, accepts@~1.3.7: +accepts@^1.3.7, accepts@~1.3.5, accepts@~1.3.7: version "1.3.7" resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.7.tgz#531bc726517a3b2b41f850021c6cc15eaab507cd" integrity sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA== @@ -4377,15 +4377,15 @@ express-fileupload@^1.1.6: dependencies: busboy "^0.3.1" -express-graphql@^0.7.1: - version "0.7.1" - resolved "https://registry.yarnpkg.com/express-graphql/-/express-graphql-0.7.1.tgz#6c7712ee966c3aba1930e064ea4c8181e56fd3ef" - integrity sha512-YpheAqTbSKpb5h57rV2yu2dPNUBi4FvZDspZ5iEV3ov34PBRgnM4lEBkv60+vZRJ6SweYL14N8AGYdov7g6ooQ== +express-graphql@^0.9.0: + version "0.9.0" + resolved "https://registry.yarnpkg.com/express-graphql/-/express-graphql-0.9.0.tgz#00fd8552f866bac5c9a4612b2c4c82076107b3c2" + integrity sha512-wccd9Lb6oeJ8yHpUs/8LcnGjFUUQYmOG9A5BNLybRdCzGw0PeUrtBxsIR8bfiur6uSW4OvPkVDoYH06z6/N9+w== dependencies: - accepts "^1.3.5" + accepts "^1.3.7" content-type "^1.0.4" - http-errors "^1.7.1" - raw-body "^2.3.3" + http-errors "^1.7.3" + raw-body "^2.4.1" express-validation@^1.0.3: version "1.0.3" @@ -5103,7 +5103,7 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6 resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423" integrity sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ== -graphql-tools@^4.0.6: +graphql-tools@^4.0.7: version "4.0.7" resolved "https://registry.yarnpkg.com/graphql-tools/-/graphql-tools-4.0.7.tgz#743309b96cb657ff45b607ee0a07193cd987e43c" integrity sha512-rApl8sT8t/W1uQRcwzxMYyUBiCl/XicluApiDkNze5TX/GR0BSTQMjM2UcRGdTmkbsb1Eqq6afkyyeG/zMxZYQ== @@ -5114,12 +5114,10 @@ graphql-tools@^4.0.6: iterall "^1.1.3" uuid "^3.1.0" -graphql@^14.5.8: - version "14.6.0" - resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.6.0.tgz#57822297111e874ea12f5cd4419616930cd83e49" - integrity sha512-VKzfvHEKybTKjQVpTFrA5yUq2S9ihcZvfJAtsDBBCuV6wauPu1xl/f9ehgVf0FcEJJs4vz6ysb/ZMkGigQZseg== - dependencies: - iterall "^1.2.2" +graphql@^15.0.0: + version "15.0.0" + resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.0.0.tgz#042a5eb5e2506a2e2111ce41eb446a8e570b8be9" + integrity sha512-ZyVO1xIF9F+4cxfkdhOJINM+51B06Friuv4M66W7HzUOeFd+vNzUn4vtswYINPi6sysjf1M2Ri/rwZALqgwbaQ== growly@^1.2.0, growly@^1.3.0: version "1.3.0" @@ -5398,7 +5396,7 @@ http-errors@1.7.2: statuses ">= 1.5.0 < 2" toidentifier "1.0.0" -http-errors@1.7.3, http-errors@^1.7.1, http-errors@~1.7.2: +http-errors@1.7.3, http-errors@^1.7.3, http-errors@~1.7.2: version "1.7.3" resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.7.3.tgz#6c619e4f9c60308c38519498c14fbb10aacebb06" integrity sha512-ZTTX0MWrsQ2ZAhA1cejAwDLycFsd7I7nVtnkT3Ol0aqodaKW+0CTZDQ1uBv5whptCnc8e8HeRRJxRs0kmm/Qfw== @@ -6135,7 +6133,7 @@ istanbul-reports@^2.2.6: dependencies: html-escaper "^2.0.0" -iterall@^1.1.3, iterall@^1.2.2: +iterall@^1.1.3: version "1.3.0" resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.3.0.tgz#afcb08492e2915cbd8a0884eb93a8c94d0d72fea" integrity sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg== @@ -9550,7 +9548,7 @@ raw-body@2.4.0: iconv-lite "0.4.24" unpipe "1.0.0" -raw-body@^2.3.3: +raw-body@^2.4.1: version "2.4.1" resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.4.1.tgz#30ac82f98bb5ae8c152e67149dac8d55153b168c" integrity sha512-9WmIKF6mkvA0SLmA2Knm9+qj89e+j1zqgyn8aXGd7+nAduPoqgI9lO57SAZNn/Byzo5P7JhXTyg9PzaJbH73bA==