Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to the typedql builder #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions graphql/schema.graphql

This file was deleted.

2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"builders": {
"node": "3.x",
"graphql": "1.x",
"typedql": "0.x",
"react": "2.x",
"pages": "0.x"
},
Expand Down
16 changes: 9 additions & 7 deletions node/dataSources/bookDataSource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DataSource } from 'apollo-datasource'
import { find, findIndex, propEq } from 'ramda'
import { Book, BookInput, ID, Int } from '../../typedql/schema'

const mock = [
{'id': '0', 'name': 'Default Book 1', 'authors': ['Default Author 1', 'Default Author 2']},
Expand All @@ -17,7 +18,7 @@ const mock = [
{'id': '22', 'name': 'Default Book 3', 'authors': ['Default Author 1', 'Default Author 2']},
{'id': '23', 'name': 'Default Book 4', 'authors': ['Default Author 1', 'Default Author 2']},
{'id': '24', 'name': 'Default Book 5', 'authors': ['Default Author 1', 'Default Author 2']},
]
] as Book[]

export class BookDataSource extends DataSource<Context> {
private db = mock
Expand All @@ -26,31 +27,32 @@ export class BookDataSource extends DataSource<Context> {
super()
}

public book = (id: string) => find(propEq('id', id), this.db)
public book = (id: ID) => find(propEq('id', id), this.db)

public books = ({from, to}: {from: number, to: number}) => this.db.slice(Math.max(from, 0), Math.min(to, this.db.length))
public books = (from = 0, to = 4) => this.db.slice(Math.max(from, 0), Math.min(to, this.db.length))

public total = () => this.db.length
public total = (): Int => this.db.length

public editBook = (id: string, book: BookInput): Maybe<Book> => {
public editBook = (id: ID, book: BookInput): Book | null => {
const foundIndex = findIndex(propEq('id', id), this.db)
if (0 <= foundIndex && foundIndex < this.db.length) {
const foundBook = this.db[foundIndex]
this.db[foundIndex] = {...foundBook, ...book}
return this.db[foundIndex]
}
return null
}

public newBook = (book: BookInput): Book => {
const newBook = {
...book,
id: this.db.length.toString(),
}
} as Book
this.db.unshift(newBook)
return newBook
}

public delete = (id: string) => {
public delete = (id: ID): boolean => {
const foundIndex = findIndex(propEq('id', id), this.db)
if (0 <= foundIndex && foundIndex < this.db.length) {
this.db.splice(foundIndex, 1)
Expand Down
8 changes: 7 additions & 1 deletion node/resolvers/book.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export const book = (_: any, {id}: {id: string}, {dataSources: {database}}: Context) => database.book(id)
import { Query } from '../../typedql/schema'

interface Args {
id:Parameters<Query['book']>[0]
}

export const book = (_: any, {id}: Args, {dataSources: {database}}: Context) => database.book(id)
8 changes: 5 additions & 3 deletions node/resolvers/books.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Query } from '../../typedql/schema'

interface Args {
from: number
to: number
from:Parameters<Query['books']>[0]
to:Parameters<Query['books']>[1]
}

export const books = (_: any, args: Args, {dataSources: {database}}: Context) => database.books(args)
export const books = (_: any, args: Args, {dataSources: {database}}: Context) => database.books(args.from, args.to)
4 changes: 3 additions & 1 deletion node/resolvers/delete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Mutation } from '../../typedql/schema'

interface Args {
id: string
id: Parameters<Mutation['delete']>[0],
}

export const deleteBook = (_: any, {id}: Args, {dataSources: {database}}: Context) => database.delete(id)
9 changes: 5 additions & 4 deletions node/resolvers/editBook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
interface EditBookArg {
id: string,
book: BookInput
import { Mutation } from './../../typedql/schema'
interface EditBookArgs {
id: Parameters<Mutation['editBook']>[0],
book: Parameters<Mutation['editBook']>[1]
}

export const editBook = (_: any, {id, book}: EditBookArg, {dataSources: {database}}: Context) =>
export const editBook = (_: any, {id, book}: EditBookArgs, {dataSources: {database}}: Context) =>
database.editBook(id, book)
3 changes: 2 additions & 1 deletion node/resolvers/newBook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Mutation } from './../../typedql/schema'
interface Args {
book: BookInput
book: Parameters<Mutation['newBook']>[0],
}

export const newBook = (_: any, {book}: Args, {dataSources: {database}}: Context) => database.newBook(book)
8 changes: 6 additions & 2 deletions node/resolvers/source.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Query } from '../../typedql/schema'

interface Args {
id: string
id:Parameters<Query['source']>[0]
}

export const source = (_: any, {id}: Args, {dataSources: {markdown}}: Context) => markdown.get(id)
export const source = (_: any, {id}:Args, {dataSources: {markdown}}: Context) => {
return markdown.get(id)
}
14 changes: 0 additions & 14 deletions node/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@ declare global {
database: BookDataSource
markdown: MardownDataSource
}

interface Book {
id: string
cacheId?: string
name: string
authors: string[]
}

interface BookInput {
name: Book['name'],
authors: Book['authors']
}

type Maybe<T> = T | void
}

export {}
39 changes: 39 additions & 0 deletions typedql/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/** @graphql Int */
export type Int = number

/** @graphql ID */
export type ID = string

export interface Book {
id: ID
cacheId: ID
name?: string
authors?: string[]
}

/** @graphql input */
export interface BookInput {
name?: string
authors?: string[]
}

export interface Query {
/**
* @graphql Directives
* @cacheControl (scope:PUBLIC, maxAge: SHORT)
*/
books(from?:Int, to?:Int): Book[]
book(id: ID): Book | null
total?: Int
/**
* @graphql Description
* Returns a markdown source given an id
*/
source(id: ID): String | null
}

export interface Mutation {
editBook(id: ID, book: BookInput): Book | null
newBook(book: BookInput): Book | null
delete(id: string): Boolean | null
}