My situation is the following.
I'm retrieving some image data from OpenAI's DALLE-2 API. The response gives me data as base64-encoded JSON. Once retrieved, I store it in a MongoDB database as Schema.Types.Buffer. I'm using Apollo-implementation of GraphQL for my API query language. I needed to create a custom scalar type so I could assign the Image model's binData field the custom scalar type ImgBinData. See below my type Image definition:
For the custom scalar type, I need to define the logic of how to handle it in the instance of the GraphQLScalarType class provided by Apollo Server. It's here where I create the Nodejs Buffer and save that to the image's binData field.
Where I'm not sure if what I'm doing is correct is once I want to retrieve the buffer data stored in an image's binData field. What do I do to convert this binary data back into the image it represents? Might I need to change what happens in the serialize method of my custom scalar? As of now, it's just returning the sequence of integers in the Buffer array:
I want to use this binary data as the source value for an image element in my view layer.
@Traynor, here is my React function component code:
import React from "react";
import { useQuery } from "@apollo/client";
import { GET_IMAGE_BY_ID } from "../utils/queries";
const ProfilePicture = () => {
// run query here
const { loading, error, data } = useQuery(GET_IMAGE_BY_ID, {
variables: { imageId: "64715e7413b66dd4d30eea3b" },
});
if (loading) return null;
if (error) return `Error! ${error}`;
const imgBinData = data?.imageByImageId.binData.data;
console.log(imgBinData);
const imgType = "image/png";
const blob = new Blob([new Uint8Array(imgBinData)], { type: imgType});
const src = URL.createObjectURL(blob);
return (
<img
src={src}
className=""
alt="A headshot of the user"
/>
);
};
export default ProfilePicture;
Here is my logic for the custom scalar:
const { GraphQLScalarType, Kind } = require("graphql");
const imgBinDataScalar = new GraphQLScalarType({
name: "ImgBinData",
description:
"Custom scalar for storing image bin data in form of Nodejs Buffer type",
serialize: (bufferObj) => bufferObj,
parseValue: (value) => Buffer.from(value, "base64"),
parseLiteral: (ast) => Buffer.from(ast.value, "base64"),
});
module.exports = {
imgBinDataScalar,
};
Below is a snapshot of the base64-encoded JSON I received from OpenAI's API. The value of the property b64_json is what I pass into as the value for my parseValue method in my custom scalar instance above. All that base64-encoded characters are fed into the Buffer.from method. I wanted to show you this so I don't leave any gaps from the moment I receive the data from OpenAI, through data transformation, up to wanting to display it in the img element.
Also, here is a snapshot of Chrome DevTools Network tab and the Elements tab: