GraphQLScalarType 中的 parseValue 和 parseLiteral 有什么区别

编程入门 行业动态 更新时间:2024-10-27 20:30:37
本文介绍了GraphQLScalarType 中的 parseValue 和 parseLiteral 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看自定义标量类型的 GraphQL 文档(我正在尝试创建自己的日期类型)我不确定 parseValueparseLiteral 之间的区别是.

Looking through the GraphQL documentation for custom scalar types (I'm trying to create my own date type) I'm not sure what the difference between parseValue and parseLiteral are.

http://graphql/graphql-js/type/#graphqlscalartype

文档似乎没有包含对函数应该做什么的任何描述.

The documentation doesn't seem to include any descriptions of what the functions are supposed to do.

谁能告诉我要求是什么?我假设 serialize 必须将标量序列化为字符串.那是对的吗?我假设 parseLiteral 是将该字符串反序列化为类型?在我的情况下是 Date 类型.但是,在示例中 - serialize 和 parseValue 是同一个函数 - 这表明它不是一个简单的反序列化方法.

Can someone let me know what the requirements are? I'm assuming that serialize must serialize the scalar to a string. Is that correct? I'm assuming that parseLiteral is a deserialization of that string to the type? In my case a Date type. However, in the examples - serialize and parseValue are the same function - which suggests it's not a simple deserialization method.

推荐答案

serialize 方法将在类型的值作为响应发送到客户端时被调用.由于输出上的值是 JSON 的形式,serialize 的返回值可以是任何值.可以是字符串、数字、数组、对象...

The serialize method would be called when the value of the type is going to be sent to the client as a response. Since the values on the output is in the form of JSON, the return value of serialize could be anything. Could be string, number, array, object ...

另外两个方法(parseValueparseLiteral)是读取输入.

The other two methods (parseValue and parseLiteral) are to read input.

在 GraphQL 中有两种从客户端读取输入的方式,一种是在查询中内联,例如:

In GraphQL there are two ways of reading input from client, one is inline in the query, like:

query {
    allUsers(first:10) {
        id
    }
}

其中 10first 参数的内联值.由于 GraphQL 的输入语言不完全是 JSON,因此值(此处为 10)正在被解析并转换为 AST(抽象语法树).在这种情况下,parseLiteral 开始发挥作用.它输入 AST 并返回类型的解析值.类型可以像 JSON 一样复杂,parseLiteral 可以遍历 AST 并返回 JSON.

where 10 is the inline value for first argument. Since the input language for GraphQL is not exactly JSON, the value (here 10) is being parsed and converted to AST (Abstract Syntax Tree). In this case, parseLiteral comes to play. It inputs the AST and returns the parsed value of the type. Types could be as complex as JSON and parseLiteral could traverse the AST and return JSON.

从客户端读取输入的另一种方式是通过变量:

The other way of reading input from clients is through variables:

query ($howMany: YourCustomType) {
  users(first: $howMany) {
    id
  }
}

变量:

{
  "howMany": {
    "thisMany": 10
  }
}

由于变量是纯 JSON,所以这里不需要 AST,你已经有了 JSON.这就是 parseValue 发挥作用的地方.它以 JSON 形式获取输入并返回查询解析器应使用的任何内容.

Since the variables are pure JSON, you don't need AST here, you already have JSON. That's where parseValue comes to play. It gets the input as JSON and returns whatever the query resolver should use.

function parseValue(value) {
    let first = value.thisMany;
    return first;
}

因此,从变量中读取与从内联读取值时可能有不同的表示,但从概念上讲,它们在表示方面应该相同.但是,由于输入的类型"不同(内联是 GraphQL,变量是 JSON),解析算法可能会有所不同.这就是为什么如果将其定义为输入类型,则需要提供两个单独的方法来读取它们.

So, you could have different presentation when you read from variables than when you read values inline, but conceptually, they should be the same in terms of presentation. However since the "type" of input is different (inline is GraphQL and variable is JSON), the parsing algorithm could be different. That's why if you define it as input type, you need to provide two separate methods to read them.

这篇关于GraphQLScalarType 中的 parseValue 和 parseLiteral 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-27 02:24:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/674173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   GraphQLScalarType   parseValue   parseLiteral

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!