graphql-operation-to-pojo
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -15,3 +15,3 @@ "use strict"; | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
@@ -50,3 +50,3 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
this.info = info; | ||
this.options = _objectSpread({}, ASTtoPOJOConverter.defaultOptions, {}, options); | ||
this.options = _objectSpread(_objectSpread({}, ASTtoPOJOConverter.defaultOptions), options); | ||
} | ||
@@ -103,9 +103,12 @@ | ||
const returnType = this.getFieldReturnType(field, parentSchemaDef); | ||
const typeDef = returnType && this.info.schema.getType(returnType.toString()); | ||
if (!typeDef) { | ||
if (!returnType) { | ||
throw Error(`graphqlOperationToPOJO(): Error matching query to schema: could not find type definition for field '${fieldPath}'`); | ||
} | ||
field.fields = this.convert(fieldAst, [], fieldPath, typeDef); | ||
if (this.options.includeReturnTypes) { | ||
field.returnType = returnType; | ||
} | ||
field.fields = this.convert(fieldAst, [], fieldPath, (0, _graphql.getNamedType)(returnType)); | ||
} | ||
@@ -170,3 +173,3 @@ | ||
const schemaField = parentSchemaDef.getFields()[field.name]; | ||
return schemaField && (0, _graphql.getNamedType)(schemaField.type); | ||
return schemaField && schemaField.type; | ||
} | ||
@@ -259,3 +262,4 @@ | ||
_defineProperty(ASTtoPOJOConverter, "defaultOptions", { | ||
includeFieldPath: false | ||
includeFieldPath: false, | ||
includeReturnTypes: false | ||
}); |
@@ -20,4 +20,4 @@ "use strict"; | ||
const { | ||
'0': key, | ||
'1': val | ||
0: key, | ||
1: val | ||
} = pair; | ||
@@ -24,0 +24,0 @@ Object.defineProperty(obj, key, { |
{ | ||
"name": "graphql-operation-to-pojo", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Converts GraphQL operations to plain old JS objects (POJOs), ready to to be serialized to JSON", | ||
@@ -13,3 +13,3 @@ "main": "lib/index.js", | ||
"watch": "babel -w src/ -d lib/", | ||
"prepublish": "npm run build", | ||
"prepublishOnly": "npm run test && npm run build", | ||
"test": "flow check && jest", | ||
@@ -44,16 +44,16 @@ "testonly": "jest" | ||
"devDependencies": { | ||
"@babel/cli": "^7.5.5", | ||
"@babel/core": "^7.5.5", | ||
"@babel/plugin-proposal-class-properties": "^7.5.5", | ||
"@babel/plugin-transform-modules-commonjs": "^7.5.0", | ||
"@babel/preset-env": "^7.5.5", | ||
"@babel/preset-flow": "^7.0.0", | ||
"@babel/cli": "^7.12.10", | ||
"@babel/core": "^7.12.10", | ||
"@babel/plugin-proposal-class-properties": "^7.12.1", | ||
"@babel/plugin-transform-modules-commonjs": "^7.12.1", | ||
"@babel/preset-env": "^7.12.11", | ||
"@babel/preset-flow": "^7.12.1", | ||
"babel-core": "~7.0.0-bridge.0", | ||
"babel-jest": "^24.9.0", | ||
"flow-bin": "^0.101.1", | ||
"graphql": "^14.5.4", | ||
"husky": "^2.7.0", | ||
"jest": "^24.9.0", | ||
"prettier": "~1.18.2", | ||
"pretty-quick": "^1.11.1" | ||
"babel-jest": "^26.6.3", | ||
"flow-bin": "^0.141.0", | ||
"graphql": "^15.4.0", | ||
"husky": "^4.3.6", | ||
"jest": "^26.6.3", | ||
"prettier": "^2.2.1", | ||
"pretty-quick": "^3.1.0" | ||
}, | ||
@@ -60,0 +60,0 @@ "peerDependencies": { |
@@ -41,3 +41,6 @@ # graphql-operation-to-pojo | ||
```js | ||
graphqlOperationToPOJO(info, { includeFieldPath: true }) | ||
graphqlOperationToPOJO(info, { | ||
includeFieldPath: true, | ||
includeReturnType: true, | ||
}) | ||
``` | ||
@@ -51,2 +54,26 @@ | ||
- `includeReturnTypes`: boolean (defaults to false) | ||
If true, the return type of each field will be included in the result. | ||
> Tip: | ||
> When using `includeReturnTypes`, you can use `getNamedType()` from graphql.js to strip any wrapping non-null or list types and get the underlying type. For example: | ||
> | ||
> ```lang-js | ||
> import { getNamedType } from 'graphql' | ||
> ... | ||
> /* | ||
> Suppose we're running a query that returns a list of users: | ||
> | ||
> type Query { | ||
> users: [User!]! | ||
> } | ||
> */ | ||
> const returnType = getNamedType(queryPojo.fields[0].returnType) | ||
> console.log(returnType.toString()) | ||
> // Output: 'User' | ||
> ``` | ||
</code> | ||
## Examples | ||
@@ -53,0 +80,0 @@ |
@@ -8,2 +8,3 @@ // @flow | ||
GraphQLField, | ||
GraphQLOutputType, | ||
OperationDefinitionNode, | ||
@@ -15,3 +16,3 @@ SelectionNode, | ||
ArgumentNode, | ||
DirectiveNode | ||
DirectiveNode, | ||
} from 'graphql' | ||
@@ -21,3 +22,3 @@ | ||
operation: string, | ||
fields: FieldPOJO[] | ||
fields: FieldPOJO[], | ||
} | ||
@@ -32,7 +33,9 @@ | ||
directives?: Object, | ||
path?: string | ||
path?: string, | ||
returnType?: GraphQLOutputType, | ||
} | ||
export type Options = { | ||
includeFieldPath?: boolean | ||
includeFieldPath?: boolean, | ||
includeReturnTypes?: boolean, | ||
} | ||
@@ -46,3 +49,3 @@ | ||
options?: Options | ||
) { | ||
): string { | ||
return JSON.stringify(graphqlOperationToPOJO(info, options)) | ||
@@ -65,3 +68,3 @@ } | ||
operation: operationAst.operation, | ||
fields: converter.convert() | ||
fields: converter.convert(), | ||
} | ||
@@ -75,3 +78,4 @@ } | ||
static defaultOptions = { | ||
includeFieldPath: false | ||
includeFieldPath: false, | ||
includeReturnTypes: false, | ||
} | ||
@@ -137,3 +141,3 @@ | ||
const field: FieldPOJO = { | ||
name: fieldAst.name.value | ||
name: fieldAst.name.value, | ||
} | ||
@@ -158,6 +162,3 @@ if (alias) { | ||
) | ||
const typeDef = | ||
returnType && | ||
this.info.schema.getType(returnType.toString()) | ||
if (!typeDef) { | ||
if (!returnType) { | ||
throw Error( | ||
@@ -167,2 +168,5 @@ `graphqlOperationToPOJO(): Error matching query to schema: could not find type definition for field '${fieldPath}'` | ||
} | ||
if (this.options.includeReturnTypes) { | ||
field.returnType = returnType | ||
} | ||
field.fields = this.convert( | ||
@@ -172,3 +176,3 @@ fieldAst, | ||
fieldPath, | ||
(typeDef: any) | ||
(getNamedType(returnType): any) | ||
) | ||
@@ -236,3 +240,3 @@ } | ||
const schemaField: GraphQLField<*, *> = (parentSchemaDef: any).getFields()[field.name] | ||
return schemaField && getNamedType(schemaField.type) | ||
return schemaField && schemaField.type | ||
} | ||
@@ -252,3 +256,3 @@ | ||
const selectionSet1ByName = Object.fromEntries( | ||
selectionSet1.map(field => [field.alias || field.name, field]) | ||
selectionSet1.map((field) => [field.alias || field.name, field]) | ||
) | ||
@@ -307,3 +311,3 @@ const mergedFieldsByName = { ...selectionSet1ByName } | ||
const args = Object.fromEntries( | ||
ast.map(arg => { | ||
ast.map((arg) => { | ||
const name = arg.name.value | ||
@@ -325,6 +329,6 @@ // @TODO refactor to use valueFromAST() and pass type info in the expected format so that | ||
return Object.fromEntries( | ||
ast.map(directive => { | ||
ast.map((directive) => { | ||
return [ | ||
directive.name.value, | ||
this.argumentsFromAst(directive.arguments) | ||
this.argumentsFromAst(directive.arguments), | ||
] | ||
@@ -331,0 +335,0 @@ }) |
@@ -7,3 +7,3 @@ if (!Object.fromEntries) { | ||
*/ | ||
Object.fromEntries = iter => { | ||
Object.fromEntries = (iter) => { | ||
const obj = {} | ||
@@ -20,3 +20,3 @@ | ||
// that it is an array or iterable. | ||
const { '0': key, '1': val } = pair | ||
const { 0: key, 1: val } = pair | ||
@@ -27,3 +27,3 @@ Object.defineProperty(obj, key, { | ||
writable: true, | ||
value: val | ||
value: val, | ||
}) | ||
@@ -30,0 +30,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { GraphQLResolveInfo } from 'graphql' | ||
import { GraphQLResolveInfo, GraphQLOutputType } from 'graphql' | ||
@@ -21,2 +21,3 @@ declare function graphqlOperationToPOJO( | ||
path?: string | ||
returnType?: GraphQLOutputType | ||
} | ||
@@ -26,4 +27,5 @@ | ||
includeFieldPath?: boolean | ||
includeReturnTypes?: boolean | ||
} | ||
export default graphqlOperationToPOJO |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
578
226
0
43799
9