2017-02-06 14:33:34 +08:00
|
|
|
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express';
|
|
|
|
import bodyParser from 'body-parser';
|
|
|
|
import express from 'express';
|
2017-02-12 22:00:13 +08:00
|
|
|
import { makeExecutableSchema } from 'graphql-tools';
|
2017-02-06 14:33:34 +08:00
|
|
|
import deepmerge from 'deepmerge';
|
2017-02-12 22:00:13 +08:00
|
|
|
import OpticsAgent from 'optics-agent'
|
2017-02-06 14:33:34 +08:00
|
|
|
|
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { check } from 'meteor/check';
|
|
|
|
import { Accounts } from 'meteor/accounts-base';
|
|
|
|
|
2017-02-12 22:00:13 +08:00
|
|
|
import { GraphQLSchema, Utils } from '../modules/index.js';
|
2017-03-11 11:17:50 +08:00
|
|
|
import { webAppConnectHandlersUse } from './meteor_patch.js';
|
2017-02-06 14:33:34 +08:00
|
|
|
|
2017-02-12 22:00:13 +08:00
|
|
|
// defaults
|
2017-02-06 14:33:34 +08:00
|
|
|
const defaultConfig = {
|
|
|
|
path: '/graphql',
|
|
|
|
maxAccountsCacheSizeInMB: 1,
|
2017-02-12 22:00:13 +08:00
|
|
|
graphiql: Meteor.isDevelopment,
|
|
|
|
graphiqlPath: '/graphiql',
|
|
|
|
graphiqlOptions: {
|
|
|
|
passHeader: "'Authorization': localStorage['Meteor.loginToken']", // eslint-disable-line quotes
|
2017-02-06 14:33:34 +08:00
|
|
|
},
|
|
|
|
configServer: (graphQLServer) => {},
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultOptions = {
|
|
|
|
formatError: e => ({
|
|
|
|
message: e.message,
|
|
|
|
locations: e.locations,
|
2017-02-12 22:00:13 +08:00
|
|
|
path: e.path,
|
2017-02-06 14:33:34 +08:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2017-02-12 22:00:13 +08:00
|
|
|
if (Meteor.isDevelopment) {
|
|
|
|
defaultOptions.debug = true;
|
|
|
|
}
|
2017-02-06 14:33:34 +08:00
|
|
|
|
2017-02-12 22:00:13 +08:00
|
|
|
// createApolloServer
|
|
|
|
const createApolloServer = (givenOptions = {}, givenConfig = {}) => {
|
|
|
|
const graphiqlOptions = { ...defaultConfig.graphiqlOptions, ...givenConfig.graphiqlOptions };
|
|
|
|
const config = { ...defaultConfig, ...givenConfig };
|
2017-02-06 14:33:34 +08:00
|
|
|
config.graphiqlOptions = graphiqlOptions;
|
|
|
|
|
|
|
|
const graphQLServer = express();
|
|
|
|
|
2017-02-12 22:00:13 +08:00
|
|
|
config.configServer(graphQLServer);
|
2017-02-06 14:33:34 +08:00
|
|
|
|
|
|
|
// Use Optics middleware
|
|
|
|
if (process.env.OPTICS_API_KEY) {
|
|
|
|
graphQLServer.use(OpticsAgent.middleware());
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphQL endpoint
|
2017-03-11 11:17:50 +08:00
|
|
|
graphQLServer.use(config.path, bodyParser.json(), graphqlExpress(async (req) => {
|
2017-02-12 22:00:13 +08:00
|
|
|
let options;
|
|
|
|
let user = null;
|
2017-02-06 14:33:34 +08:00
|
|
|
|
2017-02-12 22:00:13 +08:00
|
|
|
if (typeof givenOptions === 'function') {
|
2017-02-06 14:33:34 +08:00
|
|
|
options = givenOptions(req);
|
2017-02-12 22:00:13 +08:00
|
|
|
} else {
|
2017-02-06 14:33:34 +08:00
|
|
|
options = givenOptions;
|
2017-02-12 22:00:13 +08:00
|
|
|
}
|
2017-02-06 14:33:34 +08:00
|
|
|
|
|
|
|
// Merge in the defaults
|
2017-02-12 22:00:13 +08:00
|
|
|
options = { ...defaultOptions, ...options };
|
2017-02-06 14:33:34 +08:00
|
|
|
if (options.context) {
|
|
|
|
// don't mutate the context provided in options
|
2017-02-12 22:00:13 +08:00
|
|
|
options.context = { ...options.context };
|
2017-02-06 14:33:34 +08:00
|
|
|
} else {
|
|
|
|
options.context = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add Optics to GraphQL context object
|
|
|
|
if (process.env.OPTICS_API_KEY) {
|
|
|
|
options.context.opticsContext = OpticsAgent.context(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the token from the header
|
|
|
|
if (req.headers.authorization) {
|
|
|
|
const token = req.headers.authorization;
|
|
|
|
check(token, String);
|
|
|
|
const hashedToken = Accounts._hashLoginToken(token);
|
|
|
|
|
|
|
|
// Get the user from the database
|
2017-02-12 22:00:13 +08:00
|
|
|
user = await Meteor.users.findOne(
|
|
|
|
{ 'services.resume.loginTokens.hashedToken': hashedToken },
|
|
|
|
);
|
2017-02-06 14:33:34 +08:00
|
|
|
|
|
|
|
if (user) {
|
2017-02-12 22:00:13 +08:00
|
|
|
const loginToken = Utils.findWhere(user.services.resume.loginTokens, { hashedToken });
|
|
|
|
const expiresAt = Accounts._tokenExpiration(loginToken.when);
|
2017-02-06 14:33:34 +08:00
|
|
|
const isExpired = expiresAt < new Date();
|
|
|
|
|
|
|
|
if (!isExpired) {
|
|
|
|
options.context.userId = user._id;
|
|
|
|
options.context.currentUser = user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
options.context = deepmerge(options.context, GraphQLSchema.context);
|
|
|
|
|
|
|
|
return options;
|
2017-03-11 11:17:50 +08:00
|
|
|
}));
|
2017-02-06 14:33:34 +08:00
|
|
|
|
|
|
|
// Start GraphiQL if enabled
|
|
|
|
if (config.graphiql) {
|
2017-03-11 11:17:50 +08:00
|
|
|
graphQLServer.use(config.graphiqlPath, graphiqlExpress({ ...config.graphiqlOptions, endpointURL: config.path }));
|
2017-02-06 14:33:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This binds the specified paths to the Express server running Apollo + GraphiQL
|
2017-03-11 11:17:50 +08:00
|
|
|
webAppConnectHandlersUse(Meteor.bindEnvironment(graphQLServer), {
|
2017-02-12 22:00:13 +08:00
|
|
|
name: 'graphQLServerMiddleware_bindEnvironment',
|
|
|
|
order: 30,
|
|
|
|
});
|
2017-02-06 14:33:34 +08:00
|
|
|
};
|
|
|
|
|
2017-02-12 22:00:13 +08:00
|
|
|
// createApolloServer when server startup
|
|
|
|
Meteor.startup(() => {
|
|
|
|
|
|
|
|
// typeDefs
|
|
|
|
const generateTypeDefs = () => [`
|
|
|
|
scalar JSON
|
|
|
|
scalar Date
|
|
|
|
|
|
|
|
${GraphQLSchema.getCollectionsSchemas()}
|
|
|
|
${GraphQLSchema.getAdditionalSchemas()}
|
|
|
|
|
|
|
|
type Query {
|
|
|
|
${GraphQLSchema.queries.join('\n')}
|
|
|
|
}
|
|
|
|
|
|
|
|
${GraphQLSchema.mutations.length > 0 ? `
|
|
|
|
type Mutation {
|
|
|
|
${GraphQLSchema.mutations.join('\n')}
|
|
|
|
}
|
|
|
|
` : ''}
|
|
|
|
`];
|
|
|
|
|
2017-02-06 14:33:34 +08:00
|
|
|
const typeDefs = generateTypeDefs();
|
|
|
|
|
|
|
|
GraphQLSchema.finalSchema = typeDefs;
|
|
|
|
|
|
|
|
const schema = makeExecutableSchema({
|
|
|
|
typeDefs,
|
|
|
|
resolvers: GraphQLSchema.resolvers,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (process.env.OPTICS_API_KEY) {
|
|
|
|
OpticsAgent.instrumentSchema(schema)
|
|
|
|
}
|
|
|
|
|
|
|
|
createApolloServer({
|
|
|
|
schema,
|
|
|
|
});
|
|
|
|
});
|