diff --git a/src/graphql/schema.js b/src/graphql/schema.js index 4f2987d03355da0f672faf7b4c306c274ba4faf8..e844d0c575f82f2be4a987c9e7fd124f48bfed5a 100644 --- a/src/graphql/schema.js +++ b/src/graphql/schema.js @@ -10,7 +10,7 @@ import { request } from 'https'; const typeDefs = ` type Query { - groups: [Group]! + allGroups: [Group]! group(id : ID) : Group! } @@ -24,25 +24,22 @@ const typeDefs = ` } `; -const knex_req = { - - getAllVisibleGroups : (user) => { - let group_ids = listGroups(user.id); - return knex.select().from('groups').whereIn('id', group_ids); - }, +const getAllVisibleGroups = (user) => { + let group_ids = listGroups(user.id); + return knex.select().from('groups').whereIn('id', group_ids); +}; - getGroupIfVisible : (user, id) => { - return getAllVisibleGroups(user).where('id', args.id); - } +const getGroupIfVisible = (user, id) => { + return this.getAllVisibleGroups(user).where('id', id); }; const resolvers = { Query: { - groups: (obj, args, context) => { - return knex_req.getAllVisibleGroups(context.user); + allGroups: (obj, args, context) => { + return getAllVisibleGroups(context.user); }, group: (obj, args, context) => { - return knex_req.getGroupIfVisible(context.user, args.id); + return getGroupIfVisible(context.user, args.id); } } }; diff --git a/src/server.js b/src/server.js index eda3adc1cfadc03eef8399a1d77201b82d62ae32..00500627da7046efd2d40dfdd9d86c2b61123d00 100644 --- a/src/server.js +++ b/src/server.js @@ -16,7 +16,7 @@ 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 + extended: true })); // setting up view engine for pug @@ -32,10 +32,16 @@ server.use(favicon(path.resolve('./','assets','favicon.ico'))); server.use(morgan('dev')); // Charge le middleware express pour GraphQL -server.use('/graphql', cors(), bodyParser.json(), graphqlExpress({schema})); +server.use('/graphql', bodyParser.json(), + graphqlExpress(req => { + return { + schema : schema, + context : {user : {id : req.session.id}} + }; + })); // GraphiQL est une console interactive pour faire des requêtes au schéma GraphQL -server.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql'})); +server.use('/graphiql', graphiqlExpress({endpointURL: '/graphql'})); // connect-flash is middleware for flashing messages server.use(flash());