Skip to content

Commit

Permalink
Add ScalarType feature and graphql-scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
IceMimosa committed May 11, 2020
1 parent 2b631bf commit 8fd17d2
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 61 deletions.
10 changes: 8 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"graphql": "^14.6.0",
"graphql-tools": "^4.0.6",
"merge-graphql-schemas": "^1.7.6",
"graphql-scalars": "^1.1.2",
"nodemon": "^2.0.3",
"supertest": "^4.0.2",
"jest": "^25.5.0",
Expand All @@ -48,6 +49,7 @@
"peerDependencies": {
"graphql": "^14.6.0",
"graphql-tools": "^4.0.6",
"merge-graphql-schemas": "^1.7.6"
"merge-graphql-schemas": "^1.7.6",
"graphql-scalars": "^1.1.2"
}
}
27 changes: 27 additions & 0 deletions src/__tests__/daze/app/_common.graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { graphQL, scalar } from '../../../core/graphql.decorator';
import { Kind } from 'graphql';

@graphQL('Common')
export class CommonGraphql {

@scalar('Date')
dateResolver() {
return {
name: 'Date',
description: 'Date custom scalar type',
parseValue(value: any) {
return new Date(value); // value from the client
},
serialize(value: any) {
return value.getTime() + 1; // value sent to the client
},
parseLiteral(ast: any) {
if (ast.kind === Kind.INT) {
return new Date(+ast.value); // ast value is always in string format
}
return null;
},
};
}

}
20 changes: 10 additions & 10 deletions src/__tests__/daze/app/author.graphql.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
import { Inject } from '@dazejs/framework/dist';
import { GraphQL, Mutation, Query } from '../../../index';
import { inject } from '@dazejs/framework/dist';
import { graphQL, mutation, query } from '../../../index';
import AuthorService, { Author } from './service/author.service';
import BookService, { Book } from './service/book.service';

@GraphQL('Author')
@graphQL('Author')
export default class AuthorGraphql {

@Inject(AuthorService)
@inject(AuthorService)
private authorService: AuthorService;
@Inject(BookService)
@inject(BookService)
private bookService: BookService;

/**
* create a author
*/
@Mutation()
@mutation()
createAuthor(author: Author): Author {
return this.authorService.createAuthor(author);
}

/**
* query all authors
*/
@Query()
@query()
findAllAuthors(): Array<Author> {
return this.authorService.findAllAuthors();
}

/**
* find author by id
*/
@Query()
@query()
findAuthorById({ id }: Author): Author | undefined {
return this.authorService.findAuthorById(id);
}

/**
* update a
* update a author with book ids
*/
@Mutation()
@mutation()
updateAuthorBooks({id, bookIds}: Author): Author | undefined {
return this.authorService.updateAuthor(id, bookIds);
}
Expand Down
16 changes: 8 additions & 8 deletions src/__tests__/daze/app/book.graphql.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import { Inject } from '@dazejs/framework/dist';
import { GraphQL, Mutation, Query } from '../../../index';
import { inject } from '@dazejs/framework/dist';
import { graphQL, mutation, query } from '../../../index';
import BookService, { Book } from './service/book.service';

@GraphQL('Book')
@graphQL('Book')
export default class BookGraphQL {

@Inject(BookService)
@inject(BookService)
bookService: BookService;

/**
* create a book
*/
@Mutation()
@mutation()
createBook(book: Book): Book {
return this.bookService.createBook(book);
}

/**
* update a book
*/
@Mutation()
@mutation()
updateBook({ id, name }: Book) {
return this.bookService.updateBook(id, name);
}

/**
* query all books
*/
@Query()
@query()
findAllBooks(): Array<Book> {
return this.bookService.findAllBooks();
}

/**
* find book by id
*/
@Query()
@query()
findBookById({ id }: Book): Book | undefined {
return this.bookService.findBookById(id);
}
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/daze/app/hello.graphql.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { GraphQL, Query } from '../../../index';
import { graphQL, query } from '../../../index';

@GraphQL()
@graphQL()
export default class HelloGraphql {

@Query()
@query()
hello({ str }: any, { request }: any) {
const testHeader = request.getHeader('test-header') ?? '';
return `Hello ${str}${testHeader}`;
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/daze/app/service/author.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export default class AuthorService extends Service {
export interface Author {
id: number;
name: string;
birth: Date;
bookIds: Array<number>;
}
2 changes: 1 addition & 1 deletion src/__tests__/daze/config/graphql/author.resolver.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ type Query {
}

type Mutation {
createAuthor(name: String!): Author!
createAuthor(name: String!, birth: Date): Author!
updateAuthorBooks(id: Int!, bookIds: [Int]!): Author!
}
1 change: 1 addition & 0 deletions src/__tests__/daze/config/graphql/author.schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
type Author {
id: Int!
name: String!
birth: Date
books: [Book]
}
12 changes: 12 additions & 0 deletions src/__tests__/src/author.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ describe('test author', () => {
expect(res2.body.data.findAuthorById).toEqual(author);
});

it('test create author with date', async () => {
const birth = 1577808000000;
const res = await request(app._server).post(`/graphql?query=mutation { createAuthor(name: "Tom", birth: ${birth}) { id, name } }`);
const author = await res.body.data.createAuthor;
expect(author).toBeTruthy();
const res2 = await request(app._server).post(`/graphql?query={ findAuthorById(id: ${author.id}) { id, name, birth } }`);
expect(res2.body.data.findAuthorById).toEqual({
...author,
birth: birth + 1,
});
});

it('test update author', async () => {
const res = await request(app._server).post('/graphql?query=mutation { createAuthor(name: "Tom2") { id, name } }');
const author = await res.body.data.createAuthor;
Expand Down
Loading

0 comments on commit 8fd17d2

Please sign in to comment.