Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge wrappers into type/definition to fix circular imports (#1275) #1276

Merged
merged 1 commit into from
Mar 7, 2018
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
2 changes: 1 addition & 1 deletion src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
isListType,
isNonNullType,
} from '../type/definition';
import type { GraphQLList } from '../type/wrappers';
import type {
GraphQLObjectType,
GraphQLOutputType,
Expand All @@ -43,6 +42,7 @@ import type {
GraphQLFieldResolver,
GraphQLResolveInfo,
ResponsePath,
GraphQLList,
} from '../type/definition';
import { GraphQLSchema } from '../type/schema';
import {
Expand Down
2 changes: 1 addition & 1 deletion src/type/__tests__/schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
GraphQLString,
GraphQLInputObjectType,
GraphQLDirective,
GraphQLList,
} from '../';

import { describe, it } from 'mocha';
import { expect } from 'chai';
import { GraphQLList } from '../wrappers';

const InterfaceType = new GraphQLInterfaceType({
name: 'Interface',
Expand Down
81 changes: 80 additions & 1 deletion src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import type {
} from '../language/ast';
import type { GraphQLSchema } from './schema';
import type { MaybePromise } from '../jsutils/MaybePromise';
import { GraphQLList, GraphQLNonNull } from './wrappers';

// Predicates & Assertions

Expand Down Expand Up @@ -315,6 +314,86 @@ export function assertAbstractType(type: mixed): GraphQLAbstractType {
return type;
}

/**
* List Type Wrapper
*
* A list is a wrapping type which points to another type.
* Lists are often created within the context of defining the fields of
* an object type.
*
* Example:
*
* const PersonType = new GraphQLObjectType({
* name: 'Person',
* fields: () => ({
* parents: { type: GraphQLList(PersonType) },
* children: { type: GraphQLList(PersonType) },
* })
* })
*
*/
declare class GraphQLList<+T: GraphQLType> {
+ofType: T;
static <T>(ofType: T): GraphQLList<T>;
// Note: constructors cannot be used for covariant types. Drop the "new".
constructor(ofType: any): void;
}
// eslint-disable-next-line no-redeclare
export function GraphQLList(ofType) {
if (this instanceof GraphQLList) {
this.ofType = assertType(ofType);
} else {
return new GraphQLList(ofType);
}
}

// Also provide toJSON and inspect aliases for toString.
const listProto: any = GraphQLList.prototype;
listProto.toString = listProto.toJSON = listProto.inspect = function toString() {
return '[' + String(this.ofType) + ']';
};

/**
* Non-Null Type Wrapper
*
* A non-null is a wrapping type which points to another type.
* Non-null types enforce that their values are never null and can ensure
* an error is raised if this ever occurs during a request. It is useful for
* fields which you can make a strong guarantee on non-nullability, for example
* usually the id field of a database row will never be null.
*
* Example:
*
* const RowType = new GraphQLObjectType({
* name: 'Row',
* fields: () => ({
* id: { type: GraphQLNonNull(GraphQLString) },
* })
* })
*
* Note: the enforcement of non-nullability occurs within the executor.
*/
declare class GraphQLNonNull<+T: GraphQLNullableType> {
+ofType: T;
static <T>(ofType: T): GraphQLNonNull<T>;
// Note: constructors cannot be used for covariant types. Drop the "new".
constructor(ofType: any): void;
}
// eslint-disable-next-line no-redeclare
export function GraphQLNonNull(ofType) {
if (this instanceof GraphQLNonNull) {
this.ofType = assertNullableType(ofType);
} else {
return new GraphQLNonNull(ofType);
}
}

// Also provide toJSON and inspect aliases for toString.
const nonNullProto: any = GraphQLNonNull.prototype;
nonNullProto.toString = nonNullProto.toJSON = nonNullProto.inspect = function toString() {
return String(this.ofType) + '!';
};

/**
* These types wrap and modify other types
*/
Expand Down
2 changes: 1 addition & 1 deletion src/type/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
GraphQLFieldConfigArgumentMap,
GraphQLArgument,
} from './definition';
import { GraphQLNonNull } from './wrappers';
import { GraphQLNonNull } from './definition';
import { GraphQLString, GraphQLBoolean } from './scalars';
import instanceOf from '../jsutils/instanceOf';
import invariant from '../jsutils/invariant';
Expand Down
5 changes: 1 addition & 4 deletions src/type/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,10 @@ export {
GraphQLUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
} from './definition';

export {
// Type Wrappers
GraphQLList,
GraphQLNonNull,
} from './wrappers';
} from './definition';

export {
// Predicate
Expand Down
3 changes: 2 additions & 1 deletion src/type/introspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { print } from '../language/printer';
import {
GraphQLObjectType,
GraphQLEnumType,
GraphQLList,
GraphQLNonNull,
isScalarType,
isObjectType,
isInterfaceType,
Expand All @@ -25,7 +27,6 @@ import {
isAbstractType,
isNamedType,
} from './definition';
import { GraphQLList, GraphQLNonNull } from '../type/wrappers';
import { GraphQLString, GraphQLBoolean } from './scalars';
import { DirectiveLocation } from '../language/directiveLocation';
import type { GraphQLField } from './definition';
Expand Down
91 changes: 0 additions & 91 deletions src/type/wrappers.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/utilities/buildASTSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ import {
GraphQLUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLList,
GraphQLNonNull,
} from '../type/definition';

import { GraphQLList, GraphQLNonNull } from '../type/wrappers';

import {
GraphQLDirective,
GraphQLSkipDirective,
Expand Down
4 changes: 2 additions & 2 deletions src/utilities/buildClientSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import {
GraphQLUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLList,
GraphQLNonNull,
assertNullableType,
assertObjectType,
assertInterfaceType,
} from '../type/definition';

import { GraphQLList, GraphQLNonNull } from '../type/wrappers';

import type {
GraphQLType,
GraphQLInputType,
Expand Down
3 changes: 2 additions & 1 deletion src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import {
isUnionType,
isListType,
isNonNullType,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
} from '../type/definition';
import { GraphQLList, GraphQLNonNull } from '../type/wrappers';

import { GraphQLDirective } from '../type/directives';

Expand Down
3 changes: 2 additions & 1 deletion src/utilities/lexicographicSortSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import keyValMap from '../jsutils/keyValMap';
import objectValues from '../jsutils/objectValues';
import { GraphQLSchema } from '../type/schema';
import { GraphQLDirective } from '../type/directives';
import { GraphQLList, GraphQLNonNull } from '../type/wrappers';
import type { GraphQLNamedType } from '../type/definition';
import {
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLList,
GraphQLNonNull,
isListType,
isNonNullType,
isScalarType,
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/typeFromAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
ListTypeNode,
NonNullTypeNode,
} from '../language/ast';
import { GraphQLList, GraphQLNonNull } from '../type/wrappers';
import { GraphQLList, GraphQLNonNull } from '../type/definition';
import type { GraphQLNamedType } from '../type/definition';
import type { GraphQLSchema } from '../type/schema';

Expand Down
3 changes: 1 addition & 2 deletions src/validation/rules/VariablesInAllowedPosition.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import type ValidationContext from '../ValidationContext';
import { GraphQLError } from '../../error';
import type { ASTVisitor } from '../../language/visitor';
import { isNonNullType } from '../../type/definition';
import { GraphQLNonNull } from '../../type/wrappers';
import { GraphQLNonNull, isNonNullType } from '../../type/definition';
import { isTypeSubTypeOf } from '../../utilities/typeComparators';
import { typeFromAST } from '../../utilities/typeFromAST';
import type { GraphQLType } from '../../type/definition';
Expand Down