MichelDiz
11/20/2017 - 2:36 PM

This is an example code of how the apollo-universal-starter-kit would work using Dgraph. A powerful graph database, fast, has clustering abi

This is an example code of how the apollo-universal-starter-kit would work using Dgraph. A powerful graph database, fast, has clustering ability, created in GoLang and so on. In this example I'm using a client that uses gRPC to communicate with Dgraph. See docs https://docs.dgraph.io/

// Helpers
//import { camelizeKeys, decamelizeKeys, decamelize } from 'humps';
import { has } from 'lodash';
import bcrypt from 'bcryptjs';
import DgraphClient from 'dgraph-node';

// Create a new client
let client = new DgraphClient({
    url: 'localhost:9080',
    debug: false
  })
// Transactions
const txn = client.txn() // Just setting up instance (no call to Dgraph)


//client.dropAll();// DO NOT USE IT if you don't know what you doing. This will clean the whole DB.

const mySchema = `	
    name: string @index(term) .
    email: string @index(term) .
    friend: uid @count .
    reputation: int @index(int) .
    serialPin: password .
    serial: int .
    pass: password .
    balance: int .
    age: int @index(int)  .
    married: bool .
    location: geo @index(geo) .
    when:   int .
    work_for: string @index(term)  .
`;
//To change the Schema, simply use the line below.
//client.alter(mySchema);


// Actual query fetching and transformation in DB
export default class User {
  
    async getUsersDgraph () {
    // need to add order by -> Dgraph has this feature
    // need to add filter conditions -> Dgraph has this feature
        return await client.query(`
        query {
          getUserByTYPE(func: eq(TYPE, "user")) {
              uid : id
              name : username
              role
              is_active
              email
              first_name
              last_name
              serial
              fb_id
              display_name
            }
          }
            `);
          
      }
   async getUserDgraph (id) {
    return await client.query(`
    query {
      getUserByUID(func: eq(uid, "${id}")) {
          uid : id
          name : username
          role
          is_active
          email
          first_name
          last_name
          serial
          fb_id
          display_name
        }
      }
        `);
      
  }
  async getUserWithPasswordDgraph (id) {
    //Using the password func from Dgraph there's no way to query it - is protected
    //see here about it https://docs.dgraph.io/query-language/#extended-types
        return await client.query(`
        query {
          getUserByUID(func: eq(uid, "${id}")) {
              uid : id
              name : username
              password
              role
              is_active
              email
              first_name
              last_name
              serial
              fb_id
              display_name
            }
          }
            `);
          
      }
  async registerDgraph({ username, email, password, role, isActive }) {
    const passwordHashed = await bcrypt.hash(password, 12);

    if (role === undefined) {
      role = 'user';
    }
      return await  client.mutate({
        set: `
        _:uid <name> "${username}" .
        _:uid <email>  "${email}" .
        _:uid <role>  "${role}" .
        _:uid <pass>  "${{password: passwordHashed}}" .
        _:uid <is_active> "${is_active}" .
             `,
    })
      
  }

  async  createFacebookOuthDgraph({ id, displayName, userId }) {
    return await  client.mutate({
            set: `
            _:uid <fb_id> "${id}" .
            _:uid <display_name>  "${displayName}" .
                `,
        })
      }

  async editAuthCertificateDgraph({ id, auth: { certificate: { serial } } }) {
    const userProfile = await client.query(`
            query {
              auth_certificate(func: allofterms(uid, "${id}")) {
                  serial
                }
              }
                `);

    if (userProfile) {
        return await  client.mutate({
          set: `
          <${{user_id: id}}> <serial>  "${serial}" .
              `,
      })
    } else {
      return await  client.mutate({
        set: `
        <${{user_id: id}}> <serial>  "${serial}" .
            `,
    })
    }
  }

    async deleteUserDgraph(id) {
    // Delete mutation
    // This will clean the whole data from this ID - careful! all will be gone!
    await client.mutate({
        del: `<${id}> * * .`,
    })
}
    async  updatePasswordDgraph(id, newPassword) {
    // Set mutation
    const password = await bcrypt.hash(newPassword, 12);
  return await  client.mutate({
          set: `
          <${id}> <pass>  "${password}" .
               `,
      })
}

    async  updateActiveDgraph(id, isActive) {
    // Set mutation
  return await  client.mutate({
          set: `
          <${id}> <is_active>  "${is_active}" .
               `,
      })
}
  async  getUserByEmailDgraph(email) {
    return await client.query(`
    query {
      getUserByEmail(func: eq(email, "${email}")) {
          uid
          username
          password
          role
          is_active
          email
          first_name
          last_name
        }
      }
        `);
}
  async  getUserByFbIdOrEmailDgraph(id, email) {
    return await client.query(`
    query {
      getUserByEmail(func: eq(email, "${email}")) {
          uid
          username
          password
          fb_id
          role
          is_active
          email
          first_name
          last_name
        }
      }
        `);
}
  async  getUserByUsernameDgraph(username) {
    return await client.query(`
    query {
      getUserByUsername(func: allofterms(name,"${username}")) {
          uid
          username
          role
          is_active
          email
          first_name
          last_name
        }
      }
        `);
}

}