const express         = require('express')
const expressGraphql  = require('express-graphql')
const { buildSchema } = require('graphql')
const isGitUrl        = require('is-git-url')
const graphqlTools    = require('graphql-tools')
const cookieSession   = require('cookie-session')
const shortid         = require('shortid')

const schema = `
   type UserError {
       message : String
   }

    type Repo {
        progress : Int
        folder   : String
    }

    union Result = Repo | UserError

    type Query {
        repo(url : String) : Result
        me : String!
    }

    schema {
        query : Query
    }
`

const resolverMap = {
   Query: {
       repo({session}, {url}) {
           if (! isGitUrl(url)) {
               return {message: 'Invalid git URL'}
           }
           return {folder: repoToFolder(url), progress: 0}
       },
       me({session}) {
           return session.id
       }
   },
   Result: {
      __resolveType(root, context, info){
          if (root.folder != null) {
              return 'Repo'
          } else if (root.message != null) {
              return 'UserError'
          }
          return null;
      },
   },
}

const executableSchema = graphqlTools.makeExecutableSchema({
    typeDefs: schema,
    resolvers: resolverMap,
})

const app = express()

const session = cookieSession({
    name: 'session',
    keys: ['secret squirrel'],
    maxAge: 24 * 60 * 60 * 1000 // 24 hours
})

app.use(session)

app.all('*', (req, res, next) =>  {
    if (req.session.id == null) {
        req.session.id = shortid.generate()
    }
    return next()
})

app.get('/', (req, res) =>  {
    return res.send(req.session.id)
})

app.use('/graphql', expressGraphql((req) =>  {
   return {
       schema: executableSchema,
       graphiql: true,
       rootValue: { session: req.session },
   }
}))


app.listen(4000)
console.log('Running a GraphQL API server at localhost:4000/graphql')

function repoToFolder(repoURL)  {
    let folder = repoURL.replace(/^http:\/\//,'')
    folder = folder.replace(/^https:\/\//,'')
    folder = folder.replace(/^git:\/\//,'')
    folder = folder.replace(/^.+?@/,'')
    return folder.replace(/:/,'/')
}

@@ -572,24 +572,90 @@
 ing!%0A    %7D%0A%0A
+    type Mutation %7B%0A        addRepo(url : String) : Result%0A    %7D%0A%0A
     schema %7B
@@ -677,16 +677,44 @@
 : Query%0A
+        mutation : Mutation%0A
     %7D%0A%60%0A
@@ -1014,16 +1014,239 @@
 %7D%0A   %7D,%0A
+   Mutation: %7B%0A       addRepo(%7Bsession%7D, %7Burl%7D) %7B%0A           if (! isGitUrl(url)) %7B%0A               return %7Bmessage: 'Invalid git URL'%7D%0A           %7D%0A           return %7Bfolder: repoToFolder(url), progress: 0%7D%0A       %7D,%0A   %7D,%0A
    Resul

Add addRepo, a graphql mutation