问题描述
可以在我的GraphQl架构中将一个字段定义为日期或JSON?
type Individual { id: Int name: String birthDate: Date token: JSON }
实际上,服务器正在向我返回一个错误:
Type "Date" not found in document. at ASTDefinitionBuilder._resolveType (****node_modules\graphql\utilities\buildASTSchema.js:134:11)
和JSON的同样错误...
有什么想法吗?
推荐答案
请看一下自定义标量: https:https://.apolographql.com/docs/graphql-tools/scalars.html
在您的架构中创建一个新的标量:
scalar Date type MyType { created: Date }
并创建一个新的解析器:
import { GraphQLScalarType } from 'graphql'; import { Kind } from 'graphql/language'; const resolverMap = { Date: new GraphQLScalarType({ name: 'Date', description: 'Date custom scalar type', parseValue(value) { return new Date(value); // value from the client }, serialize(value) { return value.getTime(); // value sent to the client }, parseLiteral(ast) { if (ast.kind === Kind.INT) { return parseInt(ast.value, 10); // ast value is always in string format } return null; }, }) };
其他推荐答案
原始 graphql 是Int,Float,String,Boolean和ID.对于JSON和Date,您需要定义自己的自定义标量类型,文档在如何执行此操作方面很清楚.
在您的模式中,您必须添加:
scalar Date type MyType { created: Date }
然后,在您的代码中,您必须添加类型实现:
import { GraphQLScalarType } from 'graphql'; const dateScalar = new GraphQLScalarType({ name: 'Date', parseValue(value) { return new Date(value); }, serialize(value) { return value.toISOString(); }, })
最后,您必须在解析器中包括此自定义标量类型:
const server = new ApolloServer({ typeDefs, resolvers: { Date: dateScalar, // Remaining resolvers.. }, });
此Date实现将解析 Date构造函数,并将以ISO格式为字符串返回日期.
对于JSON您可以使用 graphql-type-json 并导入它,如图所示.
问题描述
Is it possible to have a define a field as Date or JSON in my graphql schema ?
type Individual { id: Int name: String birthDate: Date token: JSON }
actually the server is returning me an error saying :
Type "Date" not found in document. at ASTDefinitionBuilder._resolveType (****node_modules\graphql\utilities\buildASTSchema.js:134:11)
And same error for JSON...
Any idea ?
推荐答案
Have a look at custom scalars: https://www.apollographql.com/docs/graphql-tools/scalars.html
create a new scalar in your schema:
scalar Date type MyType { created: Date }
and create a new resolver:
import { GraphQLScalarType } from 'graphql'; import { Kind } from 'graphql/language'; const resolverMap = { Date: new GraphQLScalarType({ name: 'Date', description: 'Date custom scalar type', parseValue(value) { return new Date(value); // value from the client }, serialize(value) { return value.getTime(); // value sent to the client }, parseLiteral(ast) { if (ast.kind === Kind.INT) { return parseInt(ast.value, 10); // ast value is always in string format } return null; }, }) };
其他推荐答案
Primitive scalar types in GraphQL are Int, Float, String, Boolean and ID. For JSON and Date you need to define your own custom scalar types, the documentation is pretty clear on how to do this.
In your schema you have to add:
scalar Date type MyType { created: Date }
Then, in your code you have to add the type implementation:
import { GraphQLScalarType } from 'graphql'; const dateScalar = new GraphQLScalarType({ name: 'Date', parseValue(value) { return new Date(value); }, serialize(value) { return value.toISOString(); }, })
Finally, you have to include this custom scalar type in your resolvers:
const server = new ApolloServer({ typeDefs, resolvers: { Date: dateScalar, // Remaining resolvers.. }, });
This Date implementation will parse any string accepted by the Date constructor, and will return the date as a string in ISO format.
For JSON you might use graphql-type-json and import it as shown here.