Skip to content

Instantly share code, notes, and snippets.

@hai5nguy
Created February 14, 2018 16:09
Show Gist options
  • Save hai5nguy/201dd87b68de8e7681e58e869762375e to your computer and use it in GitHub Desktop.
Save hai5nguy/201dd87b68de8e7681e58e869762375e to your computer and use it in GitHub Desktop.
import { dispatch } from '~/store/store'
import PadawanApi from '~/actions/padawan-api'
const create = async (student) => {
dispatch({
type: 'CREATE_STUDENT_START',
student
})
var graph = `
mutation {
createStudent ( name: "${student.name}" ) {
_id,
name
}
}
`
try {
var data = await PadawanApi.sendGraph(graph)
Object.assign(student, data.createStudent)
var action = {
type: 'CREATE_STUDENT_SUCCESS',
student
}
} catch (err) {
var action = {
type: 'CREATE_STUDENT_FAIL',
student
}
}
dispatch(action)
}
const update = async (student) => {
dispatch({
type: 'UPDATE_STUDENT_START',
student
})
var graph = `
mutation {
updateStudent ( _id: "${student._id}" name: "${student.name}" ) {
_id,
name
}
}
`
try {
var data = await PadawanApi.sendGraph(graph)
var action = {
type: 'UPDATE_STUDENT_SUCCESS',
student: data.updateStudent
}
} catch (err) {
var action = {
type: 'UPDATE_STUDENT_FAIL',
student,
error: err
}
}
dispatch(action)
}
const _delete = async (_id) => {
dispatch({
type: 'DELETE_STUDENT_START',
_id
})
var graph = `
mutation {
deleteStudent ( _id: "${_id}" ) {
status
}
}
`
try {
var data = await PadawanApi.sendGraph(graph)
} catch (err) {
dispatch({
type: 'DELETE_STUDENT_FAIL',
_id,
error: err
})
}
if (data.deleteStudent.status === 'DELETE_SUCCESS') {
dispatch({
type: 'DELETE_STUDENT_SUCCESS',
_id
})
} else {
dispatch({
type: 'DELETE_STUDENT_FAIL',
_id,
error: JSON.stringify(data)
})
}
}
const fetchAll = async () => {
dispatch({ type: 'FETCHING_STUDENTS_START' })
var graph = `
query {
students {
_id,
name
}
}
`
try {
var data = await PadawanApi.sendGraph(graph)
var action = {
type: 'FETCHING_STUDENTS_SUCCESS',
students: data.students
}
} catch (err) {
var action = {
type: 'FETCHING_STUDENTS_FAIL',
error: err
}
}
dispatch(action)
}
const deleteAll = async () => {
dispatch({ type: 'DELETE_ALL_STUDENTS_START' })
var graph = `
mutation {
deleteAllStudents {
status
}
}
`
try {
var data = await PadawanApi.sendGraph(graph)
} catch (err) {
dispatch({ type: 'DELETE_ALL_STUDENTS_FAIL' })
throw 'Student.deleteAll error: ' + err
}
if (data.deleteAllStudents.status === 'DELETE_SUCCESS') {
dispatch({ type: 'DELETE_ALL_STUDENTS_SUCCESS' })
} else {
dispatch({ type: 'DELETE_ALL_STUDENTS_FAIL' })
throw 'Student.deleteAll error, data: ' + JSON.stringify(data)
}
}
export default { create, update, delete: _delete, fetchAll, deleteAll }
import request from 'superagent'
const PADAWAN_API_URL = 'http://localhost:9000/graphql'
const sendGraph = async (graph) => {
return new Promise((resolve, reject) => {
request.post(PADAWAN_API_URL)
.set({ 'Content-Type': 'application/graphql' })
.send(graph)
.end((err, res) => {
if (err) {
reject(err)
} else {
resolve(res.body.data)
}
})
})
}
export default { sendGraph }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment