Skip to content

Instantly share code, notes, and snippets.

@basharh
Created September 13, 2017 18:05
Show Gist options
  • Save basharh/536e4c3731e87ce87a9f83bcd791cb2c to your computer and use it in GitHub Desktop.
Save basharh/536e4c3731e87ce87a9f83bcd791cb2c to your computer and use it in GitHub Desktop.
API Schema
import {
GraphQLObjectType,
GraphQLString,
GraphQLFloat,
GraphQLList,
GraphQLSchema,
GraphQLInt,
buildSchema,
} from 'graphql';
import db from './models/db';
export const schema = buildSchema(`
input BookInput {
id: ID,
title_english: String,
title_arabic: String,
author_english: String,
isbn: String,
price: Float
shipping: Float,
image: String
}
type Book {
id: ID,
title_english: String,
title_arabic: String,
author_english: String,
isbn: String,
price: Float
shipping: Float,
image: String
}
type Library {
id: ID,
name: String,
location: String
description: String,
coords: [Float],
image: String
}
input LibraryInput {
id: ID,
name: String,
location: String
description: String,
coords: [Float],
image: String
}
type Session {
cart: [Int],
books: [Book]
}
type Query {
book(id: ID!): Book,
books(offset:Int, limit:Int): [Book],
library(id: ID!): Library,
libraries(offset:Int, limit:Int): [Library],
session: Session,
}
type Mutation {
addBookToCart(bookId: Int!): Session,
saveBook(book: BookInput!): Book,
saveLibrary(library: LibraryInput!): Library,
}
`);
function checkAdmin(req) {
if (!(req.user && req.user.role === 'admin')) {
return Promise.reject('unauthorized');
}
return Promise.resolve();
}
export const root = {
book: ({id}, req) => {
return db.Book.findOne({ where: { id } });
},
books: ({offset, limit}, req) => {
return db.Book.findAll({offset, limit});
},
library: ({id}) => {
return db.Library.findOne({ where: { id } });
},
libraries: ({offset, limit}) => {
return db.Library.findAll({offset, limit});
},
session: function (args, req) {
let cart = (req.session && req.session.data && req.session.data.cart) || [];
let books = db.Book.findAll({ where: { id: cart } });
return { cart, books };
},
addBookToCart: ({ bookId }, req) => {
let session = req.session;
session.data = session.data || {};
session.data.cart = session.data.cart || [];
// TODO: check if the book id exists before adding it to the session.
session.data.cart.push(bookId);
session.data.books = db.Book.findAll({ where: { id: session.data.cart } });
return session.data;
},
saveBook: ({ book }, req) => {
return checkAdmin(req).then(() => {
if(!book.id) { // insert
return db.Book.create(book, { returning: true });
}
//update
return db.Book.update(book, { where: { id: book.id }})
.then(([affectedCount, affectedRows]) => {
return db.Book.findOne({ where: { id: book.id } });
});
});
},
saveLibrary: ({ library }, req) => {
return checkAdmin(req).then(() => {
if(!library.id) { // insert
return db.Library.create(library, { returning: true });
}
//update
return db.Library.update(library, { where: { id: library.id }})
.then(([affectedCount, affectedRows]) => {
return db.Library.findOne({ where: { id: library.id } });
});
});
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment