Skip to content

Instantly share code, notes, and snippets.

@mtvspec
Last active July 19, 2020 17:59
Show Gist options
  • Save mtvspec/f0c60752968a41414976f9583565d4e4 to your computer and use it in GitHub Desktop.
Save mtvspec/f0c60752968a41414976f9583565d4e4 to your computer and use it in GitHub Desktop.
GraphQL Resolver Map Example
export const resolvers = {
Query: {
bookList: () => {
return {
count: (obj, args, ctx, info) => BookController.getBookListCount(),
nodes: (obj, args, ctx, info) => BookController.getBookList(),
}
},
book: (obj, args, ctx, info) => BookController.getBook(args['id']),
authorList: (obj, args, ctx, info) => {
return {
count: (obj, args, ctx, info) => AuthorController.getAuthorListCount(),
nodes: (obj, args, ctx, info) => AuthorController.getAuthorList(),
}
},
author: (obj, args, ctx, info) => AuthorController.getAuthor(args['id']),
},
Author: {
bookList: (obj, args, ctx, info) => {
return {
count: () => AuthorController.getAuthorBookListCount(obj['id']),
nodes: () => AuthorController.getAuthorBookList(obj['id']),
}
}
},
Book: {
author: (obj, args, ctx, info) => AuthorController.getAuthor(obj['author'])
},
Mutation: {
createBook: (obj, { data }, ctx, info) => BookController.createBook(data),
updateBook: (obj, { id, data }, ctx, info) => BookController.updateBook(id, data),
deleteBook: (obj, { id }, ctx, info) => BookController.deleteBook(id),
createAuthor: (obj, { data }, ctx, info) => AuthorController.createAuthor(data),
updateAuthor: (obj, { id, data }, ctx, info) => AuthorController.updateAuthor(id, data),
deleteAuthor: (obj, { id }, ctx, info) => AuthorController.deleteAuthor(id),
}
}
class BookController {
static bookList = [
{
id: 1,
title: 'Book 1',
description: 'Book 1 Description',
author: 1,
},
{
id: 2,
title: 'Book 2',
description: 'Book 2 Description',
author: 1,
},
{
id: 3,
title: 'Book 3',
description: 'Book 3 Description',
author: 2,
}
]
static getBookListCount = () => BookController.bookList.length
static getBookList = () => BookController.bookList
static getBook = (id: number) => BookController.bookList.filter(book => book.id == id)[0]
static createBook(data: any) {
data['id'] = BookController.bookList.length + 1
BookController.bookList.push(data)
return data
}
static updateBook(id: number, data: any) {
let book = BookController.bookList.filter(book => book.id == id)[0]
if (book) {
book = data
book['id'] = id
BookController.bookList = BookController.bookList.filter(book => book.id != id)
BookController.bookList.push(book)
return book
} else {
return null
}
}
static deleteBook(id: number) {
const book = BookController.bookList.filter(book => book.id == id)[0]
if (book) {
BookController.bookList = BookController.bookList.filter(book => book.id != id)
return book
} else {
return null
}
}
}
class AuthorController {
static authorList = [
{
id: 1,
name: 'Author 1',
bookList: {
count: [BookController.bookList[0], BookController.bookList[1]].length,
nodes: [BookController.bookList[0], BookController.bookList[1]]
}
},
{
id: 2,
name: 'Author 2',
bookList: {
count: [BookController.bookList[2]].length,
nodes: [BookController.bookList[2]]
}
}
]
static getAuthorListCount = () => AuthorController.authorList.length
static getAuthorList = () => AuthorController.authorList
static getAuthor = (id: number) => AuthorController.authorList.filter(author => author.id == id)[0]
static getAuthorBookListCount = (id: number) => {
const author = AuthorController.authorList.filter(author => author.id == id)
return author.length ? author[0].bookList['count'] : 0
}
static getAuthorBookList = (id: number) => BookController.bookList.filter(book => book.author == id)
static createAuthor(data: any) {
data['id'] = AuthorController.authorList.length + 1
AuthorController.authorList.push(data)
return data
}
static updateAuthor(id: number, data: any) {
let author = AuthorController.authorList.filter(author => author.id == id)[0]
if (author) {
author = data
author['id'] = id
AuthorController.authorList = AuthorController.authorList.filter(author => author.id != id)
AuthorController.authorList.push(author)
return author
} else {
return null
}
}
static deleteAuthor(id: number) {
const author = AuthorController.authorList.filter(author => author.id == id)[0]
if (author) {
AuthorController.authorList = AuthorController.authorList.filter(author => author.id != id)
return author
} else {
return null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment