From 440d6b7e07354d660bf9f06bea1c1c047320d38a Mon Sep 17 00:00:00 2001 From: FelipeCoimbra Date: Thu, 14 Feb 2019 16:15:43 -0200 Subject: [PATCH] Migrate to the typedql builder --- graphql/schema.graphql | 34 -------------------------- manifest.json | 2 +- node/dataSources/bookDataSource.ts | 16 ++++++------ node/resolvers/book.ts | 8 +++++- node/resolvers/books.ts | 8 +++--- node/resolvers/delete.ts | 4 ++- node/resolvers/editBook.ts | 9 ++++--- node/resolvers/newBook.ts | 3 ++- node/resolvers/source.ts | 8 ++++-- node/typings.d.ts | 14 ----------- typedql/schema.ts | 39 ++++++++++++++++++++++++++++++ 11 files changed, 77 insertions(+), 68 deletions(-) delete mode 100644 graphql/schema.graphql create mode 100644 typedql/schema.ts diff --git a/graphql/schema.graphql b/graphql/schema.graphql deleted file mode 100644 index 2d71adf..0000000 --- a/graphql/schema.graphql +++ /dev/null @@ -1,34 +0,0 @@ -# To really understand GraphQL, go to http://graphql.org/guide/ - -type Book { - id: ID! - cacheId: ID! - name: String - authors: [String!] -} - -# It is necessary to generate input types for creating books -# ref: http://graphql.org/graphql-js/mutations-and-input-types/ -input BookInput { - name: String - authors: [String!] -} - -type Query { - # Cache control can only be set at root fields - books(from: Int = 0, to: Int = 4): [Book] @cacheControl(scope: PUBLIC, maxAge: SHORT) - book(id: ID!): Book - total: Int - - """ - Returns a markdown source given an id - """ - source(id: ID!): String -} - -type Mutation { - # You should always return the whole object in a mutation, not only a boolean. This will help in browser caching - editBook(id: ID!, book: BookInput!): Book - newBook(book: BookInput!): Book - delete(id: String): Boolean -} diff --git a/manifest.json b/manifest.json index 0575cb5..c02ee01 100644 --- a/manifest.json +++ b/manifest.json @@ -9,7 +9,7 @@ }, "builders": { "node": "3.x", - "graphql": "1.x", + "typedql": "0.x", "react": "2.x", "pages": "0.x" }, diff --git a/node/dataSources/bookDataSource.ts b/node/dataSources/bookDataSource.ts index b415a20..f8cc29a 100644 --- a/node/dataSources/bookDataSource.ts +++ b/node/dataSources/bookDataSource.ts @@ -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']}, @@ -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 { private db = mock @@ -26,31 +27,32 @@ export class BookDataSource extends DataSource { 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 => { + 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) diff --git a/node/resolvers/book.ts b/node/resolvers/book.ts index 7208ede..22ffe26 100644 --- a/node/resolvers/book.ts +++ b/node/resolvers/book.ts @@ -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[0] +} + +export const book = (_: any, {id}: Args, {dataSources: {database}}: Context) => database.book(id) diff --git a/node/resolvers/books.ts b/node/resolvers/books.ts index b0dcb1d..55f31b1 100644 --- a/node/resolvers/books.ts +++ b/node/resolvers/books.ts @@ -1,6 +1,8 @@ +import { Query } from '../../typedql/schema' + interface Args { - from: number - to: number + from:Parameters[0] + to:Parameters[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) diff --git a/node/resolvers/delete.ts b/node/resolvers/delete.ts index d3fb5f1..773fe59 100644 --- a/node/resolvers/delete.ts +++ b/node/resolvers/delete.ts @@ -1,5 +1,7 @@ +import { Mutation } from '../../typedql/schema' + interface Args { - id: string + id: Parameters[0], } export const deleteBook = (_: any, {id}: Args, {dataSources: {database}}: Context) => database.delete(id) diff --git a/node/resolvers/editBook.ts b/node/resolvers/editBook.ts index f249f5c..4d1523b 100644 --- a/node/resolvers/editBook.ts +++ b/node/resolvers/editBook.ts @@ -1,7 +1,8 @@ -interface EditBookArg { - id: string, - book: BookInput +import { Mutation } from './../../typedql/schema' +interface EditBookArgs { + id: Parameters[0], + book: Parameters[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) diff --git a/node/resolvers/newBook.ts b/node/resolvers/newBook.ts index 05296de..3eebe82 100644 --- a/node/resolvers/newBook.ts +++ b/node/resolvers/newBook.ts @@ -1,5 +1,6 @@ +import { Mutation } from './../../typedql/schema' interface Args { - book: BookInput + book: Parameters[0], } export const newBook = (_: any, {book}: Args, {dataSources: {database}}: Context) => database.newBook(book) diff --git a/node/resolvers/source.ts b/node/resolvers/source.ts index e7e1f16..188ce9e 100644 --- a/node/resolvers/source.ts +++ b/node/resolvers/source.ts @@ -1,5 +1,9 @@ +import { Query } from '../../typedql/schema' + interface Args { - id: string + id:Parameters[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) +} diff --git a/node/typings.d.ts b/node/typings.d.ts index f46b5c1..1c2f1f5 100644 --- a/node/typings.d.ts +++ b/node/typings.d.ts @@ -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 | void } export {} diff --git a/typedql/schema.ts b/typedql/schema.ts new file mode 100644 index 0000000..a8d3778 --- /dev/null +++ b/typedql/schema.ts @@ -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 +}