starter icon indicating copy to clipboard operation
starter copied to clipboard

Example: how to create dedicated email and URL types

Open singingwolfboy opened this issue 5 years ago • 3 comments
trafficstars

This pull request uses PostgreSQL's domain types feature to define two new types directly in the database: email and URL. It also modifies the existing tables to use these types where appropriate, and creates two new graphile-engine plugins to expose these data types in the GraphQL schema.

The benefit of this change is that GraphQL clients are informed that these values in the schema won't accept just any string -- it must be properly formatted, whether it's an email or a URL. This was already enforced in the database, but because it wasn't exposed in the GraphQL type system, clients may not recognize this requirement until they run into a validation error.

If this pull request is accepted, then we can also use the URL type in #108.

singingwolfboy avatar Feb 13 '20 17:02 singingwolfboy

I think I'd like to see this as a separate plugin, potentially a built-in option to PostGraphile, rather than adding a significant amount of code to starter to handle it. Ideally just issuing

create domain app_public.email as citext check(VALUE ~ '[^@]+@[^@]+\.[^@]+');
comment on domain app_public.email is
  'An address in the electronic mail system. Email addresses such as `[email protected]` are made up of a local-part, followed by an `@` symbol, followed by a domain.';

should result in the GraphQL schema containing

"""
An address in the electronic mail system. Email addresses such as 
`[email protected]` are made up of a local-part, followed by
an `@` symbol, followed by a domain.
"""
scalar Email

benjie avatar Feb 26 '20 15:02 benjie

That makes sense to me. Are you OK with leaving this pull request open until that feature exists in PostGraphile? That way, other people using PostGraphile are more likely to find this code and use it if they need it.

singingwolfboy avatar Feb 26 '20 15:02 singingwolfboy

Absolutely; I plan to leave it open :+1:

benjie avatar Feb 26 '20 16:02 benjie