In this tutorial, we will explore more about using Scalars on Hypi’s low code platform.
So, here is the first question: What are scalars?
Scalar types represent primitive leaf values in a GraphQL type system. GraphQL responses take the form of a hierarchical tree. The leaves of this tree are typically GraphQL Scalar types. (May also be Enum types or null values). The Scalar fields don’t have any sub-fields – they are the leaves of the query.
The second question is, How to use Scalars in a schema design?
We are building a Social Media Platform, and we want to determine the fields of a Post object. Here is our sample Schema.
type Post {
postedby: Account @computed(query: "hypi.id = '${self.hypi.createdBy}'")
date: DateTime
text: String
likecount: Int @computedCount(field: "likedby")
likedby: [Account!]
comments: [Post!]
rating: Float
share: Json
postuuid: UUID
gppost: Boolean
tagFriends: [String]
}
The above Post object has used certain built-in scalar types from GraphQL like String, Int, Float, Boolean, etc. It supports custom scalar types like DateTime, UUID, JSON as well.
One to Many Scalars
Third question is, “Can we store multiple values in a Scalar field?”
Multiple values can be stored in a Scalar Array. In the Post data type, tagFriends represent String Array. In Hypi, tagFriends: [String] is a one-to-many relationship but with scalars. Note that Scalar Array cannot contain duplicates. If you add the same item multiple times, it’ll just be stored/return once.
Scalar Specifications
Fourth question is, “How to use Scalars to store data?“
Here is a quick look into the specifications of Scalars.
Int: signed 32 bit numeric non-fractional value
Float: signed double-precision finite floating-point value
String: UTF-8 character sequence
Boolean: true or false
ID: unique identifier
UUID (universally unique identifier): a scalar serialized as a string conforming to RFC 4122.
JSON (JavaScript Object Notation): lightweight format for storing and transporting data.
DateTime: valid ISO 8601 date time value
Any: any possible value without any specific format. (Use this only when the data value doesn’t belong to any of the above types)
You may further check out these scalar types here.
Let’s check how to insert data into these scalar fields.
mutation {
upsert(
values: {
Post: [
{
hypi:{
id: "01F52TZ03J426MHJYKYRB9Q8PT"
}
date: "2021-05-07"
text: "The future is low code!",
rating: 4.6,
postuuid: "bfcf282c-b7a4-11eb-8529-0242ac130003",
gppost: false,
share:{
gpname: "scalars",
url: "scalars@hypi.in"
},
tagFriends: ["GraphQL","Groovy","Velocity","GraphQL"]
}
]
}
) {
id
}
}
#Result
{
"data": {
"upsert": [
{
"id": "01F52TZ03J426MHJYKYRB9Q8PT"
}
]
}
}
String, ID, DateTime, UUID values have to be specified in double-quotes. Array values are specified in square brackets separated by commas.
You may retrieve the inserted data like this.
{
get(type: Post, id: "01F52TZ03J426MHJYKYRB9Q8PT") {
... on Post {
hypi {
id
created
}
text
likecount
rating
postuuid
gppost
share
tagFriends
}
}
}
#Result
{
"data": {
"get": {
"hypi": {
"id": "01F52TZ03J426MHJYKYRB9Q8PT",
"created": "2021-05-07T07:16:40Z"
},
"text": "The future is low code!",
"likecount": 2,
"rating": 4.6,
"postuuid": "bfcf282c-b7a4-11eb-8529-0242ac130003",
"gppost": false,
"share": {
"gpname": "scalars",
"url": "scalars@hypi.in"
},
"tagFriends": [
"Groovy",
"GraphQL",
"Velocity"
]
}
}
}
Delete Scalars
Fifth and last question is, “How do we delete values in Scalar Array?“
deleteScalars function is used to delete items in a Scalar Array. Scalar Array fields in Hypi are not stored directly with the other scalar fields in an object. You may add a large number of items to a Scalar Array field.
Let’s use deleteScalars to delete items from tagFriends Scalar Array from the Post type. The function returns the number of records affected by the deletion. “Groovy”, “GraphQL” values are deleted. Frame arcql query to select the Post object and get an id.
mutation{
deleteScalars(
type: Post,
field:"tagFriends",
values:["Groovy","GraphQL"],
arcql:"hypi.id ='01F52TZ03J426MHJYKYRB9Q8PT'"
)
}
#Result
{
"data": {
"deleteScalars": 1
}
}
Concluding Note
Now you may use the Scalars on Hypi’s low code platform to make objects with various fields and insert/retrieve the scalars from the mutation/query.
Specify as many Scalar fields as needed to meet the functional requirements of your App. Use them with ease on Hypi’s low code platform!