不为自定义标量类型的输入参数调用 `parseValue`

`parseValue` are not called for input parameter of a customised scalar type

我这样定义一个模式:

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 格式,因为我需要在运行时构建我的架构。

serialize 仅在响应中将标量发送回客户端时调用。它作为参数接收的值是解析器中返回的值(或者如果解析器返回了 Promise,则为 Promise 解析为的值)。

parseLiteral 仅在解析查询中的 文字 值时调用。文字值包括字符串 ("foo")、数字 (42)、布尔值 (true) 和 null。该方法作为参数接收的值是该文字值的 AST 表示。

parseValue 仅在解析查询中的变量值时调用。在这种情况下,该方法从与查询一起提交的 variables 对象中接收相关的 JSON 值作为参数。

所以,假设这样的模式:

type Query {
  someField(someArg: CustomScalar): String
  someOtherField: CustomScalar
}

序列化:

query {
  someOtherField: CustomScalar
}

解析文字:

query {
  someField(someArg: "something")
}

解析值:

query ($myVariable: CustomScalar) {
  someField(someArg: $myVariable)
}