Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/utilities/astFromValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,54 +42,51 @@ import { GraphQLID } from '../type/scalars';
*
*/
export function astFromValue(value: mixed, type: GraphQLInputType): ?ValueNode {
// Ensure flow knows that we treat function params as const.
const _value = value;

if (isNonNullType(type)) {
const astValue = astFromValue(_value, type.ofType);
const astValue = astFromValue(value, type.ofType);
if (astValue && astValue.kind === Kind.NULL) {
return null;
}
return astValue;
}

// only explicit null, not undefined, NaN
if (_value === null) {
if (value === null) {
return { kind: Kind.NULL };
}

// undefined, NaN
if (isInvalid(_value)) {
if (isInvalid(value)) {
return null;
}

// Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
// the value is not an array, convert the value using the list's item type.
if (isListType(type)) {
const itemType = type.ofType;
if (isCollection(_value)) {
if (isCollection(value)) {
const valuesNodes = [];
forEach((_value: any), item => {
forEach((value: any), item => {
const itemNode = astFromValue(item, itemType);
if (itemNode) {
valuesNodes.push(itemNode);
}
});
return { kind: Kind.LIST, values: valuesNodes };
}
return astFromValue(_value, itemType);
return astFromValue(value, itemType);
}

// Populate the fields of the input object by creating ASTs from each value
// in the JavaScript object according to the fields in the input type.
if (isInputObjectType(type)) {
if (_value === null || typeof _value !== 'object') {
if (value === null || typeof value !== 'object') {
return null;
}
const fields = objectValues(type.getFields());
const fieldNodes = [];
fields.forEach(field => {
const fieldValue = astFromValue(_value[field.name], field.type);
const fieldValue = astFromValue(value[field.name], field.type);
if (fieldValue) {
fieldNodes.push({
kind: Kind.OBJECT_FIELD,
Expand All @@ -104,7 +101,7 @@ export function astFromValue(value: mixed, type: GraphQLInputType): ?ValueNode {
if (isScalarType(type) || isEnumType(type)) {
// Since value is an internally represented value, it must be serialized
// to an externally represented value before converting into an AST.
const serialized = type.serialize(_value);
const serialized = type.serialize(value);
if (isNullish(serialized)) {
return null;
}
Expand Down
15 changes: 6 additions & 9 deletions src/utilities/typeComparators.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,26 @@ export function doTypesOverlap(
typeA: GraphQLCompositeType,
typeB: GraphQLCompositeType,
): boolean {
// So flow is aware this is constant
const _typeB = typeB;

// Equivalent types overlap
if (typeA === _typeB) {
if (typeA === typeB) {
return true;
}

if (isAbstractType(typeA)) {
if (isAbstractType(_typeB)) {
if (isAbstractType(typeB)) {
// If both types are abstract, then determine if there is any intersection
// between possible concrete types of each.
return schema
.getPossibleTypes(typeA)
.some(type => schema.isPossibleType(_typeB, type));
.some(type => schema.isPossibleType(typeB, type));
}
// Determine if the latter type is a possible concrete type of the former.
return schema.isPossibleType(typeA, _typeB);
return schema.isPossibleType(typeA, typeB);
}

if (isAbstractType(_typeB)) {
if (isAbstractType(typeB)) {
// Determine if the former type is a possible concrete type of the latter.
return schema.isPossibleType(_typeB, typeA);
return schema.isPossibleType(typeB, typeA);
}

// Otherwise the types do not overlap.
Expand Down