Hanums

Hanums

GraphQL JSON data type

I have json type field on Postgres and trying to create object type in GraphQL but compiler issuing error:

== Compilation error in file lib/community_web/schema.ex ==
** (Absinthe.Schema.Error) Invalid schema:
/Users/kishore/CodeLearning/Elixir/community/lib/community_web/schema.ex:21: Values :json is not defined in your schema.

Types must exist if referenced.

lib/absinthe/schema.ex:271: Absinthe.Schema.__after_compile__/2
(stdlib) lists.erl:1263: :lists.foldl/3
(stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:206: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6

Here is object definition:
object :field_values do
field :id, non_null(:id)
field :name, :string
field :description, :string
field :values, :json
end

Do I need to create custom object type? but as per the documentation http://graphql-elixir.org/docs/basic-types/ :json is data type.

Please help.

Marked As Solved

axelson

axelson

Scenic Core Team

Basic Types - GraphQL Elixir is the documentation for graphql | Hex but it looks like you are using absinthe | Hex

In Absinthe :json is not built-in and you need to add it to your project manually. Generally speaking you want to be cautious on over-relying on untyped JSON in GraphQL, but a JSON field in Postgres sounds like a good use case to me :+1:

Here are the instructions for adding a custom JSON type in Absinthe:

Also Liked

sorentwo

sorentwo

Oban Core Team

The json db type may have an array or a map with unstructured keys. Unstructured data is strictly forbidden in GraphQL, so you’ll need to work around it. You have two primary options:

  1. If you are storing semi-structures data in the field (like a list of strings, of a map with a few possible keys), then make an object with those fields.
  2. If you have arbitrary data in there then you can send it as a string and have the client decode it.

In our primary GraphQL API we use both of these techniques.

One caveat, if you do go with option 2: enforce the size of the arbitrary data that can be stored. For example, if you have a meta field with client provided values you should restrict the serialized JSON to a fixed length. We do this and limit data to 1024 characters.

gregvaughn

gregvaughn

I don’t know GraphQL well enough to try to help with the immediate problem, but there is a deeper conceptual problem going on here. JSON is not a data type. It is an encoding format. Even though that is the “field type” in the database, you conceptually have some sort of business domain type you are storing that way. That business domain type is what you should expose via GraphQL.

Exposing your field as a “json type” is basically working against the GraphQL goal of specifying your types explicitly. If you are not very careful, this will become a maintenance problem over time.

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement