Postgraphile: How to update column value with mutation to NULL

Created on 7 Sep 2016  Ā·  13Comments  Ā·  Source: graphile/postgraphile

Hi @calebmer. I'm challenging problem. How I can set value to NULL?

image

šŸ› bug

Most helpful comment

This definitely looks like a bug. I'll investigate šŸ‘

All 13 comments

One possible solution is to add boolean fields into Input shape for each nullable column,
like newImageFeatured for setting value
and imageFeaturedSetNull or nullImageFeatured for database NULL.

This definitely looks like a bug. I'll investigate šŸ‘

@calebmer Can you point out some relevant pieces of code for me? I can prepare PR for this, but I feel there will be more discussion about this... So, any ideas? I really want this feature :smiling_imp:

For sure :blush:

This statement will be what you want to look at. Also, here where setClauses gets defined.

Also a quick crash-course on SQLBuilder (it was a quick and dirty solution, there’s a much cleaner SQL builder in the next branch :wink:). Counting up (think $1, $2, $3, etc.) like in normal prepared statements is hard to keep track of and very difficult to compose. So SQLBuilder allows you to just use $ and pass the array of ordered values like you normally would. Then SQLBuilder will compose those $ to have the correct index.

The bug may be here or it may be as easy a fix as changing this to:

if (value === null) {
  setClauses.push(`"${column.name}" = null`)
}
else {
  setClauses.push(`"${column.name}" = $`)
  setValues.push(value)
}

(pg may not like nulls :blush:)

Let me know how it goes :+1: :tada:

Thanks! Will look. Btw do you like way I'm using template strings in my pg-async library?

It depends, how much do you support? :wink: I didn’t see any documentation when I looked quickly.

I really don’t like SQLBuilder (it was the best I could do at the time…), so I’m going to talk more about the PostGraphQL 2 SQL builder.

Some requirements for a SQL builder used by PostGraphQL is:

  • Supports identifier variables. So something like "forum_example"."person"."id" can be added as so sql.query${sql.identifier('forum_example', 'person', 'id')} = 1``.
  • Allows for both eager and lazy values. Therefore you could do column = ${sql.value(1)} or column = ${sql.placeholder('num')} and then inject a value for num later. In both cases though we still need to use PostgreSQL placeholders with auto-incrementing ā€œnamesā€ ($1, $2, $3, etc).
  • Must be type safe. This is more a design constraint of PostGraphQL 2 though.

The implementation I landed on is also pretty fast. At the end of the days it’s just some string concatenation. Under the hood everything is an object though, it takes the strings and turns it into an array, so a query like:

sql.query`where ${sql.identifier('a', 'b', 'c')} = ${sql.value(value)}`

Turns into:

[{ type: 'RAW', text: 'where ' }, { type: 'IDENTIFIER', names: ['a', 'b', 'c'] }, { type: 'RAW', text: ' = ' }, { type: 'VALUE', value: value }]

I really like the API/implementation I ended up with, but I’m always open to new ideas :blush:

After some more research, I remembered that there is no idea of ā€œnullā€ for inputs in GraphQL. See this issue https://github.com/graphql/graphql-js/issues/133 and this RFC pull request https://github.com/facebook/graphql/pull/83

For now if you want to continue with your proposal (imageFeaturedSetNull etc) go for it, just know it will probably be deprecated when we get support for the null literal in GraphQL :+1:

Another option is we could add a removedColumns array, but I like the idea of imageFeaturedSetNull better more personally.

Hi. I just finished new PR https://github.com/graphql/graphql-js/pull/544 extending GraphQL with null literal in GraphQL language itself so RFC will be fulfilled soon I hope :-)

But it looks that null values are supported already in graphql-js@master.

What is state of this issue? Are explicit nulls still unhandled?

I’ve been watching the progress, great job :blush:

I’ll have to check and see if null values are supported, but I’ll definitely make sure everything works as expected when https://github.com/graphql/graphql-js/pull/544 lands :+1:

:tada: now just waiting for it to get released in 0.8.0 then I’ll test to make sure everything works as expected.

Also see https://github.com/calebmer/postgraphql/issues/202

@calebmer what is the status of this?

@ferdinandsalis I can confirm this works, just make sure you have a version of GraphQL above 0.8.0 :+1:

Submit a PR to add tests and prevent regression, once that gets merged we can close this issue: https://github.com/calebmer/postgraphql/pull/255

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CarlFMateus picture CarlFMateus  Ā·  4Comments

k-ogawa-1988 picture k-ogawa-1988  Ā·  3Comments

angelosarto picture angelosarto  Ā·  3Comments

srghma picture srghma  Ā·  3Comments

outsidenote picture outsidenote  Ā·  4Comments