'use strict';

const _ = require('lodash');

const opticsAgent = require('optics-agent');

const {
    graphqlExpress,
    graphiqlExpress
} = require('graphql-server-express');

const bodyParser = require('body-parser');

const featureContext = {};

const features = require('../util/features');

if (features.has('plebiscite')) {
    _.assign(featureContext, {
        plebiscite: require('../features/plebiscite')
    });
}

const querySchema = require('../graph');

const Connector = require('../graph/storage/connector');
const {Substances} = require('../graph/storage/models');

const SMWDataArbitrator = require('../graph/helpers/smwDataArbitrator');
const smwDataArbitrator = new SMWDataArbitrator();

const PWPropParser = require('../graph/helpers/pwPropParser');

const pwPropParser = new PWPropParser({
    smwDataArbitrator
});

module.exports = function* ({app, log}) {
    const baseQuerySchema = querySchema({log});

    app.use(opticsAgent.middleware());

    app.get('/', graphiqlExpress({
        endpointURL: '/',
        query:
`{
  substances {
    name

    effects {
      name
    }
  }
}`,
    }));

    app.post('/', bodyParser.json(), (req, res, next) =>
        graphqlExpress({
            schema: opticsAgent.instrumentSchema(baseQuerySchema.schema),
            rootValue: baseQuerySchema.root(req, res),
            context: _.assign({}, {
                substances: new Substances({
                    connector: new Connector({log}),
                    pwPropParser,
                    log
                }),
                opticsContext: opticsAgent.context(req)
            }, featureContext)
        })(req, res, next)
    );
};

@@ -1060,51 +1060,218 @@
 nces
- %7B%0A    name%0A%0A    effects %7B%0A      name%0A    %7D
+(query: %22Armodafinil%22) %7B%0A    effects %7B%0A      name%0A      substances %7B%0A        addictionPotential%0A        crossTolerances%0A        featured%0A        name%0A        summary%0A        url%0A      %7D%0A      url%0A    %7D%0A    name
 %0A  %7D

Make the default query more complex