Skip to content

Commit

Permalink
Move GraphQL-specific utils to tools
Browse files Browse the repository at this point in the history
  • Loading branch information
leebyron committed Jul 30, 2015
1 parent 9424ef2 commit 19b3367
Show file tree
Hide file tree
Showing 20 changed files with 66 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/executor/executor.js
Expand Up @@ -12,7 +12,7 @@ import { GraphQLError, locatedError } from '../error';
import find from '../utils/find';
import invariant from '../utils/invariant';
import isNullish from '../utils/isNullish';
import typeFromAST from '../utils/typeFromAST';
import { typeFromAST } from '../tools/typeFromAST';
import { Kind } from '../language';
import { getVariableValues, getArgumentValues } from './values';
import {
Expand Down
14 changes: 8 additions & 6 deletions src/executor/values.js
Expand Up @@ -12,8 +12,8 @@ import { GraphQLError } from '../error';
import invariant from '../utils/invariant';
import isNullish from '../utils/isNullish';
import keyMap from '../utils/keyMap';
import typeFromAST from '../utils/typeFromAST';
import valueFromAST from '../utils/valueFromAST';
import { typeFromAST } from '../tools/typeFromAST';
import { valueFromAST } from '../tools/valueFromAST';
import { print } from '../language/printer';
import {
isInputType,
Expand Down Expand Up @@ -112,7 +112,8 @@ function isValidValue(type: GraphQLInputType, value: any): boolean {
if (isNullish(value)) {
return false;
}
return isValidValue(type.ofType, value);
var nullableType: GraphQLInputType = (type.ofType: any);
return isValidValue(nullableType, value);
}

if (isNullish(value)) {
Expand All @@ -121,7 +122,7 @@ function isValidValue(type: GraphQLInputType, value: any): boolean {

// Lists accept a non-list value as a list of one.
if (type instanceof GraphQLList) {
var itemType = type.ofType;
var itemType: GraphQLInputType = (type.ofType: any);
if (Array.isArray(value)) {
return value.every(item => isValidValue(itemType, item));
} else {
Expand Down Expand Up @@ -165,15 +166,16 @@ function coerceValue(type: GraphQLInputType, value: any): any {
if (type instanceof GraphQLNonNull) {
// Note: we're not checking that the result of coerceValue is non-null.
// We only call this function after calling isValidValue.
return coerceValue(type.ofType, value);
var nullableType: GraphQLInputType = (type.ofType: any);
return coerceValue(nullableType, value);
}

if (isNullish(value)) {
return null;
}

if (type instanceof GraphQLList) {
var itemType = type.ofType;
var itemType: GraphQLInputType = (type.ofType: any);
// TODO: support iterable input
if (Array.isArray(value)) {
return value.map(item => coerceValue(itemType, item));
Expand Down
8 changes: 4 additions & 4 deletions src/utils/TypeInfo.js → src/tools/TypeInfo.js
Expand Up @@ -8,7 +8,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

import { Kind } from '../language';
import * as Kind from '../language/kinds';
import type { Field } from '../language/ast';
import {
isCompositeType,
Expand Down Expand Up @@ -36,16 +36,16 @@ import {
} from '../type/introspection';
import type { GraphQLSchema } from '../type/schema';
import type { Node } from '../language/ast';
import typeFromAST from './typeFromAST';
import find from './find';
import { typeFromAST } from './typeFromAST';
import find from '../utils/find';


/**
* TypeInfo is a utility class which, given a GraphQL schema, can keep track
* of the current field and type definitions at any point in a GraphQL document
* AST during a recursive descent by calling `enter(node)` and `leave(node)`.
*/
export default class TypeInfo {
export class TypeInfo {
_schema: GraphQLSchema;
_typeStack: Array<?GraphQLOutputType>;
_parentTypeStack: Array<?GraphQLCompositeType>;
Expand Down
Expand Up @@ -9,13 +9,13 @@

import { describe, it } from 'mocha';
import { expect } from 'chai';
import astFromValue from '../astFromValue';
import { astFromValue } from '../astFromValue';
import {
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLList
} from '../../type/definition';
import { GraphQLFloat } from '../../type/scalars';
GraphQLList,
GraphQLFloat,
} from '../../type';


describe('astFromValue', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/astFromValue.js → src/tools/astFromValue.js
Expand Up @@ -47,7 +47,7 @@ import { GraphQLFloat } from '../type/scalars';
* | Number | Int / Float |
*
*/
export default function astFromValue(
export function astFromValue(
value: any,
type?: ?GraphQLType
): ?Value {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/buildASTSchema.js
Expand Up @@ -10,7 +10,7 @@
import isNullish from '../utils/isNullish';
import keyMap from '../utils/keyMap';
import keyValMap from '../utils/keyValMap';
import valueFromAST from '../utils/valueFromAST';
import { valueFromAST } from './valueFromAST';

import {
LIST_TYPE,
Expand Down
4 changes: 1 addition & 3 deletions src/tools/buildClientSchema.js
Expand Up @@ -11,10 +11,8 @@
import invariant from '../utils/invariant';
import keyMap from '../utils/keyMap';
import keyValMap from '../utils/keyValMap';
import valueFromAST from '../utils/valueFromAST';

import { valueFromAST } from './valueFromAST';
import { parseValue } from '../language/parser';

import { GraphQLSchema } from '../type/schema';

import {
Expand Down
26 changes: 26 additions & 0 deletions src/tools/index.js
Expand Up @@ -22,3 +22,29 @@ export {
printSchema,
printIntrospectionSchema,
} from './schemaPrinter';

// Create a GraphQLType from a GraphQL language AST
export {
typeFromAST
} from './typeFromAST';

// Create a JavaScript value from a GraphQL language AST
export {
valueFromAST
} from './valueFromAST';

// Create a GraphQL language AST from a JavaScript value
export {
astFromValue
} from './astFromValue';

// A helper to use within recursive-descent visitors which need to be aware of
// the GraphQL type system.
export {
TypeInfo
} from './TypeInfo';

// Determine if AST values adhere to a GraphQL type
export {
isValidLiteralValue
} from './isValidLiteralValue';
Expand Up @@ -22,9 +22,9 @@ import {
GraphQLNonNull
} from '../type/definition';
import type { GraphQLInputType } from '../type/definition';
import invariant from './invariant';
import keyMap from './keyMap';
import isNullish from './isNullish';
import invariant from '../utils/invariant';
import keyMap from '../utils/keyMap';
import isNullish from '../utils/isNullish';


/**
Expand All @@ -34,7 +34,7 @@ import isNullish from './isNullish';
* Note that this only validates literal values, variables are assumed to
* provide values of the correct type.
*/
export default function isValidLiteralValue(
export function isValidLiteralValue(
type: GraphQLInputType,
valueAST: Value
): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/schemaPrinter.js
Expand Up @@ -10,7 +10,7 @@

import invariant from '../utils/invariant';
import isNullish from '../utils/isNullish';
import astFromValue from '../utils/astFromValue';
import { astFromValue } from '../tools/astFromValue';
import { print } from '../language/printer';
import type { GraphQLSchema } from '../type/schema';
import type { GraphQLNamedType } from '../type/definition';
Expand Down
4 changes: 2 additions & 2 deletions src/utils/typeFromAST.js → src/tools/typeFromAST.js
Expand Up @@ -8,15 +8,15 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

import invariant from './invariant';
import invariant from '../utils/invariant';
import { NAMED_TYPE, LIST_TYPE, NON_NULL_TYPE } from '../language/kinds';
import type { Type } from '../language/ast';
import { GraphQLList, GraphQLNonNull } from '../type/definition';
import type { GraphQLType } from '../type/definition';
import type { GraphQLSchema } from '../type/schema';


export default function typeFromAST(
export function typeFromAST(
schema: GraphQLSchema,
inputTypeAST: Type
): ?GraphQLType {
Expand Down
10 changes: 5 additions & 5 deletions src/utils/valueFromAST.js → src/tools/valueFromAST.js
Expand Up @@ -8,10 +8,10 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

import keyMap from './keyMap';
import invariant from './invariant';
import isNullish from './isNullish';
import { Kind } from '../language';
import keyMap from '../utils/keyMap';
import invariant from '../utils/invariant';
import isNullish from '../utils/isNullish';
import * as Kind from '../language/kinds';
import {
GraphQLScalarType,
GraphQLEnumType,
Expand Down Expand Up @@ -43,7 +43,7 @@ import type {
* | Int / Float | Number |
*
*/
export default function valueFromAST(
export function valueFromAST(
valueAST: ?Value,
type: GraphQLInputType,
variables?: ?{ [key: string]: any }
Expand Down
2 changes: 1 addition & 1 deletion src/type/introspection.js
Expand Up @@ -8,7 +8,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

import astFromValue from '../utils/astFromValue';
import { astFromValue } from '../tools/astFromValue';
import { print } from '../language/printer';
import {
GraphQLScalarType,
Expand Down
2 changes: 1 addition & 1 deletion src/validator/index.js
Expand Up @@ -25,7 +25,7 @@ import type {
GraphQLArgument
} from '../type/definition';
import type { GraphQLDirective } from '../type/directives';
import TypeInfo from '../utils/TypeInfo';
import { TypeInfo } from '../tools/TypeInfo';
import { allRules } from './allRules';


Expand Down
2 changes: 1 addition & 1 deletion src/validator/rules/ArgumentsOfCorrectType.js
Expand Up @@ -12,7 +12,7 @@ import type { ValidationContext } from '../index';

import { GraphQLError } from '../../error';
import { print } from '../../language/printer';
import isValidLiteralValue from '../../utils/isValidLiteralValue';
import { isValidLiteralValue } from '../../tools/isValidLiteralValue';
import { badValueMessage } from '../errors';


Expand Down
2 changes: 1 addition & 1 deletion src/validator/rules/DefaultValuesOfCorrectType.js
Expand Up @@ -13,7 +13,7 @@ import type { ValidationContext } from '../index';
import { GraphQLError } from '../../error';
import { print } from '../../language/printer';
import { GraphQLNonNull } from '../../type/definition';
import isValidLiteralValue from '../../utils/isValidLiteralValue';
import { isValidLiteralValue } from '../../tools/isValidLiteralValue';
import {
defaultForNonNullArgMessage,
badValueForDefaultArgMessage,
Expand Down
2 changes: 1 addition & 1 deletion src/validator/rules/OverlappingFieldsCanBeMerged.js
Expand Up @@ -26,7 +26,7 @@ import type {
GraphQLType,
GraphQLFieldDefinition
} from '../../type/definition';
import typeFromAST from '../../utils/typeFromAST';
import { typeFromAST } from '../../tools/typeFromAST';
import find from '../../utils/find';


Expand Down
2 changes: 1 addition & 1 deletion src/validator/rules/PossibleFragmentSpreads.js
Expand Up @@ -17,7 +17,7 @@ import {
GraphQLUnionType
} from '../../type/definition';
import keyMap from '../../utils/keyMap';
import typeFromAST from '../../utils/typeFromAST';
import { typeFromAST } from '../../tools/typeFromAST';
import {
typeIncompatibleSpreadMessage,
typeIncompatibleAnonSpreadMessage,
Expand Down
2 changes: 1 addition & 1 deletion src/validator/rules/VariablesAreInputTypes.js
Expand Up @@ -13,7 +13,7 @@ import type { VariableDefinition } from '../../language/ast';
import { GraphQLError } from '../../error';
import { print } from '../../language/printer';
import { isInputType } from '../../type/definition';
import typeFromAST from '../../utils/typeFromAST';
import { typeFromAST } from '../../tools/typeFromAST';
import { nonInputTypeOnVarMessage } from '../errors';


Expand Down
2 changes: 1 addition & 1 deletion src/validator/rules/VariablesInAllowedPosition.js
Expand Up @@ -9,7 +9,7 @@
*/

import type { ValidationContext } from '../index';
import typeFromAST from '../../utils/typeFromAST';
import { typeFromAST } from '../../tools/typeFromAST';
import { GraphQLError } from '../../error';
import { GraphQLList, GraphQLNonNull } from '../../type/definition';
import { badVarPosMessage } from '../errors';
Expand Down

0 comments on commit 19b3367

Please sign in to comment.