Is it possible to overwrite scalar types in GraphQL? - graphql

I want an ID scalar to serialize data not as a string but rather as a Mongo Object id. The default behaviour is serializing data as a string. I don't want to create a custom scalar with another name as it may create some difficulties down the road.

The ID type is required to serialize as a string, but absolutely nothing is specified about its format and it's intended to be opaque. You could use a Mongo object ID hex string as a GraphQL ID and (at a GraphQL level) there wouldn't be any problems with this.

Related

Mongo ObjectId backward conversion from JSON to POJO

in my database I have user with ObjectId("5f78cd195a52a201fb117175").
Then I send it by Spring REST Controller to Angular Frontend and there my object id looks like this:
{date: 1601752345000, timestamp: 1601752345}
Afterwards in frontend I create product object, which contains field userId whose value is set to {date: 1601752345000, timestamp: 1601752345} . That object is sent to backend and later on is saved in db. The problem is that when it is converted by Jackson in Rest controller the userId field has value ObjectId("5f78cd19065ece5ade441e7a").
So from user with ObjectId("5f78cd195a52a201fb117175") I receive ObjectId("5f78cd19065ece5ade441e7a")
I dont have user with this second ObjectId so the field with userId contains no realtion to real user.
DO you know why it happens and how to deal with it ?
Jackson treats all objects like POJO types. By default, serialises all getter-s. Let's assume, that ObjectId instance in Java is represented by org.bson.types.ObjectId which has two getter-s:
getDate in your case date field in JSON
getTimestamp in your case timestamp field in JSON
These two fields will be serialised to JSON payload. Deserialisation process needs constructor and setters. I guess, in this case constructor ObjectId​(int timestamp, int counter) will be chosen (but I am not sure). You can try to create new instance of ObjectId using this constructor with values from your JSON payload.
When you use Jackson to serialise/deserialise types from 3-rd party libraries you can expect that there is a module implemented for this. Take a look on MongoJack:
Since MongoDB uses BSON, a binary form of JSON, to store its
documents, a JSON mapper is a perfect mechanism for mapping Java
objects to MongoDB documents. And the best Java JSON mapper is
Jackson. ...
You need to register this module and serialisation/deserialisation processes should work as expected.
See also:
How do you extract a timestamp from a MongoDB ObjectId in Spring Data MongoDB?
Binary JSON with bson4jackson

When to use request in Laravel?

I am reading an article casting created_at to a string as it will return as an object by default. When to make request exactly? I know that the request can be used to transform one format to another. But when exactly? Any rules like when you create a data column you must use it? Things like that?
Using created_at as a string is not a best practice. Laravel Eloquent will automatically extract them from DB and cast them to Carbon instance, so you can transform it with a string (with ->format()) whenever you want.
Plus, having a Carbon object, you have access to lots of methods to handle your date, like comparison, extraction of single data (like day, month, hours...), so it doesn't make sense to cast it to a string, imho.

What is the point of GraphQL's ID Scalar?

https://graphql.org/learn/schema/#scalar-types
I have read the description
ID: The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be human‐readable.
But in practice, what actually changes if I use ID instead of String ?
Nothing changes as it is mentioned in the description. ID is serialized exactly the same as String type; therefore, if you were to replace ID with String, computer would see no difference in it.
However, what is important to you is that YOU as a programmer know for sure that it's a unique identifier which will be useful when you paginate records, modify apollo cache, iteratively create jsx components.
So in practice, it has a very practical implication - it conveys important meaning for the programmer that the field is unique for the type.

How to validate data against a GraqhQL type

Having a GraphQL schema and some JSON object, is there a way to check if the object is a valid type in the schema. The only thing that I've found so far is isValidJSValue which only expects GraphQLInputType.

Getting started with Bleve using BoltDB

I am trying to wrap my head around Bleve and I understand everything that is going on in the tutorials, videos and documentation. I however get very confused when I am using it on BoltDB and don't know how to start.
Say I have an existing BoltDB database called data.db populated with values of struct type Person
type Person struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
Sex string `json:"sex"`
}
How do I index this data so that I can do a search? How do I handle the indexing of data that will be stored in the database in the future?
Any help will be highly appreciated.
Bleve uses BoltDB as one of several backend stores and is separate from where you store your application data. To index your data in Bleve, simply add your Index:
index.Index(person.ID, person)
That index exists separately from your application data (whether it's in Bolt, Postgres, etc).
To retrieve your data, you'll need to construct a search request using bleve.NewSearchRequest(), then call Index.Search(). This will return a SearchResult which includes a Hits field where you can retrieve the ID for your object. You can use this to look up the object in your application data store.
Disclaimer: I am the author of BoltDB.
How you index your data depends on how you want to query for it.
If you want to query by any arbitrary fields, like {Age:15, Name:"Bob"} then BoltDB isn't an awesome fit for your problem.
BoltDB is simply a key value store with fast access to sequential keys and efficient prefix seeking. It's not really a replacement for general use databases.
You likely want something more like a document store (ie: MongoDB) or RDBMS (ie: PostgreSQL).
If you just wanted something that uses simple files and is embedded, you could also use SQlite with the Go module
If you want to search by only a single field, like ID or Name, then use that as the key.
If lookup speed doesn't matter at all, I guess you can use Bolt to just iterate over the entire db, parse the json and check the fields. But that's probably the worst approach you could take.

Resources