Source: src/server.js

import express from 'express';
import bodyParser from 'body-parser';
import favicon from 'serve-favicon';
import morgan from 'morgan';
import knex from '../db/knex_router';
import path from 'path';
import ldap_auth from './ldap_auth/ldap_auth';
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express';
import { makeExecutableSchema } from 'graphql-tools';

/** 
 * @file Cree le serveur express avec tous les middleware qui vont bien
*/

const server = express();

// on sait pas a quoi ca sert mais il parait que c'est utile
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({
    extended: false
}));

// setting up view engine for pug
let viewpath = path.resolve('./','src','views');
console.log(viewpath);
server.set('views', viewpath);
server.set('view engine', 'pug');

// favicon: capital sigma symbol
server.use(favicon(path.resolve('./','assets','favicon.ico')));

// Morgan is middleware for logging requests
server.use(morgan('dev'));

// setting up ldap authentication
ldap_auth(server);

const typeDefs = `
    type Query {
        groups: [Group]
    }

    type Group {
        name: String!
        id: ID!
        updatedAt: String!
        description: String
        school: String!
    }
`;

const resolvers = {
    Query: {
        groups: () => knex.select().from('groups')
    }
};

const schema = makeExecutableSchema({
    typeDefs,
    resolvers
});

server.use('/graphql', bodyParser.json(), graphqlExpress({schema}));

server.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql'}));


export default server;