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: enable bypassing persisted operations #4

Merged
merged 18 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ persistedOperations?: { [hash: string]: string };
* stores (e.g. S3).
*/
persistedOperationsGetter?: PersistedOperationGetter;

/**
* There are situations where you may want to allow arbitrary operations
* (for example using GraphiQL in development, or allowing an admin to
* make arbitrary requests in production) whilst enforcing Persisted
* Operations for the application and non-admin users. This function
* allows you to determine under which circumstances persisted operations
* may be bypassed.
benjie marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
*
* ```
* app.use(postgraphile(DATABASE_URL, SCHEMAS, {
* allowUnpersistedOperation(req) {
* return process.env.NODE_ENV === "development" && req.headers.referer?.endsWith("/graphiql");
* }
* });
* ```
*/
allowUnpersistedOperation?(request: IncomingMessage, payload: any): boolean;
```

All these options are optional; but you should specify exactly one of
Expand Down
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@
### v0.0.1

- Initial release

### Pending

- Added `allowUnpersistedOperation` option, allowing arbitrary operations to be issued under controlled circumstances
leo91000 marked this conversation as resolved.
Show resolved Hide resolved
50 changes: 45 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFileSync, promises as fsp } from "fs";
import { PostGraphileOptions, PostGraphilePlugin } from "postgraphile";
import { IncomingMessage } from "http";

/**
* Given a persisted operation hash, return the associated GraphQL operation
Expand Down Expand Up @@ -49,6 +50,29 @@ declare module "postgraphile" {
* stores (e.g. S3).
*/
persistedOperationsGetter?: PersistedOperationGetter;

/**
* There are situations where you may want to allow arbitrary operations
* (for example using GraphiQL in development, or allowing an admin to
* make arbitrary requests in production) whilst enforcing Persisted
* Operations for the application and non-admin users. This function
* allows you to determine under which circumstances persisted operations
* may be bypassed.
benjie marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
*
* ```
* app.use(postgraphile(DATABASE_URL, SCHEMAS, {
* allowUnpersistedOperation(req) {
* return process.env.NODE_ENV === "development" && req.headers.referer.endsWith("/graphiql");
* }
* });
* ```
*/
allowUnpersistedOperation?(
request: IncomingMessage,
payload: any
): boolean;
}
}

Expand Down Expand Up @@ -195,12 +219,17 @@ function getterFromOptions(options: PostGraphileOptions) {
*/
function persistedOperationFromPayload(
payload: any,
options: PostGraphileOptions
options: PostGraphileOptions,
allowUnpersistedOperation: boolean
): string | null {
try {
const hashFromPayload = options.hashFromPayload || defaultHashFromPayload;
const hash = hashFromPayload(payload);
if (typeof hash !== "string") {
if (allowUnpersistedOperation && typeof payload?.query === "string") {
return payload.query;
}

throw new Error(
"We could not find a persisted operation hash string in the request."
);
Expand Down Expand Up @@ -251,20 +280,31 @@ const PersistedQueriesPlugin: PostGraphilePlugin = {
},

// For regular HTTP requests
"postgraphile:httpParamsList"(paramsList, { options }) {
"postgraphile:httpParamsList"(paramsList, { options, req }) {
return paramsList.map((params: any) => {
const allowUnpersistedOperations =
options.allowUnpersistedOperation || (() => false);
leo91000 marked this conversation as resolved.
Show resolved Hide resolved

// ALWAYS OVERWRITE, even if invalid; the error will be thrown elsewhere.
params.query = persistedOperationFromPayload(params, options) as string;
params.query = persistedOperationFromPayload(
params,
options,
allowUnpersistedOperations(req, params)
leo91000 marked this conversation as resolved.
Show resolved Hide resolved
) as string;
return params;
});
},

// For websocket requests
"postgraphile:ws:onOperation"(params, { message, options }) {
"postgraphile:ws:onOperation"(params, { message, options, req }) {
leo91000 marked this conversation as resolved.
Show resolved Hide resolved
const allowUnpersistedOperations =
options.allowUnpersistedOperation || (() => false);

// ALWAYS OVERWRITE, even if invalid; the error will be thrown elsewhere.
params.query = persistedOperationFromPayload(
message.payload,
options
options,
allowUnpersistedOperations(req, message.payload)
) as string;
return params;
},
Expand Down