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
}
And in the GraphQL schema I want to have a different type:
type Image {
id: ID!
image: 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()
}
EDIT
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.
Clarification
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)
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 = ""
}
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)
}
}
But it would be cleaner if I could tell the graphql schema parser to do that.
See Question&Answers more detail:
os