Skip to content

Commit fb27b92

Browse files
Jannis Pohlmannleebyron
Jannis Pohlmann
authored andcommitted
Merge wrappers into type/definition to fix circular imports (#1275) (#1276)
1 parent 4a9e37a commit fb27b92

13 files changed

+96
-109
lines changed

src/execution/execute.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import {
3333
isListType,
3434
isNonNullType,
3535
} from '../type/definition';
36-
import type { GraphQLList } from '../type/wrappers';
3736
import type {
3837
GraphQLObjectType,
3938
GraphQLOutputType,
@@ -43,6 +42,7 @@ import type {
4342
GraphQLFieldResolver,
4443
GraphQLResolveInfo,
4544
ResponsePath,
45+
GraphQLList,
4646
} from '../type/definition';
4747
import { GraphQLSchema } from '../type/schema';
4848
import {

src/type/__tests__/schema-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import {
1212
GraphQLString,
1313
GraphQLInputObjectType,
1414
GraphQLDirective,
15+
GraphQLList,
1516
} from '../';
1617

1718
import { describe, it } from 'mocha';
1819
import { expect } from 'chai';
19-
import { GraphQLList } from '../wrappers';
2020

2121
const InterfaceType = new GraphQLInterfaceType({
2222
name: 'Interface',

src/type/definition.js

+80-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import type {
3232
} from '../language/ast';
3333
import type { GraphQLSchema } from './schema';
3434
import type { MaybePromise } from '../jsutils/MaybePromise';
35-
import { GraphQLList, GraphQLNonNull } from './wrappers';
3635

3736
// Predicates & Assertions
3837

@@ -315,6 +314,86 @@ export function assertAbstractType(type: mixed): GraphQLAbstractType {
315314
return type;
316315
}
317316

317+
/**
318+
* List Type Wrapper
319+
*
320+
* A list is a wrapping type which points to another type.
321+
* Lists are often created within the context of defining the fields of
322+
* an object type.
323+
*
324+
* Example:
325+
*
326+
* const PersonType = new GraphQLObjectType({
327+
* name: 'Person',
328+
* fields: () => ({
329+
* parents: { type: GraphQLList(PersonType) },
330+
* children: { type: GraphQLList(PersonType) },
331+
* })
332+
* })
333+
*
334+
*/
335+
declare class GraphQLList<+T: GraphQLType> {
336+
+ofType: T;
337+
static <T>(ofType: T): GraphQLList<T>;
338+
// Note: constructors cannot be used for covariant types. Drop the "new".
339+
constructor(ofType: any): void;
340+
}
341+
// eslint-disable-next-line no-redeclare
342+
export function GraphQLList(ofType) {
343+
if (this instanceof GraphQLList) {
344+
this.ofType = assertType(ofType);
345+
} else {
346+
return new GraphQLList(ofType);
347+
}
348+
}
349+
350+
// Also provide toJSON and inspect aliases for toString.
351+
const listProto: any = GraphQLList.prototype;
352+
listProto.toString = listProto.toJSON = listProto.inspect = function toString() {
353+
return '[' + String(this.ofType) + ']';
354+
};
355+
356+
/**
357+
* Non-Null Type Wrapper
358+
*
359+
* A non-null is a wrapping type which points to another type.
360+
* Non-null types enforce that their values are never null and can ensure
361+
* an error is raised if this ever occurs during a request. It is useful for
362+
* fields which you can make a strong guarantee on non-nullability, for example
363+
* usually the id field of a database row will never be null.
364+
*
365+
* Example:
366+
*
367+
* const RowType = new GraphQLObjectType({
368+
* name: 'Row',
369+
* fields: () => ({
370+
* id: { type: GraphQLNonNull(GraphQLString) },
371+
* })
372+
* })
373+
*
374+
* Note: the enforcement of non-nullability occurs within the executor.
375+
*/
376+
declare class GraphQLNonNull<+T: GraphQLNullableType> {
377+
+ofType: T;
378+
static <T>(ofType: T): GraphQLNonNull<T>;
379+
// Note: constructors cannot be used for covariant types. Drop the "new".
380+
constructor(ofType: any): void;
381+
}
382+
// eslint-disable-next-line no-redeclare
383+
export function GraphQLNonNull(ofType) {
384+
if (this instanceof GraphQLNonNull) {
385+
this.ofType = assertNullableType(ofType);
386+
} else {
387+
return new GraphQLNonNull(ofType);
388+
}
389+
}
390+
391+
// Also provide toJSON and inspect aliases for toString.
392+
const nonNullProto: any = GraphQLNonNull.prototype;
393+
nonNullProto.toString = nonNullProto.toJSON = nonNullProto.inspect = function toString() {
394+
return String(this.ofType) + '!';
395+
};
396+
318397
/**
319398
* These types wrap and modify other types
320399
*/

src/type/directives.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
GraphQLFieldConfigArgumentMap,
1212
GraphQLArgument,
1313
} from './definition';
14-
import { GraphQLNonNull } from './wrappers';
14+
import { GraphQLNonNull } from './definition';
1515
import { GraphQLString, GraphQLBoolean } from './scalars';
1616
import instanceOf from '../jsutils/instanceOf';
1717
import invariant from '../jsutils/invariant';

src/type/index.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,10 @@ export {
6363
GraphQLUnionType,
6464
GraphQLEnumType,
6565
GraphQLInputObjectType,
66-
} from './definition';
67-
68-
export {
6966
// Type Wrappers
7067
GraphQLList,
7168
GraphQLNonNull,
72-
} from './wrappers';
69+
} from './definition';
7370

7471
export {
7572
// Predicate

src/type/introspection.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { print } from '../language/printer';
1414
import {
1515
GraphQLObjectType,
1616
GraphQLEnumType,
17+
GraphQLList,
18+
GraphQLNonNull,
1719
isScalarType,
1820
isObjectType,
1921
isInterfaceType,
@@ -25,7 +27,6 @@ import {
2527
isAbstractType,
2628
isNamedType,
2729
} from './definition';
28-
import { GraphQLList, GraphQLNonNull } from '../type/wrappers';
2930
import { GraphQLString, GraphQLBoolean } from './scalars';
3031
import { DirectiveLocation } from '../language/directiveLocation';
3132
import type { GraphQLField } from './definition';

src/type/wrappers.js

-91
This file was deleted.

src/utilities/buildASTSchema.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ import {
4949
GraphQLUnionType,
5050
GraphQLEnumType,
5151
GraphQLInputObjectType,
52+
GraphQLList,
53+
GraphQLNonNull,
5254
} from '../type/definition';
5355

54-
import { GraphQLList, GraphQLNonNull } from '../type/wrappers';
55-
5656
import {
5757
GraphQLDirective,
5858
GraphQLSkipDirective,

src/utilities/buildClientSchema.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import {
2525
GraphQLUnionType,
2626
GraphQLEnumType,
2727
GraphQLInputObjectType,
28+
GraphQLList,
29+
GraphQLNonNull,
2830
assertNullableType,
2931
assertObjectType,
3032
assertInterfaceType,
3133
} from '../type/definition';
3234

33-
import { GraphQLList, GraphQLNonNull } from '../type/wrappers';
34-
3535
import type {
3636
GraphQLType,
3737
GraphQLInputType,

src/utilities/extendSchema.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ import {
2323
isUnionType,
2424
isListType,
2525
isNonNullType,
26+
GraphQLList,
27+
GraphQLNonNull,
2628
GraphQLObjectType,
2729
GraphQLInterfaceType,
2830
GraphQLUnionType,
2931
} from '../type/definition';
30-
import { GraphQLList, GraphQLNonNull } from '../type/wrappers';
3132

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

src/utilities/lexicographicSortSchema.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ import keyValMap from '../jsutils/keyValMap';
1212
import objectValues from '../jsutils/objectValues';
1313
import { GraphQLSchema } from '../type/schema';
1414
import { GraphQLDirective } from '../type/directives';
15-
import { GraphQLList, GraphQLNonNull } from '../type/wrappers';
1615
import type { GraphQLNamedType } from '../type/definition';
1716
import {
1817
GraphQLObjectType,
1918
GraphQLInterfaceType,
2019
GraphQLUnionType,
2120
GraphQLEnumType,
2221
GraphQLInputObjectType,
22+
GraphQLList,
23+
GraphQLNonNull,
2324
isListType,
2425
isNonNullType,
2526
isScalarType,

src/utilities/typeFromAST.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
ListTypeNode,
1414
NonNullTypeNode,
1515
} from '../language/ast';
16-
import { GraphQLList, GraphQLNonNull } from '../type/wrappers';
16+
import { GraphQLList, GraphQLNonNull } from '../type/definition';
1717
import type { GraphQLNamedType } from '../type/definition';
1818
import type { GraphQLSchema } from '../type/schema';
1919

src/validation/rules/VariablesInAllowedPosition.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
import type ValidationContext from '../ValidationContext';
1111
import { GraphQLError } from '../../error';
1212
import type { ASTVisitor } from '../../language/visitor';
13-
import { isNonNullType } from '../../type/definition';
14-
import { GraphQLNonNull } from '../../type/wrappers';
13+
import { GraphQLNonNull, isNonNullType } from '../../type/definition';
1514
import { isTypeSubTypeOf } from '../../utilities/typeComparators';
1615
import { typeFromAST } from '../../utilities/typeFromAST';
1716
import type { GraphQLType } from '../../type/definition';

0 commit comments

Comments
 (0)