graphile / persisted-operations Goto Github PK
View Code? Open in Web Editor NEWPersisted operations (aka "persisted queries") support for PostGraphile
License: MIT License
Persisted operations (aka "persisted queries") support for PostGraphile
License: MIT License
See title.
!options.persistedOperationsGetter && !options.persistedOperations && !options.persistedOperationsDirectory
This clause is triggered.
null assignment
with throw new Error('No getter function was specified')
.I'm not sure yet how much of this involves writing a tool vs. just documenting a pattern or what, and I'm not sure to what extent it relates to this repo vs graphile-engine etc.
It would be awesome to write tests that, for each persisted operation, execute the query with EXPLAIN against the server while it points to a DB that represents production (perhaps actually being production) with representative variables.
Something maybe very loosely like this:
// in your app
const myQuery = gql`
query myQuery($condition: String)
@explain(variables: {condition: "foo"}, maxcost: 1000)
@explain(variables: {condition: "bar"}, maxcost: 5000)
{
getUsers(condition: $condition) {
nodes {
username
}
}
}
`
This would generate several files:
.persisted-operations/client.json
:
{
"myQuery": "xyzsha",
}
.persisted-operations/xyzsha.graphql
:
query myQuery($condition: String) {
...
}
.persisted-operations/xyzsha-myQuery-foo.sql
:
-- query: ./xyzsha.graphql (make it easy to cmd+click to the gql in editors like vs code)
-- variable: condition=foo
select username from users where thing = 'foo';
/*
EXPLAIN results here, showing the cost and query plan
*/
.persisted-operations/xyzsha-myQuery-bar.sql
:
-- query: ./xyzsha.graphql
-- variable: condition=bar
select username from users where thing = 'bar';
/*
EXPLAIN results here, showing the cost and query plan
*/
This way, developers can easily see the sql generated from the queries they write, as well as the query plan (imagine actually seeing where a Seq Scan is used!). Reviewers can audit it. And the build can fail if EXPLAIN thinks the cost of a query will be higher than the specified maxcost
.
Maybe this could even be part of graphile pro.
Problems / open questions:
@name
for each persisted operation instead of using a sha? Maybe what I have here would be fine-enough, and maybe even just using sha's would be fine.maxcost
annotation would be silently exceeded for a time.Add a function, like allowExplain
, that governs whether a request may use arbitrary operations versus only persisted operations.
In development it's common to want to send arbitrary queries from GraphiQL whilst also enforcing Persisted Operations from the application.
app.use(postgraphile(DATABASE_URL, SCHEMAS, {
allowUnpersistedOperation(req) {
return process.env.NODE_ENV === "development" && req.headers.referer.endsWith("/graphiql");
}
});
(Should we pass the operation too, I wonder?)
None
Heya,
I'm considering postgraphile for a project, and one of my core anxieties about the tool (and graphql in general) is being able to detect and diagnose huge, expensive queries. Persisted operations seems like one helpful way to at least lock that down to known-at-build-time queries, but the next step would be to actually understand the performance of these queries.
One approach might be prepared statements – saving each persisted operation as its sha. This would reduce query parsing and planning time, which I assume isn't much.
My thinking is that this might also help make it easier to see what queries are running frequently and expensively, debug their query plans, etc – but this may be totally off-base.
As an aside, it might be worth adding a section to Production Considerations talking about how to understand your query performance.
Cheers!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.