graphql-query-rewriter
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -245,2 +245,3 @@ import { parse, print, parseType } from 'graphql'; | ||
*/ | ||
/** @hidden */ | ||
var extractPath = function (parents) { | ||
@@ -663,3 +664,47 @@ var path = []; | ||
var fragmentMatchCondition = (function (_a) { | ||
var JsonToTypedObjectRewriter = /** @class */ (function (_super) { | ||
__extends(JsonToTypedObjectRewriter, _super); | ||
function JsonToTypedObjectRewriter(_a) { | ||
var fieldName = _a.fieldName, objectFields = _a.objectFields; | ||
var _this = _super.call(this, { fieldName: fieldName }) || this; | ||
_this.objectFields = objectFields; | ||
return _this; | ||
} | ||
JsonToTypedObjectRewriter.prototype.matches = function (nodeAndVars, parents) { | ||
if (!_super.prototype.matches.call(this, nodeAndVars, parents)) | ||
return false; | ||
var node = nodeAndVars.node; | ||
// make sure there's no subselections on this field | ||
if (node.selectionSet) | ||
return false; | ||
return true; | ||
}; | ||
JsonToTypedObjectRewriter.prototype.rewriteQuery = function (nodeAndVarDefs) { | ||
var node = nodeAndVarDefs.node; | ||
var variableDefinitions = nodeAndVarDefs.variableDefinitions; | ||
// if there's a subselection already, just return | ||
if (node.selectionSet) | ||
return nodeAndVarDefs; | ||
var selectionSet = this.generateSelectionSet(this.objectFields); | ||
return { | ||
variableDefinitions: variableDefinitions, | ||
node: __assign({}, node, { selectionSet: selectionSet }) | ||
}; | ||
}; | ||
JsonToTypedObjectRewriter.prototype.generateSelectionSet = function (fields) { | ||
var _this = this; | ||
return { | ||
kind: 'SelectionSet', | ||
selections: fields.map(function (_a) { | ||
var name = _a.name, subfields = _a.subfields; | ||
return (__assign({ kind: 'Field', name: { kind: 'Name', value: name } }, (subfields && { | ||
selectionSet: _this.generateSelectionSet(subfields) | ||
}))); | ||
}) | ||
}; | ||
}; | ||
return JsonToTypedObjectRewriter; | ||
}(Rewriter)); | ||
var fragmentMatchCondition = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, fragmentNames = _b.fragmentNames, fragmentTypes = _b.fragmentTypes, pathRegexes = _b.pathRegexes; | ||
@@ -688,4 +733,5 @@ return function (_a, parents) { | ||
}; | ||
}); | ||
}; | ||
/** @hidden */ | ||
var operationMatchCondition = (function (_a) { | ||
@@ -719,3 +765,3 @@ var _b = _a === void 0 ? {} : _a, operationNames = _b.operationNames, operationTypes = _b.operationTypes, pathRegexes = _b.pathRegexes; | ||
var queryMatchCondition = (function (_a) { | ||
var queryMatchCondition = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, queryNames = _b.queryNames, pathRegexes = _b.pathRegexes; | ||
@@ -727,5 +773,5 @@ return operationMatchCondition({ | ||
}); | ||
}); | ||
}; | ||
var mutationMatchCondition = (function (_a) { | ||
var mutationMatchCondition = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, mutationNames = _b.mutationNames, pathRegexes = _b.pathRegexes; | ||
@@ -737,5 +783,5 @@ return operationMatchCondition({ | ||
}); | ||
}); | ||
}; | ||
export { RewriteHandler, Rewriter, FieldArgNameRewriter, FieldArgsToInputTypeRewriter, FieldArgTypeRewriter, NestFieldOutputsRewriter, ScalarFieldToObjectFieldRewriter, fragmentMatchCondition, queryMatchCondition, mutationMatchCondition }; | ||
export { RewriteHandler, Rewriter, FieldArgNameRewriter, FieldArgsToInputTypeRewriter, FieldArgTypeRewriter, NestFieldOutputsRewriter, ScalarFieldToObjectFieldRewriter, JsonToTypedObjectRewriter, fragmentMatchCondition, queryMatchCondition, mutationMatchCondition }; | ||
//# sourceMappingURL=index.es5.js.map |
@@ -249,2 +249,3 @@ (function (global, factory) { | ||
*/ | ||
/** @hidden */ | ||
var extractPath = function (parents) { | ||
@@ -667,3 +668,47 @@ var path = []; | ||
var fragmentMatchCondition = (function (_a) { | ||
var JsonToTypedObjectRewriter = /** @class */ (function (_super) { | ||
__extends(JsonToTypedObjectRewriter, _super); | ||
function JsonToTypedObjectRewriter(_a) { | ||
var fieldName = _a.fieldName, objectFields = _a.objectFields; | ||
var _this = _super.call(this, { fieldName: fieldName }) || this; | ||
_this.objectFields = objectFields; | ||
return _this; | ||
} | ||
JsonToTypedObjectRewriter.prototype.matches = function (nodeAndVars, parents) { | ||
if (!_super.prototype.matches.call(this, nodeAndVars, parents)) | ||
return false; | ||
var node = nodeAndVars.node; | ||
// make sure there's no subselections on this field | ||
if (node.selectionSet) | ||
return false; | ||
return true; | ||
}; | ||
JsonToTypedObjectRewriter.prototype.rewriteQuery = function (nodeAndVarDefs) { | ||
var node = nodeAndVarDefs.node; | ||
var variableDefinitions = nodeAndVarDefs.variableDefinitions; | ||
// if there's a subselection already, just return | ||
if (node.selectionSet) | ||
return nodeAndVarDefs; | ||
var selectionSet = this.generateSelectionSet(this.objectFields); | ||
return { | ||
variableDefinitions: variableDefinitions, | ||
node: __assign({}, node, { selectionSet: selectionSet }) | ||
}; | ||
}; | ||
JsonToTypedObjectRewriter.prototype.generateSelectionSet = function (fields) { | ||
var _this = this; | ||
return { | ||
kind: 'SelectionSet', | ||
selections: fields.map(function (_a) { | ||
var name = _a.name, subfields = _a.subfields; | ||
return (__assign({ kind: 'Field', name: { kind: 'Name', value: name } }, (subfields && { | ||
selectionSet: _this.generateSelectionSet(subfields) | ||
}))); | ||
}) | ||
}; | ||
}; | ||
return JsonToTypedObjectRewriter; | ||
}(Rewriter)); | ||
var fragmentMatchCondition = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, fragmentNames = _b.fragmentNames, fragmentTypes = _b.fragmentTypes, pathRegexes = _b.pathRegexes; | ||
@@ -692,4 +737,5 @@ return function (_a, parents) { | ||
}; | ||
}); | ||
}; | ||
/** @hidden */ | ||
var operationMatchCondition = (function (_a) { | ||
@@ -723,3 +769,3 @@ var _b = _a === void 0 ? {} : _a, operationNames = _b.operationNames, operationTypes = _b.operationTypes, pathRegexes = _b.pathRegexes; | ||
var queryMatchCondition = (function (_a) { | ||
var queryMatchCondition = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, queryNames = _b.queryNames, pathRegexes = _b.pathRegexes; | ||
@@ -731,5 +777,5 @@ return operationMatchCondition({ | ||
}); | ||
}); | ||
}; | ||
var mutationMatchCondition = (function (_a) { | ||
var mutationMatchCondition = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, mutationNames = _b.mutationNames, pathRegexes = _b.pathRegexes; | ||
@@ -741,3 +787,3 @@ return operationMatchCondition({ | ||
}); | ||
}); | ||
}; | ||
@@ -751,2 +797,3 @@ exports.RewriteHandler = RewriteHandler; | ||
exports.ScalarFieldToObjectFieldRewriter = ScalarFieldToObjectFieldRewriter; | ||
exports.JsonToTypedObjectRewriter = JsonToTypedObjectRewriter; | ||
exports.fragmentMatchCondition = fragmentMatchCondition; | ||
@@ -753,0 +800,0 @@ exports.queryMatchCondition = queryMatchCondition; |
@@ -209,2 +209,3 @@ "use strict"; | ||
*/ | ||
/** @hidden */ | ||
exports.extractPath = function (parents) { | ||
@@ -211,0 +212,0 @@ var path = []; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var ast_1 = require("../ast"); | ||
exports.default = (function (_a) { | ||
var fragmentMatchCondition = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, fragmentNames = _b.fragmentNames, fragmentTypes = _b.fragmentTypes, pathRegexes = _b.pathRegexes; | ||
@@ -28,3 +28,4 @@ return function (_a, parents) { | ||
}; | ||
}); | ||
}; | ||
exports.default = fragmentMatchCondition; | ||
//# sourceMappingURL=fragmentMatchCondition.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var operationMatchCondition_1 = require("./operationMatchCondition"); | ||
exports.default = (function (_a) { | ||
var mutationMatchCondition = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, mutationNames = _b.mutationNames, pathRegexes = _b.pathRegexes; | ||
@@ -11,3 +11,4 @@ return operationMatchCondition_1.default({ | ||
}); | ||
}); | ||
}; | ||
exports.default = mutationMatchCondition; | ||
//# sourceMappingURL=mutationMatchCondition.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var ast_1 = require("../ast"); | ||
/** @hidden */ | ||
exports.default = (function (_a) { | ||
@@ -5,0 +6,0 @@ var _b = _a === void 0 ? {} : _a, operationNames = _b.operationNames, operationTypes = _b.operationTypes, pathRegexes = _b.pathRegexes; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var operationMatchCondition_1 = require("./operationMatchCondition"); | ||
exports.default = (function (_a) { | ||
var queryMatchCondition = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, queryNames = _b.queryNames, pathRegexes = _b.pathRegexes; | ||
@@ -11,3 +11,4 @@ return operationMatchCondition_1.default({ | ||
}); | ||
}); | ||
}; | ||
exports.default = queryMatchCondition; | ||
//# sourceMappingURL=queryMatchCondition.js.map |
@@ -15,2 +15,4 @@ "use strict"; | ||
exports.ScalarFieldToObjectFieldRewriter = ScalarFieldToObjectFieldRewriter_1.default; | ||
var JsonToTypedObjectRewriter_1 = require("./JsonToTypedObjectRewriter"); | ||
exports.JsonToTypedObjectRewriter = JsonToTypedObjectRewriter_1.default; | ||
//# sourceMappingURL=index.js.map |
@@ -39,2 +39,3 @@ import { ASTNode, DocumentNode, VariableDefinitionNode } from 'graphql'; | ||
*/ | ||
/** @hidden */ | ||
export declare const extractPath: (parents: ReadonlyArray<ASTNode>) => ReadonlyArray<string>; | ||
@@ -41,0 +42,0 @@ /** @hidden */ |
@@ -7,3 +7,3 @@ import matchCondition from './matchCondition'; | ||
} | ||
declare const _default: ({ fragmentNames, fragmentTypes, pathRegexes }?: FragmentMatchConditionOpts) => matchCondition; | ||
export default _default; | ||
declare const fragmentMatchCondition: ({ fragmentNames, fragmentTypes, pathRegexes }?: FragmentMatchConditionOpts) => matchCondition; | ||
export default fragmentMatchCondition; |
@@ -6,3 +6,3 @@ import matchCondition from './matchCondition'; | ||
} | ||
declare const _default: ({ mutationNames, pathRegexes }?: MutationMatchConditionOpts) => matchCondition; | ||
export default _default; | ||
declare const mutationMatchCondition: ({ mutationNames, pathRegexes }?: MutationMatchConditionOpts) => matchCondition; | ||
export default mutationMatchCondition; |
import matchCondition from './matchCondition'; | ||
/** @hidden */ | ||
export interface OperationMatchConditionOpts { | ||
@@ -8,2 +9,3 @@ operationNames?: string[]; | ||
declare const _default: ({ operationNames, operationTypes, pathRegexes }?: OperationMatchConditionOpts) => matchCondition; | ||
/** @hidden */ | ||
export default _default; |
@@ -6,3 +6,3 @@ import matchCondition from './matchCondition'; | ||
} | ||
declare const _default: ({ queryNames, pathRegexes }?: QueryMatchConditionOpts) => matchCondition; | ||
export default _default; | ||
declare const queryMatchCondition: ({ queryNames, pathRegexes }?: QueryMatchConditionOpts) => matchCondition; | ||
export default queryMatchCondition; |
@@ -7,1 +7,2 @@ export { default as Rewriter } from './Rewriter'; | ||
export { default as ScalarFieldToObjectFieldRewriter } from './ScalarFieldToObjectFieldRewriter'; | ||
export { default as JsonToTypedObjectRewriter } from './JsonToTypedObjectRewriter'; |
{ | ||
"name": "graphql-query-rewriter", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -301,2 +301,77 @@ # GraphQL Query Rewriter | ||
### JsonToTypedObjectRewriter | ||
`JsonToTypedObjectRewriter` can be used to rewrite a field that previously is just freeform JSON into a typed graphQL field with proper selections and subseelctions. For example, imagine there's a `user` query with type JSON, like `user: { type: GraphQLJSON }`. However, we'd like to improve our API by properly typing the fields within `user` using a graphQL type named `User` with fields `id`, `name`, `isAdmin`, etc.... For example, we have: | ||
```graphql | ||
query { | ||
user | ||
} | ||
``` | ||
and we want to change it to | ||
```graphql | ||
query { | ||
user { | ||
id | ||
name | ||
isAdmin | ||
} | ||
} | ||
``` | ||
we can make this change with the following rewriter: | ||
```js | ||
import { JsonToTypedObjectRewriter } from 'graphql-query-rewriter'; | ||
// add this to the rewriters array in graphqlRewriterMiddleware(...) | ||
const rewriter = new JsonToTypedObjectRewriter({ | ||
fieldName: 'user', | ||
objectFields: [ | ||
{ name: 'id' }, | ||
{ name: 'name' }, | ||
{ name: 'isAdmin' }, | ||
] | ||
}); | ||
``` | ||
This rewriter also supports rewriting subfields recursively by adding a `subfields` array inside of an object field. For example, the rewriter below: | ||
```js | ||
import { JsonToTypedObjectRewriter } from 'graphql-query-rewriter'; | ||
// add this to the rewriters array in graphqlRewriterMiddleware(...) | ||
const rewriter = new JsonToTypedObjectRewriter({ | ||
fieldName: 'user', | ||
objectFields: [ | ||
{ name: 'id' }, | ||
{ name: 'name' }, | ||
{ | ||
name: 'posts' | ||
subfields: [ | ||
{ name: 'id' }, | ||
{ name: 'title' }, | ||
] | ||
}, | ||
] | ||
}); | ||
``` | ||
would rewrite `query { user }` into: | ||
```graphql | ||
query { | ||
user { | ||
id | ||
name | ||
posts { | ||
id | ||
title | ||
} | ||
} | ||
} | ||
``` | ||
### NestFieldOutputsRewriter | ||
@@ -303,0 +378,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
282679
61
2786
546