Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
fix(healSchema): revert 6.3.6
Browse files Browse the repository at this point in the history
healSchema cannot automatically include reparsing of default values.

Automatic reparsing assumes that the default value corresponds to the old type, but that may not always be the case, and when it is not the case, the default value will be lost if automatic reparsing is implemented as in 6.3.6.
  • Loading branch information
yaacovCR committed Sep 3, 2019
1 parent 7ffcbb2 commit 58b98ac
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
8 changes: 7 additions & 1 deletion src/generate/addResolveFunctionsToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import SchemaError from './SchemaError';
import checkForResolveTypeResolver from './checkForResolveTypeResolver';
import extendResolversFromInterfaces from './extendResolversFromInterfaces';
import forEachField from './forEachField';
import forEachDefaultValue from './forEachDefaultValue';
import { parseInputValue, serializeInputValue } from '../transformInputValue';
import { healSchema } from '../schemaVisitor';

function addResolveFunctionsToSchema(
Expand Down Expand Up @@ -183,8 +185,12 @@ function addResolveFunctionsToSchema(

checkForResolveTypeResolver(schema, requireResolversForResolveType);

// schema may have new scalar or enum types that require healing
// serialize all default values prior to healing fields with new scalar/enum types.
forEachDefaultValue(schema, serializeInputValue);
// schema may have new scalar/enum types that require healing
healSchema(schema);
// reparse all default values with new parsing functions.
forEachDefaultValue(schema, parseInputValue);

if (defaultFieldResolver) {
forEachField(schema, field => {
Expand Down
31 changes: 31 additions & 0 deletions src/generate/forEachDefaultValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getNamedType, GraphQLInputObjectType, GraphQLSchema, GraphQLObjectType } from 'graphql';
import { IDefaultValueIteratorFn } from '../Interfaces';

function forEachDefaultValue(schema: GraphQLSchema, fn: IDefaultValueIteratorFn): void {

const typeMap = schema.getTypeMap();
Object.keys(typeMap).forEach(typeName => {
const type = typeMap[typeName];

if (!getNamedType(type).name.startsWith('__')) {
if (type instanceof GraphQLObjectType) {
const fields = type.getFields();
Object.keys(fields).forEach(fieldName => {
const field = fields[fieldName];

field.args.forEach(arg => {
arg.defaultValue = fn(arg.type, arg.defaultValue);
});
});
} else if (type instanceof GraphQLInputObjectType) {
const fields = type.getFields();
Object.keys(fields).forEach(fieldName => {
const field = fields[fieldName];
field.defaultValue = fn(field.type, field.defaultValue);
});
}
}
});
}

export default forEachDefaultValue;
18 changes: 1 addition & 17 deletions src/schemaVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ import {
GraphQLList,
GraphQLNonNull,
isNamedType,
getNamedType,
} from 'graphql';

import {
getArgumentValues,
} from 'graphql/execution/values';
import { serializeInputValue, parseInputValue } from './transformInputValue';

export type VisitableSchemaType =
GraphQLSchema
Expand Down Expand Up @@ -393,30 +391,16 @@ export function healSchema(schema: GraphQLSchema) {
field.type = healType(field.type);
if (field.args) {
each(field.args, arg => {
const originalType = arg.type;
arg.type = healType(arg.type);
if (getNamedType(arg.type) !== getNamedType(originalType)) {
arg.defaultValue = parseInputValue(
arg.type,
serializeInputValue(originalType, arg.defaultValue)
);
}
});
}
});
}

function healInputFields(type: GraphQLInputObjectType) {
each(type.getFields(), field => {
const originalType = field.type;
field.type = healType(field.type);
if (getNamedType(field.type) !== getNamedType(originalType)) {
field.defaultValue = parseInputValue(
field.type,
serializeInputValue(originalType, field.defaultValue)
);
}
});
});
}

function healType<T extends GraphQLType>(type: T): T {
Expand Down

0 comments on commit 58b98ac

Please sign in to comment.