【发布时间】:2020-04-05 06:42:08
【问题描述】:
我这样定义一个模式:
const query = new GraphQLObjectType({
name: 'Query',
fields: {
quote: {
type: queryType,
args: {
id: { type: QueryID }
},
},
},
});
const schema = new GraphQLSchema({
query,
});
QueryID 是自定义的标量类型。
const QueryID = new GraphQLScalarType({
name: 'QueryID',
description: 'query id field',
serialize(dt) {
// value sent to the client
return dt;
},
parseLiteral(ast) {
if (ast.kind === 'IntValue') {
return Number(ast.value);
}
return null;
},
parseValue(v) {
// value from the client
return v;
},
});
客户端查询
query {
quote(queryType: 1)
}
我发现当客户端向我的服务器发送查询时没有调用parseValue 方法。我可以看到parseLiteral 被正确调用。
在我能找到的大部分文档中,他们使用gql 来定义架构,他们需要将scalar QueryID 放在他们的架构定义中。但就我而言,我使用 GraphQLSchema 对象作为模式。这是根本原因吗?如果是,那么让它发挥作用的最佳方法是什么?我不想切换到gql 格式,因为我需要在运行时构建我的架构。
【问题讨论】: