graphql-operation-to-pojo
Advanced tools
Comparing version
@@ -52,3 +52,3 @@ "use strict"; | ||
convert(ast = this.info.operation, tree = [], parentPath = '', // the type definition from the schema that corresponds to `ast.selectionSet` | ||
convert(ast = this.info.operation, parentPath = '', // the type definition from the schema that corresponds to `ast.selectionSet` | ||
parentSchemaDef = this.getOperationTypeDef(), enclosingFragmentType) { | ||
@@ -61,3 +61,3 @@ const fieldMap = {}; | ||
const fragmentType = fragmentAst.typeCondition && fragmentAst.typeCondition.name.value; | ||
const fragmentFields = this.convert(fragmentAst, tree, parentPath, parentSchemaDef, fragmentType); // Add fragment fields to the fieldMap. | ||
const fragmentFields = this.convert(fragmentAst, parentPath, parentSchemaDef, fragmentType); // Add fragment fields to the fieldMap. | ||
// This is needed in case of fragment field overrides...graphql allows you to repeat fields that are | ||
@@ -98,17 +98,20 @@ // part of the fragment above and/or below the fragment. This matters because in the case of nested | ||
field.path = fieldPath; | ||
} // get the type definition from the schema for the return type of this field | ||
let returnType; | ||
if (this.options.includeReturnTypes) { | ||
// If the includeReturnTypes option is enabled, we always get the return type. | ||
// Otherwise, we only need it if there are nested field selections. | ||
returnType = this.getFieldReturnType(field, parentSchemaDef, fieldPath); | ||
field.returnType = returnType; | ||
} | ||
if (fieldAst.selectionSet) { | ||
// get the type definition from the schema for the return type of this field | ||
const returnType = this.getFieldReturnType(field, parentSchemaDef); | ||
if (!returnType) { | ||
throw Error(`graphqlOperationToPOJO(): Error matching query to schema: could not find type definition for field '${fieldPath}'`); | ||
returnType = this.getFieldReturnType(field, parentSchemaDef, fieldPath); | ||
} | ||
if (this.options.includeReturnTypes) { | ||
field.returnType = returnType; | ||
} | ||
field.fields = this.convert(fieldAst, [], fieldPath, (0, _graphql.getNamedType)(returnType)); | ||
field.fields = this.convert(fieldAst, fieldPath, (0, _graphql.getNamedType)(returnType)); | ||
} | ||
@@ -141,3 +144,3 @@ | ||
return [...tree, ...Object.values(fieldMap)]; | ||
return Object.values(fieldMap); | ||
} // Get the top-level type definition (either Query, Mutation, or Subscription) | ||
@@ -165,3 +168,3 @@ | ||
getFieldReturnType(field, parentSchemaDef) { | ||
getFieldReturnType(field, parentSchemaDef, fieldPath) { | ||
// Get the concrete type for fragment fields. | ||
@@ -175,3 +178,8 @@ // This is needed because for fragment fields, the original parentSchemaDef might be a union or interface type. | ||
const schemaField = parentSchemaDef.getFields()[field.name]; | ||
return schemaField && schemaField.type; | ||
if (!schemaField) { | ||
throw Error(`graphqlOperationToPOJO(): Error matching query to schema: could not find type definition for field '${fieldPath}'`); | ||
} | ||
return schemaField.type; | ||
} | ||
@@ -178,0 +186,0 @@ |
{ | ||
"name": "graphql-operation-to-pojo", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Converts GraphQL operations to plain old JS objects (POJOs), ready to to be serialized to JSON", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,2 +0,1 @@ | ||
// @flow | ||
import './polyfills' | ||
@@ -86,3 +85,2 @@ import { valueFromASTUntyped, getNamedType } from 'graphql' | ||
| FragmentDefinitionNode = this.info.operation, | ||
tree: FieldPOJO[] = [], | ||
parentPath = '', | ||
@@ -106,3 +104,2 @@ // the type definition from the schema that corresponds to `ast.selectionSet` | ||
fragmentAst, | ||
tree, | ||
parentPath, | ||
@@ -150,19 +147,24 @@ parentSchemaDef, | ||
} | ||
if (fieldAst.selectionSet) { | ||
// get the type definition from the schema for the return type of this field | ||
const returnType = this.getFieldReturnType( | ||
// get the type definition from the schema for the return type of this field | ||
let returnType: GraphQLOutputType | ||
if (this.options.includeReturnTypes) { | ||
// If the includeReturnTypes option is enabled, we always get the return type. | ||
// Otherwise, we only need it if there are nested field selections. | ||
returnType = this.getFieldReturnType( | ||
field, | ||
parentSchemaDef | ||
parentSchemaDef, | ||
fieldPath | ||
) | ||
field.returnType = returnType | ||
} | ||
if (fieldAst.selectionSet) { | ||
if (!returnType) { | ||
throw Error( | ||
`graphqlOperationToPOJO(): Error matching query to schema: could not find type definition for field '${fieldPath}'` | ||
returnType = this.getFieldReturnType( | ||
field, | ||
parentSchemaDef, | ||
fieldPath | ||
) | ||
} | ||
if (this.options.includeReturnTypes) { | ||
field.returnType = returnType | ||
} | ||
field.fields = this.convert( | ||
fieldAst, | ||
[], | ||
fieldPath, | ||
@@ -195,3 +197,3 @@ (getNamedType(returnType): any) | ||
} | ||
return [...tree, ...(Object.values(fieldMap): any)] | ||
return (Object.values(fieldMap): any) | ||
} | ||
@@ -222,4 +224,5 @@ | ||
field: FieldPOJO, | ||
parentSchemaDef: GraphQLCompositeType | ||
) { | ||
parentSchemaDef: GraphQLCompositeType, | ||
fieldPath: string | ||
): GraphQLOutputType { | ||
// Get the concrete type for fragment fields. | ||
@@ -234,3 +237,8 @@ // This is needed because for fragment fields, the original parentSchemaDef might be a union or interface type. | ||
const schemaField: GraphQLField<*, *> = (parentSchemaDef: any).getFields()[field.name] | ||
return schemaField && schemaField.type | ||
if (!schemaField) { | ||
throw Error( | ||
`graphqlOperationToPOJO(): Error matching query to schema: could not find type definition for field '${fieldPath}'` | ||
) | ||
} | ||
return schemaField.type | ||
} | ||
@@ -237,0 +245,0 @@ |
Sorry, the diff of this file is not supported yet
44812
2.31%591
2.25%