implements pattern for graphql
This commit is contained in:
43
src/graphql/init.js
Normal file
43
src/graphql/init.js
Normal file
@@ -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;
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 || {}) };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user