Socket
Socket
Sign inDemoInstall

graphql-persisted-document-loader

Package Overview
Dependencies
12
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.0 to 3.0.0

48

index.js
const vm = require('vm');
const os = require('os');
const loaderUtils = require('loader-utils');
const { ExtractGQL } = require('persistgraphql/lib/src/ExtractGQL');
const queryTransformers = require('persistgraphql/lib/src/queryTransformers');
const { print, parse, separateOperations } = require('graphql');
const { addTypenameToDocument } = require('apollo-utilities');

@@ -76,28 +76,24 @@ module.exports = function graphQLPersistedDocumentLoader(content) {

function tryAddDocumentId(options, content, querySource) {
const queryMap = new ExtractGQL({
queryTransformers: [options.addTypename && queryTransformers.addTypenameTransformer].filter(Boolean)
}).createOutputMapFromString(querySource);
// Every file may contain multiple operations
const operations = separateOperations(parse(querySource));
const queries = Object.keys(queryMap);
if (queries.length > 1) {
queries
.map(query => {
const matched = query.match(/^(mutation|query)\ ([^\ \(\{]*)/)
if (!matched) {
return false
}
return {
operationName: matched[2],
id: generateIdForQuery(options, query)
}
})
.filter(isValid => !!isValid)
.forEach(({ id, operationName }) => {
content += `${os.EOL}module.exports["${operationName}"].documentId = ${JSON.stringify(id)};`
})
} else if (queries.length === 1) {
const queryId = generateIdForQuery(options, Object.keys(queryMap)[0]);
content += `${os.EOL}doc.documentId = ${JSON.stringify(queryId)}`;
}
Object.keys(operations).map((operation) => {
const document = options.addTypename
? addTypenameToDocument(operations[operation])
: operations[operation];
const query = print(document);
const queryId = generateIdForQuery(options, query);
// Add them as exports to the final file
// If there is only one operation, it will be the default export
if (Object.keys(operations).length > 1) {
content += `${
os.EOL
}module.exports["${operation}"].documentId = ${JSON.stringify(queryId)};`;
} else {
content += `${os.EOL}doc.documentId = ${JSON.stringify(queryId)}`;
}
});
return content;

@@ -104,0 +100,0 @@ }

{
"name": "graphql-persisted-document-loader",
"version": "2.0.0",
"version": "3.0.0",
"description": "Webpack loader that adds a documentId to a compiled graphql document, which can be used when persisting/retrieving documents",

@@ -10,12 +10,14 @@ "main": "index.js",

"scripts": {
"start": "webpack-dev-server --config ./example/webpack.config.js"
"start": "webpack serve --config ./example/webpack.config.js"
},
"devDependencies": {
"graphql-tag": "^2.9.2",
"webpack": "^3.8.1",
"webpack-dev-server": "^2.9.5"
"webpack": "4.29.6",
"webpack-cli": "^4.2.0",
"webpack-dev-server": "^3.11.0"
},
"dependencies": {
"loader-utils": "^1.1.0",
"persistgraphql": "^0.3.11"
"apollo-utilities": "^1.3.4",
"graphql": "^15.4.0",
"loader-utils": "^1.1.0"
},

@@ -22,0 +24,0 @@ "peerDependencies": {

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