Socket
Socket
Sign inDemoInstall

graphql-codegen-persisted-query-ids

Package Overview
Dependencies
165
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.3 to 0.0.4

__tests__/__snapshots__/codegen.test.ts.snap

2

lib/apollo.d.ts

@@ -1,3 +0,3 @@

export declare function usePregeneratedHashed(hashes: {
export declare function usePregeneratedHashes(hashes: {
[operationsName: string]: string | undefined;
}): (doc: import("graphql").DocumentNode) => string;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function usePregeneratedHashed(hashes) {
function usePregeneratedHashes(hashes) {
return function (doc) {

@@ -22,3 +22,3 @@ var operationDefinition = doc.definitions.find(function (def) { return def.kind === "OperationDefinition"; });

}
exports.usePregeneratedHashed = usePregeneratedHashed;
exports.usePregeneratedHashes = usePregeneratedHashes;
//# sourceMappingURL=apollo.js.map

@@ -0,1 +1,2 @@

import { DocumentNode } from "graphql";
import { PluginFunction } from "graphql-codegen-core";

@@ -6,2 +7,5 @@ export interface PluginConfig {

}
export declare function generateQueryIds(docs: DocumentNode[], config: PluginConfig): {
[key: string]: string;
};
export declare const plugin: PluginFunction<PluginConfig>;

@@ -19,9 +19,12 @@ "use strict";

var graphql_1 = require("graphql");
function createHash(def) {
function createHash(s) {
return crypto_1.default
.createHash("sha256")
.update(graphql_1.print(def), "utf8")
.update(s, "utf8")
.digest()
.toString("hex");
}
function printDefinitions(definitions) {
return definitions.map(graphql_1.print).join("\n");
}
var TYPENAME_FIELD = {

@@ -66,18 +69,26 @@ kind: "Field",

}
exports.plugin = function (schema, documents, config) {
if (config.output !== "client" && config.output !== "server") {
throw new Error("graphql-codegen-persisted-query-id must configure output to 'server' or 'client'");
function findUsedFragments(operation) {
var fragmentNames = [];
for (var _i = 0, _a = operation.selectionSet.selections; _i < _a.length; _i++) {
var selection = _a[_i];
if (selection.kind == "FragmentSpread") {
if (!fragmentNames.includes(selection.name.value)) {
fragmentNames.push(selection.name.value);
}
}
}
var clientOutput = {};
var serverOutput = {};
for (var _i = 0, documents_1 = documents; _i < documents_1.length; _i++) {
var doc = documents_1[_i];
var fragments = "";
var content = config.addTypeName
? addTypenameToDocument(doc.content)
: doc.content;
for (var _a = 0, _b = content.definitions; _a < _b.length; _a++) {
return fragmentNames;
}
function generateQueryIds(docs, config) {
var out = {};
for (var _i = 0, docs_1 = docs; _i < docs_1.length; _i++) {
var doc = docs_1[_i];
if (config.addTypeName) {
doc = addTypenameToDocument(doc);
}
var fragments = [];
for (var _a = 0, _b = doc.definitions; _a < _b.length; _a++) {
var def = _b[_a];
if (def.kind === "FragmentDefinition") {
fragments += graphql_1.print(def) + "\n";
fragments.push(def);
}

@@ -88,11 +99,32 @@ if (def.kind === "OperationDefinition") {

}
var hash = createHash(def);
clientOutput[def.name.value] = hash;
serverOutput[hash] = fragments + graphql_1.print(def);
var usedFragments = findUsedFragments(def);
var definitions = [];
for (var _c = 0, fragments_1 = fragments; _c < fragments_1.length; _c++) {
var fragment = fragments_1[_c];
if (usedFragments.includes(fragment.name.value)) {
definitions.push(fragment);
}
}
definitions.push(def);
var query = printDefinitions(definitions);
var hash = createHash(query);
if (config.output === "client") {
out[def.name.value] = hash;
}
else {
out[hash] = query;
}
}
}
}
var out = config.output === "server" ? serverOutput : clientOutput;
return out;
}
exports.generateQueryIds = generateQueryIds;
exports.plugin = function (_schema, documents, config) {
if (config.output !== "client" && config.output !== "server") {
throw new Error("graphql-codegen-persisted-query-id must configure output to 'server' or 'client'");
}
var out = generateQueryIds(documents.map(function (doc) { return doc.content; }), config);
return JSON.stringify(out, null, " ");
};
//# sourceMappingURL=index.js.map
{
"name": "graphql-codegen-persisted-query-ids",
"version": "0.0.3",
"version": "0.0.4",
"description": "Generate persisted query ids",

@@ -11,3 +11,3 @@ "main": "lib/index.js",

"test": "echo todo",
"build": "tsc --project tsconfig.build.json && rm -rf lib && mv build lib && rm -rf build",
"build": "tsc --project tsconfig.build.json && rm -rf lib && mv build/src lib && rm -rf build",
"clean": "rm -rf lib build",

@@ -19,4 +19,7 @@ "dtslint": "tslint --project tsconfig.dtslint.json",

"@types/graphql": "^14.0.7",
"@types/jest": "^24.0.9",
"@types/node": "^11.9.5",
"jest": "^24.1.0",
"prettier": "^1.16.4",
"ts-jest": "^24.0.0",
"typescript": "^3.3.3333"

@@ -23,0 +26,0 @@ },

@@ -25,4 +25,4 @@ # Generate Persisted Query IDs

- graphql-codegen-persisted-query-ids:
addTypeName: true # required if using apollo-client
output: server
addTypeName: true # required when using apollo-client
```

@@ -34,1 +34,52 @@

./node_modules/.bin/gql-gen --overwrite
Commit these files to git.
### Integrating with WPGraphQL
Add my fork of the wp-graphql-persisted-queries plugin
cd wp-content/plugins
git clone https://github.com/epeli/wp-graphql-persisted-queries
> The fork is required for now but I've sent the changes as PRs to the upstream
In your theme's `functions.php` add
```php
add_filter( 'graphql_persisted_queries_load_query', function( string $query_id ) {
$queries = json_decode( file_get_contents( __DIR__ . '/persisted-query-ids/server.json' ), true );
return $queries[ $query_id ] ?? null;
}, 10, 1 );
```
You can progmatically manage the lock mode with the option filter
```php
add_filter( 'option_graphql_persisted_queries_is_locked', function() {
return defined( 'WP_ENV' ) && 'production' === WP_ENV;
}, 10 , 1 );
```
### Integrating with Apollo Client
Add custom `generateHash` to [apollo-link-persisted-queries](https://github.com/apollographql/apollo-link-persisted-queries)
```ts
import { createPersistedQueryLink } from "apollo-link-persisted-queries";
import { usePregeneratedHashes } from "graphql-codegen-persisted-query-ids/lib/apollo";
const hashes = require("../persisted-query-ids/client");
const persistedLink = createPersistedQueryLink({
useGETForHashedQueries: true, // Optional but allows better caching
generateHash: usePregeneratedHashes(hashes),
});
// And pass it to ApolloClient
const client = new ApolloClient({
link: persistedLink.concat(createHttpLink({ uri: "/graphql" })),
cache: new InMemoryCache(),
});
```

@@ -1,2 +0,2 @@

export function usePregeneratedHashed(hashes: {
export function usePregeneratedHashes(hashes: {
[operationsName: string]: string | undefined;

@@ -3,0 +3,0 @@ }) {

@@ -8,9 +8,12 @@ import crypto from "crypto";

FieldNode,
FragmentDefinitionNode,
} from "graphql";
import { PluginFunction } from "graphql-codegen-core";
function createHash(def: any) {
type Definition = FragmentDefinitionNode | OperationDefinitionNode;
function createHash(s: string) {
return crypto
.createHash("sha256")
.update(print(def), "utf8")
.update(s, "utf8")
.digest()

@@ -20,2 +23,6 @@ .toString("hex");

function printDefinitions(definitions: Definition[]) {
return definitions.map(print).join("\n");
}
const TYPENAME_FIELD: FieldNode = {

@@ -80,26 +87,29 @@ kind: "Field",

export const plugin: PluginFunction<PluginConfig> = (
schema,
documents,
config,
) => {
if (config.output !== "client" && config.output !== "server") {
throw new Error(
"graphql-codegen-persisted-query-id must configure output to 'server' or 'client'",
);
function findUsedFragments(operation: OperationDefinitionNode) {
const fragmentNames: string[] = [];
for (const selection of operation.selectionSet.selections) {
if (selection.kind == "FragmentSpread") {
if (!fragmentNames.includes(selection.name.value)) {
fragmentNames.push(selection.name.value);
}
}
}
const clientOutput: { [operationName: string]: string } = {};
const serverOutput: { [hash: string]: string } = {};
return fragmentNames;
}
for (const doc of documents) {
let fragments = "";
export function generateQueryIds(docs: DocumentNode[], config: PluginConfig) {
const out: { [key: string]: string } = {};
const content = config.addTypeName
? addTypenameToDocument(doc.content)
: doc.content;
for (let doc of docs) {
if (config.addTypeName) {
doc = addTypenameToDocument(doc);
}
for (const def of content.definitions) {
let fragments: FragmentDefinitionNode[] = [];
for (const def of doc.definitions) {
if (def.kind === "FragmentDefinition") {
fragments += print(def) + "\n";
fragments.push(def);
}

@@ -112,5 +122,22 @@

const hash = createHash(def);
clientOutput[def.name.value] = hash;
serverOutput[hash] = fragments + print(def);
const usedFragments = findUsedFragments(def);
const definitions: Definition[] = [];
for (const fragment of fragments) {
if (usedFragments.includes(fragment.name.value)) {
definitions.push(fragment);
}
}
definitions.push(def);
const query = printDefinitions(definitions);
const hash = createHash(query);
if (config.output === "client") {
out[def.name.value] = hash;
} else {
out[hash] = query;
}
}

@@ -120,4 +147,19 @@ }

const out = config.output === "server" ? serverOutput : clientOutput;
return out;
}
export const plugin: PluginFunction<PluginConfig> = (
_schema,
documents,
config,
) => {
if (config.output !== "client" && config.output !== "server") {
throw new Error(
"graphql-codegen-persisted-query-id must configure output to 'server' or 'client'",
);
}
const out = generateQueryIds(documents.map(doc => doc.content), config);
return JSON.stringify(out, null, " ");
};

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc