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

use table enums in functions via domain in V5 #2214

Open
4 of 6 tasks
tsnobip opened this issue Oct 17, 2024 · 2 comments
Open
4 of 6 tasks

use table enums in functions via domain in V5 #2214

tsnobip opened this issue Oct 17, 2024 · 2 comments

Comments

@tsnobip
Copy link

tsnobip commented Oct 17, 2024

Feature description

V4 has the feature to use table enums in functions via domain, I think we should port it to V5 too. I'm just not sure how to do it in V5 (possibly via build.setGraphQLTypeForPgCodec).

Motivating example

Same motivation as for the feature in V4, allowing to have nice GraphQL enums in function parameter / return types.

Breaking changes

not breaking as far as I know

Supporting development

I [tick all that apply]:

  • am interested in building this feature myself
  • am interested in collaborating on building this feature
  • am willing to help testing this feature before it's released
  • am willing to write a test-driven test suite for this feature (before it exists)
  • am a Graphile sponsor ❤️
  • have an active support or consultancy contract with Graphile
@github-project-automation github-project-automation bot moved this to 🌳 Triage in V5.0.0 Oct 17, 2024
@tsnobip
Copy link
Author

tsnobip commented Oct 17, 2024

I made some experiments, something like this almost works:

{
      name: 'EnumDomainGraphQLTypePlugin',
      version: '0.0.0',
      schema: {
        hooks: {
          finalize(_, build) {
            for (const [key, codec] of Object.entries(build.input.pgRegistry.pgCodecs)) {
              if (codec.domainOfCodec?.name == 'text') {
                const name = build.inflection.builtin(codec.name.charAt(0).toUpperCase() + codec.name.slice(1, -4));
                build.setGraphQLTypeForPgCodec(codec, "output", name);
                build.setGraphQLTypeForPgCodec(codec, "input", name);
              }
            };
            return _;
          },
        },
      },
    }

The issue is that the domain is already registered as a scalar so it fails and I don't know how to deregister a scalar (or prevent it to get registered).

@benjie
Copy link
Member

benjie commented Oct 17, 2024

Indeed; this relates to:

You shouldn't be using the finalize hook for this, it's way too late - that's the final step when the schema is built. Instead use the init hook, and have it run before PgCodecsPlugin:

schema: {
  hooks: {
    init: {
      before: ['PgCodecsPlugin'],
      callback(_, build) { ... }
    }
  }
}

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

No branches or pull requests

3 participants
@benjie @tsnobip and others