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转换为String的代码片段.要进行这种转换,最好使用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屋!