Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tags): allow smart tags to indicate a column has a default #760

Merged
merged 1 commit into from Oct 21, 2021

Conversation

benjie
Copy link
Member

@benjie benjie commented Oct 21, 2021

Description

PostGraphile automatically determines when a table column has a default, and thus makes that field nullable on the input to a create mutation. However, when we are looking at a different source (for example a view) we cannot determine whether the column has a default or not. If you use the smart tag @primaryKey then we'll know that that column is not-null, but it does not indicate that it has a default, and so we think it's required. Often this is not the case (many primary keys have defaults), so in this PR we introduce the @hasDefault smart tag that you can use to indicate that a column (e.g. your @primaryKey column) has a default and thus should be exposed as nullable.

Fixes graphile/crystal#1544

Performance impact

Negligible.

Security impact

None known.

@bfelbo
Copy link

bfelbo commented Nov 19, 2021

@benjie Thanks a lot for adding this 🙌

This doesn't seem to work though if the column marked with @hasDefault also has an @omit create.

For instance, we have a NOT NULL ranking_score column that should be set by a PostGraphile plugin (never provided by a user, thereby the @omit create). However, marking the column @hasDefault + @omit create and setting the value using a plugin as shown below still throws null value in column "ranking_score" violates not-null constraint.

import { makeWrapResolversPlugin } from "graphile-utils"

export default makeWrapResolversPlugin({
  Mutation: {
    createGameComment: async(resolver, source, args, context, info) => {
      args.input.gameComment.rankingScore = 1.0
      const result = await resolver(source, args, context, info)
      return result
    }
  }
})

EDIT: Clarified the importance of @omit create.

@bfelbo
Copy link

bfelbo commented Nov 19, 2021

The ideal solution would be that adding @hasDefault not only makes the column nullable, but also allows a plugin to insert a value for that column despite of @omit create.

I can create a new issue for this if you'd prefer.

@benjie
Copy link
Member Author

benjie commented Nov 20, 2021

Please do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

id is non-nullable in mutation input in view using INSTEAD OF INSERT
2 participants