Graphql 工具:将实体类型映射到 graphql 类型 [英] Graphql Tools: Map entity type to graphql type
问题描述
我在 spring 应用程序中使用了 graphql java 工具.我有一个这样的实体:
I'm using graphql java tools in my spring application. I have an entity like this:
@Entity
data class Image(
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(nullable = false)
val id: Long = 0
) {
@Lob
@Column(nullable = false)
var image: Blob? = null
}
在 GraphQL 模式中,我想要一个不同的类型:
And in the GraphQL schema I want to have a different type:
type Image {
id: ID!
image: String!
}
当然,由于模式解析异常,启动此操作将不起作用.所以我认为必须可以在模式解析之间加入一些东西来实现从 Blob
到 String
的映射.
Starting this won't work, of course, because of a schema parsing exception.
So I thought it must be possible to put something in between the schema parsing to implement a mapping from Blob
to String
.
这在某种程度上可能吗?
Is this somehow possible here?
@Bean
fun schemaParserDictionary(): SchemaParserDictionary {
return SchemaParserDictionary()
}
编辑
抱歉,它不会给出模式解析异常.它只会将字节放入字符串中.但是我不能使用那个字符串来创建图像.
Sorry, it won't give a schema parsing exception. It will simply put the bytes into a string. But I can't use that string then to create the image.
澄清
抱歉,我想我的问题不够清楚.我不需要知道如何将 blob 转换为字符串.对此有非常简单的解决方案.例如我可以使用一个简单的 API 函数将字节码转换为 base64:
Sorry, I guess I was not clear enough with my question. I don't need to know how to transform a blob into a string. There are very easy solutions for that. For example I can use a simple API function to transform byte code into base64:
val base64 = Base64.getEncoder().encodeToString(byteCode)
我想问的是如何告诉"graphql 模式解析器为特定字段使用自定义"转换.因此,当我的实体对象具有数据类型为 blob
的字段 image
时,graphql 会自动将其转换为 string
.但是字符串不包含我需要的内容.所以我想对那个特定的领域使用自定义解析/映射/转换/任何东西.当然,我也可以通过引入另一个我没有存储在数据库中的字段来轻松解决这个问题:
What I tried to ask was how I can "tell" graphql schema parser to use that "custom" transformation for a specific field. So when my entity object has the field image
with datatype blob
graphql is automatically transforming this to a string
. But the string does not contain the content that I need. So I want to use a custom parsing / mapping / transformation / whatever for that particularly field. Of course, I could also easily get around this by introducing another field which I do not store in the database:
@Entity
data class Image(
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(nullable = false)
val id: Long = 0
) {
@Lob
@Column(nullable = false)
var image: Blob? = null
@Transient
var base64: String = ""
}
但是我可以通过 graphql api 查询:
but which I make queryable through the graphql api:
type Image {
id: ID!
image: String!
base64: String!
}
type Images {
images: [Image!]!
totalCount: Int!
}
type Query {
getImages: Images!
}
并将其设置在我的控制器中:
And set it in my controller:
private fun getImages(): Images {
var images: List<Image> = repository.findTop1000()
images.forEach {
it.base64 = Base64.getEncoder().encodeToString(it.image)
queue.offer(it)
}
}
但是如果我可以告诉 graphql 模式解析器这样做会更清晰.
But it would be cleaner if I could tell the graphql schema parser to do that.
推荐答案
Kushal 的回答提供了将 Blob 转换为字符串的片段.要使用这种转换,最好使用 DTO.
Kushal's answer provides the snippets to convert a Blob to a String. To use this conversion, it is good practice to use DTO.
为此,将您的 graphql 模式转换为:
To do so, transform your graphql schema to:
type ImageDTO {
id: ID!
image: String!
}
并创建一个 DTO 类 ImageDTO:
And create a DTO class ImageDTO:
public class ImageDTO {
private Image entity;
//Constructor
public ImageDTO(Image image) {
this.entity=image;
}
//Getters
public Long getId() {
return entity.getId();
}
public String getImage() {
//convert the entity image and return it as String
return Base64.getEncoder().encodeToString(entity.getImage());
}
}
将 DTO 与实体分开可以让您更好地控制传输的数据.
Separating the DTO from the entity grants you a finer control on the data transferred.
这篇关于Graphql 工具:将实体类型映射到 graphql 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!