From 2d4823d70ff34e5f8106136c1d963cbead20b12e Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Tue, 6 Mar 2018 22:00:12 +0100 Subject: [PATCH] Merge wrappers into type/definition to fix circular imports (#1275) --- src/execution/execute.js | 2 +- src/type/__tests__/schema-test.js | 2 +- src/type/definition.js | 81 ++++++++++++++++++- src/type/directives.js | 2 +- src/type/index.js | 5 +- src/type/introspection.js | 3 +- src/utilities/buildASTSchema.js | 4 +- src/utilities/buildClientSchema.js | 4 +- src/utilities/extendSchema.js | 3 +- src/utilities/lexicographicSortSchema.js | 3 +- src/utilities/typeFromAST.js | 2 +- .../rules/VariablesInAllowedPosition.js | 3 +- 12 files changed, 96 insertions(+), 18 deletions(-) diff --git a/src/execution/execute.js b/src/execution/execute.js index 72dbcec6cc2..af9163aa9d3 100644 --- a/src/execution/execute.js +++ b/src/execution/execute.js @@ -33,7 +33,6 @@ import { isListType, isNonNullType, } from '../type/definition'; -import type { GraphQLList } from '../type/wrappers'; import type { GraphQLObjectType, GraphQLOutputType, @@ -43,6 +42,7 @@ import type { GraphQLFieldResolver, GraphQLResolveInfo, ResponsePath, + GraphQLList, } from '../type/definition'; import { GraphQLSchema } from '../type/schema'; import { diff --git a/src/type/__tests__/schema-test.js b/src/type/__tests__/schema-test.js index 6e37a633013..1f8a5563e1e 100644 --- a/src/type/__tests__/schema-test.js +++ b/src/type/__tests__/schema-test.js @@ -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', diff --git a/src/type/definition.js b/src/type/definition.js index 428304f71d8..c1385da4c65 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -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 @@ -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 (ofType: T): GraphQLList; + // 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 (ofType: T): GraphQLNonNull; + // 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 */ diff --git a/src/type/directives.js b/src/type/directives.js index aa8a734cceb..9d4d7764f8c 100644 --- a/src/type/directives.js +++ b/src/type/directives.js @@ -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'; diff --git a/src/type/index.js b/src/type/index.js index 1f23b363ac4..f7ac4fd8cd3 100644 --- a/src/type/index.js +++ b/src/type/index.js @@ -63,13 +63,10 @@ export { GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, -} from './definition'; - -export { // Type Wrappers GraphQLList, GraphQLNonNull, -} from './wrappers'; +} from './definition'; export { // Predicate diff --git a/src/type/introspection.js b/src/type/introspection.js index 41bbbb53fd8..a4810c6b630 100644 --- a/src/type/introspection.js +++ b/src/type/introspection.js @@ -14,6 +14,8 @@ import { print } from '../language/printer'; import { GraphQLObjectType, GraphQLEnumType, + GraphQLList, + GraphQLNonNull, isScalarType, isObjectType, isInterfaceType, @@ -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'; diff --git a/src/utilities/buildASTSchema.js b/src/utilities/buildASTSchema.js index c28da3aea3c..2ff2f7dd1d0 100644 --- a/src/utilities/buildASTSchema.js +++ b/src/utilities/buildASTSchema.js @@ -49,10 +49,10 @@ import { GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, + GraphQLList, + GraphQLNonNull, } from '../type/definition'; -import { GraphQLList, GraphQLNonNull } from '../type/wrappers'; - import { GraphQLDirective, GraphQLSkipDirective, diff --git a/src/utilities/buildClientSchema.js b/src/utilities/buildClientSchema.js index 3dddb07d50b..b86b9bff408 100644 --- a/src/utilities/buildClientSchema.js +++ b/src/utilities/buildClientSchema.js @@ -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, diff --git a/src/utilities/extendSchema.js b/src/utilities/extendSchema.js index 3c1a57aa7f2..cab1321acb8 100644 --- a/src/utilities/extendSchema.js +++ b/src/utilities/extendSchema.js @@ -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'; diff --git a/src/utilities/lexicographicSortSchema.js b/src/utilities/lexicographicSortSchema.js index 8593d24d7c5..570d3e45f98 100644 --- a/src/utilities/lexicographicSortSchema.js +++ b/src/utilities/lexicographicSortSchema.js @@ -12,7 +12,6 @@ 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, @@ -20,6 +19,8 @@ import { GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, + GraphQLList, + GraphQLNonNull, isListType, isNonNullType, isScalarType, diff --git a/src/utilities/typeFromAST.js b/src/utilities/typeFromAST.js index 07a25fd02ba..596e16f9d4b 100644 --- a/src/utilities/typeFromAST.js +++ b/src/utilities/typeFromAST.js @@ -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'; diff --git a/src/validation/rules/VariablesInAllowedPosition.js b/src/validation/rules/VariablesInAllowedPosition.js index 47073e5757b..25a95f5943e 100644 --- a/src/validation/rules/VariablesInAllowedPosition.js +++ b/src/validation/rules/VariablesInAllowedPosition.js @@ -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';