Type-graphql: Jsonb Support for postgres column

Created on 7 Feb 2020  路  4Comments  路  Source: MichalLytek/type-graphql

I have a address column that return jsonb but I am unable to map it to @Field() like city etc
sorry for any inconvenience, i am new :) .. thanks
image

Question Solved

Most helpful comment

Don't invite the wheel again - use ready and battle tested solution:
https://github.com/taion/graphql-type-json

All 4 comments

Please paste here your code. I think you return an object for field of type String, so you should use GraphQLJSON scalar.

I am trying to make JsonScalar .. but do not how to map it to Address ObjectType

///Scalar
export const JsonScalar = new GraphQLScalarType({
name: "JsonScalar",
description: "return response as a Json string ",
parseValue(value: any) {
return JSON.stringify(value); // value from the client input variables
},
serialize(value: any) {
return JSON.stringify(value); // value sent to the client
},
parseLiteral(ast) {
//console.log(ast, "from the Scalar")
if (ast.kind === Kind.STRING) {
return JSON.parse(ast.value); // value from the client query
}
return null;
}
});
/// Address ObjectType

@ObjectType()
@InputType("AddressInput")
export class Address {
@Field() name: string;
@Field() state?: string;
@Field() city?: string;
@Field() postcode?: number;
}
/// Under my customer class

@Field(() => JsonScalar)
@Column({ type: "jsonb" })
address: Address
/// Able to achieve result like this
{
"data": {
"Customer": [
{
"name": "Abrar Property Sdn Bhd",
"id": "a67ff16a-2bb4-42fb-aaa7-24080c527766",
"address": "{\"city\":\"Petaling Jaya\",\"name\":\"Block D,NO.2,Jalan PJU 24A/7A\",\"state\":\"Selangor\",\"postcode\":47301}"
}
]
}
}
// if i change the Scalar this part
serialize(value: any) {
return JSON.parse(value); // value sent to the client
},
my Graphql will return this error

{
"errors": [
{
"message": "Unexpected token o in JSON at position 1",
"locations": [
{
"line": 5,
"column": 5
}
],
"path": [
"Customer",
0,
"address"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"SyntaxError: Unexpected token o in JSON at position 1",
" at JSON.parse ()",
" at GraphQLScalarType.serialize (D:\work\Practice\node-crm\src\util\JsonScalar.ts:10:17)",
" at completeLeafValue (D:\work\Practice\node-crm\node_modules\graphql\execution\execute.js:635:37)",
" at completeValue (D:\work\Practice\node-crm\node_modules\graphql\execution\execute.js:579:12)",
" at completeValue (D:\work\Practice\node-crm\node_modules\graphql\execution\execute.js:557:21)",
" at D:\work\Practice\node-crm\node_modules\graphql\execution\execute.js:492:16",
" at processTicksAndRejections (internal/process/task_queues.js:94:5)",
" at async Promise.all (index 2)",
" at async Promise.all (index 0)",
" at async Promise.all (index 0)"
]
}
}
}
],
"data": {
"Customer": null
}
}

Thank you.

Don't invite the wheel again - use ready and battle tested solution:
https://github.com/taion/graphql-type-json

but do not how to map it to Address ObjectType

So if you want it to be an object type, just use it and not the scalar.

But be aware that the database has to return an object, not a JSON string - ask for TypeORM help how to map jsonb column into an object if this doesn't work for you:

@Field()
@Column({ type: "jsonb" })
address: Address;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

avkonst picture avkonst  路  3Comments

tongtwist picture tongtwist  路  3Comments

Janushan picture Janushan  路  3Comments

MichalLytek picture MichalLytek  路  4Comments

limenutt picture limenutt  路  3Comments