Fork me on GitHub
#graphql
<
2020-10-09
>
aeskilson20:10:20

Do any folks here have some tips on the best location to handle GraphQLs expectations of ID types and the ways they may differ from Integer-type expectations from relational databases for example?

Lennart Buit20:10:43

We have defined a custom parser for the :ID scalar that enforces our ‘format’ for node ids

Lennart Buit20:10:01

It throws a coercion failure if its expectations are not met

Lennart Buit20:10:24

(Oh, I am assuming lacinia here, you were not so specific)

aeskilson20:10:30

Yep, I'm using Lacinia.

Lennart Buit20:10:28

Yeah, so we just defined {`{:scalars {:ID {:parse :parse/id :serialize :serialize/id}}}` in our schema, and the rest of our application only deals with parsed ids 🙂

Lennart Buit20:10:59

Does that help 🙂?

aeskilson20:10:09

That makes sense! That was the direction I was headed.

aeskilson20:10:48

Thanks for the quick reply!

Lennart Buit20:10:18

Take a look at the object identification relay spec, if you haven’t already encountered that 🙂

Lennart Buit20:10:10

Bit seperate tho, but is good to consider when designing the ‘internal’ structure of your ids

aeskilson20:10:09

The longer form is that my database tables use integers for their IDs, but GraphQL certainly wants its ID types to be serialized as Strings.

hlship21:10:41

If you want to use GraphQL’s ID type. The reasoning is that database ids are often 64 bit integers, which are wider than what JSON supports for numbers, so a (implicitly numeric) string is a lossless representation.

hlship21:10:21

Or, you could use a UUID type (you’d have to define that as a scalar, but that’s not too hard).

hlship21:10:05

I’m pretty sure Postgres has a UUID type suitable for a primary key, and can generate that UUID on insert using a database function. I’m a bit rusty on the Postgres front, however.

hlship21:10:26

It’s good to eat your own dog-food. I’ve discovered that in directive arguments (and perhaps field arguments) the SDL parser can’t parse a default value that’s boolean. Very strange.

hlship21:10:06

true/false/null seem to be the problem

hlship23:10:23

Anyone know Anltr4 better than me? Here’s the problem I’m stuck on: https://stackoverflow.com/questions/64288464/parsing-graphql-schema-fails-unexpectedly

hlship16:10:51

StackOverflow to the rescue! Got a fix working.

Lennart Buit23:10:57

Just throwing stuff at the wall here: Your grammar specifies that directive arguments may have directives, is that something that could be problematic?

Lennart Buit23:10:12

Hmm, nevermind

Lennart Buit23:10:44

Anyhow, since you are saying its true, false and null that are issues. I’m expecting ANTLR to somehow interpret those not as keywords, but as a name

Lennart Buit23:10:12

There are IDE integrations for ANTLR btw, maybe those can shed some light on how antlr is attempting to parse the SDL