From 787bbed7342e3e2250d73b4694b6aa2641c63864 Mon Sep 17 00:00:00 2001 From: piglovesyou Date: Sat, 22 Sep 2018 16:52:15 +0900 Subject: [PATCH] Add "Timestamp" scalar type for convinience * rm unnecessary files --- src/data/graphql/Scalar/Timestamp/index.js | 32 ++++++++++++++++++++++ src/data/graphql/Scalar/schema.js | 10 +++++++ src/data/schema.js | 8 +++++- src/data/types/NewsItemType.js | 27 ------------------ src/data/types/UserType.js | 25 ----------------- 5 files changed, 49 insertions(+), 53 deletions(-) create mode 100644 src/data/graphql/Scalar/Timestamp/index.js create mode 100644 src/data/graphql/Scalar/schema.js delete mode 100644 src/data/types/NewsItemType.js delete mode 100644 src/data/types/UserType.js diff --git a/src/data/graphql/Scalar/Timestamp/index.js b/src/data/graphql/Scalar/Timestamp/index.js new file mode 100644 index 000000000..16463ad68 --- /dev/null +++ b/src/data/graphql/Scalar/Timestamp/index.js @@ -0,0 +1,32 @@ +// @flow + +import { GraphQLScalarType } from 'graphql'; +import { Kind } from 'graphql/language'; + +export const schema = [ + ` + # GraphQL cannot handle long - normal timestamp will go failed. + # In that case, use Timestamp. + type Timestamp + +`, +]; + +export const resolvers = { + Timestamp: new GraphQLScalarType({ + name: 'Timestamp', + description: 'Timestamp custom scalar type', + parseValue(value) { + return value; + }, + serialize(value) { + return value; + }, + parseLiteral(ast) { + if (ast.kind === Kind.INT) { + return ast.value; + } + return null; + }, + }), +}; diff --git a/src/data/graphql/Scalar/schema.js b/src/data/graphql/Scalar/schema.js new file mode 100644 index 000000000..fb0fa635a --- /dev/null +++ b/src/data/graphql/Scalar/schema.js @@ -0,0 +1,10 @@ +import merge from 'lodash.merge'; + +import { + schema as TimestampSchema, + resolvers as TimestampResolvers, +} from './Timestamp'; + +export const schema = [...TimestampSchema]; + +export const resolvers = merge(TimestampResolvers); diff --git a/src/data/schema.js b/src/data/schema.js index d938a153e..f13c7e3fd 100644 --- a/src/data/schema.js +++ b/src/data/schema.js @@ -22,6 +22,11 @@ import { queries as DatabaseQueries, } from './graphql/Database/schema'; +import { + schema as TimestampSchema, + resolvers as TimestampResolvers, +} from './graphql/Scalar/Timestamp'; + const RootQuery = [ ` # # React-Starter-Kit Querying API @@ -68,10 +73,11 @@ const SchemaDefinition = [ // Merge all of the resolver objects together // Put schema together into one array of schema strings -const resolvers = merge(NewsResolvers, DatabaseResolvers); +const resolvers = merge(NewsResolvers, DatabaseResolvers, TimestampResolvers); const schema = [ ...SchemaDefinition, + ...TimestampSchema, ...RootQuery, ...Mutation, diff --git a/src/data/types/NewsItemType.js b/src/data/types/NewsItemType.js deleted file mode 100644 index 1933e1fb5..000000000 --- a/src/data/types/NewsItemType.js +++ /dev/null @@ -1,27 +0,0 @@ -/** - * React Starter Kit (https://www.reactstarterkit.com/) - * - * Copyright © 2014-present Kriasoft, LLC. All rights reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE.txt file in the root directory of this source tree. - */ - -import { - GraphQLObjectType as ObjectType, - GraphQLString as StringType, - GraphQLNonNull as NonNull, -} from 'graphql'; - -const NewsItemType = new ObjectType({ - name: 'NewsItem', - fields: { - title: { type: new NonNull(StringType) }, - link: { type: new NonNull(StringType) }, - author: { type: StringType }, - pubDate: { type: new NonNull(StringType) }, - content: { type: StringType }, - }, -}); - -export default NewsItemType; diff --git a/src/data/types/UserType.js b/src/data/types/UserType.js deleted file mode 100644 index d68165ee5..000000000 --- a/src/data/types/UserType.js +++ /dev/null @@ -1,25 +0,0 @@ -/** - * React Starter Kit (https://www.reactstarterkit.com/) - * - * Copyright © 2014-present Kriasoft, LLC. All rights reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE.txt file in the root directory of this source tree. - */ - -import { - GraphQLObjectType as ObjectType, - GraphQLID as ID, - GraphQLString as StringType, - GraphQLNonNull as NonNull, -} from 'graphql'; - -const UserType = new ObjectType({ - name: 'User', - fields: { - id: { type: new NonNull(ID) }, - email: { type: StringType }, - }, -}); - -export default UserType;