question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type safety with custom resolvers and `selectGraphQLResultFromTable`

See original GitHub issue

Summary

Is it possible to have type-safe queries in makeExtendSchemaPlugin resolvers, so that the resolvers break at build time if incompatible database schema changes are made?

Details

I’ve been following the makeExtendSchemaPlugin docs and have written some custom resolvers.

I’ve converted my resolvers to TypeScript and have used Zapatos to generate a TypeScript schema from the DB. Now I get squiggly lines in my IDE and compile errors if my queries don’t match the database schema (see images below)

This is perfect, but I don’t see a way to get the same type-safety when using the selectGraphQLResultFromTable query builder. This means the resolvers may still compile if I change the schema, but fail at runtime.

I’ve tried building the queries using Zapatos and using a hacked version of the resulting SQL in the query builder using sql.raw(), but this seems like a bad idea.

Is there some way to get type safety when using selectGraphQLResultFromTable? If not, could I start a feature-request discussion to have Postgraphile generate a TypeScript schema to be used with pgClient and selectGraphQLResultFromTable?

Additional context

Squigglies in VS Code

image

TypeScript compile errors

image

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
benjiecommented, Aug 5, 2022

pg-sql2, the library we use for templating SQL, can also be used with pgClient - please refer to its readme 👍

I’ve used Zapatos within PostGraphile projects 👍

1reaction
benjiecommented, Aug 3, 2022

selectGraphQLResultFromTable is going away in PostGraphile V5; it was only ever intended to be a wrapper method to stop people having to write so much boilerplate. I’d love your input on the new stuff once it’s ready to be shared publicly, so I’ll leave this open.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · graphile/postgraphile - GitHub
Type safety with custom resolvers and selectGraphQLResultFromTable revisit-in-v5 Not happy with this behaviour, but changing it is a breaking change.
Read more >
Better Type Safety for your GraphQL resolvers with GraphQL ...
This plugin allows you to easily have typings for your resolvers, with super flexible configuration that allow you to integrate it easily to ......
Read more >
Generated GraphQL: Part 2 - Brandon Konkle
I like to define an AppRequest type alias that passes my custom Context ... effective strategy for easy type-safety and useful autocomplete ...
Read more >
PostGraphile | makeExtendSchemaPlugin (graphile-utils)
Using makeExtendSchemaPlugin you can write a plugin that will merge additional GraphQL types and resolvers into your schema using a similar ...
Read more >
Writing query resolvers | Full-Stack Quickstart - Apollo GraphQL
Data of the type required by the resolver's corresponding schema field ... By keeping resolvers thin as a best practice, you can safely...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found