Skip to content

Commit 134184e

Browse files
committed
Merge branch 'APIs-guru-predicates'
2 parents 74d5bf5 + ce937da commit 134184e

File tree

8 files changed

+104
-124
lines changed

8 files changed

+104
-124
lines changed

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export {
5656
TypeKind,
5757

5858
// Scalars
59+
specifiedScalarTypes,
5960
GraphQLInt,
6061
GraphQLFloat,
6162
GraphQLString,
@@ -77,6 +78,7 @@ export {
7778
TypeNameMetaFieldDef,
7879

7980
// GraphQL Types for introspection.
81+
introspectionTypes,
8082
__Schema,
8183
__Directive,
8284
__DirectiveLocation,
@@ -94,6 +96,9 @@ export {
9496
isCompositeType,
9597
isAbstractType,
9698
isNamedType,
99+
isSpecifiedScalarType,
100+
isIntrospectionType,
101+
isSpecifiedDirective,
97102

98103
// Assertions
99104
assertType,

src/type/directives.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,11 @@ export const specifiedDirectives: Array<GraphQLDirective> = [
160160
GraphQLSkipDirective,
161161
GraphQLDeprecatedDirective,
162162
];
163+
164+
export function isSpecifiedDirective(
165+
directive: GraphQLDirective
166+
): boolean %checks {
167+
return specifiedDirectives.some(
168+
specifiedDirective => specifiedDirective.name === directive.name
169+
);
170+
}

src/type/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export {
4949
GraphQLDirective,
5050

5151
// Built-in Directives defined by the Spec
52+
isSpecifiedDirective,
5253
specifiedDirectives,
5354
GraphQLIncludeDirective,
5455
GraphQLSkipDirective,
@@ -60,6 +61,8 @@ export {
6061

6162
// Common built-in scalar instances.
6263
export {
64+
isSpecifiedScalarType,
65+
specifiedScalarTypes,
6366
GraphQLInt,
6467
GraphQLFloat,
6568
GraphQLString,
@@ -72,6 +75,8 @@ export {
7275
TypeKind,
7376

7477
// GraphQL Types for introspection.
78+
isIntrospectionType,
79+
introspectionTypes,
7580
__Schema,
7681
__Directive,
7782
__DirectiveLocation,

src/type/introspection.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ import {
2020
GraphQLList,
2121
GraphQLNonNull,
2222
isAbstractType,
23+
isNamedType,
2324
} from './definition';
2425
import { GraphQLString, GraphQLBoolean } from './scalars';
2526
import { DirectiveLocation } from '../language/directiveLocation';
26-
import type { GraphQLField } from './definition';
27+
import type { GraphQLField, GraphQLNamedType, GraphQLType } from './definition';
2728

2829

2930
export const __Schema = new GraphQLObjectType({
@@ -451,3 +452,20 @@ export const TypeNameMetaFieldDef: GraphQLField<*, *> = {
451452
args: [],
452453
resolve: (source, args, context, { parentType }) => parentType.name
453454
};
455+
456+
export const introspectionTypes: Array<GraphQLNamedType> = [
457+
__Schema,
458+
__Directive,
459+
__DirectiveLocation,
460+
__Type,
461+
__Field,
462+
__InputValue,
463+
__EnumValue,
464+
__TypeKind,
465+
];
466+
467+
export function isIntrospectionType(type: ?GraphQLType): boolean %checks {
468+
return isNamedType(type) && introspectionTypes.some(
469+
introspectionType => introspectionType.name === type.name
470+
);
471+
}

src/type/scalars.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
* @flow
88
*/
99

10-
import { GraphQLScalarType } from './definition';
10+
import { GraphQLScalarType, isNamedType } from './definition';
1111
import * as Kind from '../language/kinds';
12+
import type { GraphQLType } from './definition';
1213

1314
// As per the GraphQL Spec, Integers are only treated as valid when a valid
1415
// 32-bit signed integer, providing the broadest support across platforms.
@@ -135,3 +136,17 @@ export const GraphQLID = new GraphQLScalarType({
135136
undefined;
136137
}
137138
});
139+
140+
export const specifiedScalarTypes: Array<GraphQLScalarType> = [
141+
GraphQLString,
142+
GraphQLInt,
143+
GraphQLFloat,
144+
GraphQLBoolean,
145+
GraphQLID,
146+
];
147+
148+
export function isSpecifiedScalarType(type: ?GraphQLType): boolean %checks {
149+
return isNamedType(type) && specifiedScalarTypes.some(
150+
specifiedScalarType => specifiedScalarType.name === type.name
151+
);
152+
}

src/utilities/buildASTSchema.js

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import invariant from '../jsutils/invariant';
11+
import keyMap from '../jsutils/keyMap';
1112
import keyValMap from '../jsutils/keyValMap';
1213
import type {ObjMap} from '../jsutils/ObjMap';
1314
import { valueFromAST } from './valueFromAST';
@@ -39,15 +40,9 @@ import type {
3940
DirectiveDefinitionNode,
4041
} from '../language/ast';
4142

42-
import { GraphQLSchema } from '../type/schema';
43-
44-
import {
45-
GraphQLString,
46-
GraphQLInt,
47-
GraphQLFloat,
48-
GraphQLBoolean,
49-
GraphQLID,
50-
} from '../type/scalars';
43+
import type {
44+
DirectiveLocationEnum
45+
} from '../language/directiveLocation';
5146

5247
import {
5348
GraphQLScalarType,
@@ -62,35 +57,26 @@ import {
6257
assertOutputType,
6358
} from '../type/definition';
6459

65-
import type {
66-
GraphQLType,
67-
GraphQLNamedType,
68-
GraphQLInputType,
69-
GraphQLOutputType,
70-
GraphQLFieldConfig,
71-
} from '../type/definition';
72-
7360
import {
7461
GraphQLDirective,
7562
GraphQLSkipDirective,
7663
GraphQLIncludeDirective,
7764
GraphQLDeprecatedDirective,
7865
} from '../type/directives';
7966

80-
import type {
81-
DirectiveLocationEnum
82-
} from '../language/directiveLocation';
67+
import { introspectionTypes } from '../type/introspection';
8368

84-
import {
85-
__Schema,
86-
__Directive,
87-
__DirectiveLocation,
88-
__Type,
89-
__Field,
90-
__InputValue,
91-
__EnumValue,
92-
__TypeKind,
93-
} from '../type/introspection';
69+
import { specifiedScalarTypes } from '../type/scalars';
70+
71+
import { GraphQLSchema } from '../type/schema';
72+
73+
import type {
74+
GraphQLType,
75+
GraphQLNamedType,
76+
GraphQLInputType,
77+
GraphQLOutputType,
78+
GraphQLFieldConfig,
79+
} from '../type/definition';
9480

9581
type Options = {| commentDescriptions?: boolean |};
9682

@@ -260,7 +246,7 @@ export class ASTDefinitionBuilder {
260246
_typeDefinitionsMap: TypeDefinitionsMap;
261247
_options: ?Options;
262248
_resolveType: TypeResolver;
263-
_cache: { [typeName: string]: GraphQLNamedType };
249+
_cache: ObjMap<GraphQLNamedType>;
264250

265251
constructor(
266252
typeDefinitionsMap: TypeDefinitionsMap,
@@ -271,21 +257,10 @@ export class ASTDefinitionBuilder {
271257
this._options = options;
272258
this._resolveType = resolveType;
273259
// Initialize to the GraphQL built in scalars and introspection types.
274-
this._cache = {
275-
String: GraphQLString,
276-
Int: GraphQLInt,
277-
Float: GraphQLFloat,
278-
Boolean: GraphQLBoolean,
279-
ID: GraphQLID,
280-
__Schema,
281-
__Directive,
282-
__DirectiveLocation,
283-
__Type,
284-
__Field,
285-
__InputValue,
286-
__EnumValue,
287-
__TypeKind,
288-
};
260+
this._cache = keyMap(
261+
specifiedScalarTypes.concat(introspectionTypes),
262+
type => type.name
263+
);
289264
}
290265

291266
_buildType(typeName: string, typeNode?: ?NamedTypeNode): GraphQLNamedType {

src/utilities/buildClientSchema.js

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { valueFromAST } from './valueFromAST';
1414
import { parseValue } from '../language/parser';
1515
import { GraphQLSchema } from '../type/schema';
1616

17+
import { DirectiveLocation } from '../language/directiveLocation';
18+
1719
import {
1820
isInputType,
1921
isOutputType,
@@ -27,38 +29,19 @@ import {
2729
GraphQLNonNull,
2830
} from '../type/definition';
2931

30-
import {
31-
__Schema,
32-
__Directive,
33-
__DirectiveLocation,
34-
__Type,
35-
__Field,
36-
__InputValue,
37-
__EnumValue,
38-
__TypeKind,
39-
} from '../type/introspection';
40-
41-
import {
42-
GraphQLInt,
43-
GraphQLFloat,
44-
GraphQLString,
45-
GraphQLBoolean,
46-
GraphQLID
47-
} from '../type/scalars';
48-
49-
import { GraphQLDirective } from '../type/directives';
50-
51-
import { DirectiveLocation } from '../language/directiveLocation';
52-
53-
import { TypeKind } from '../type/introspection';
54-
5532
import type {
5633
GraphQLType,
5734
GraphQLInputType,
5835
GraphQLOutputType,
5936
GraphQLNamedType,
6037
} from '../type/definition';
6138

39+
import { GraphQLDirective } from '../type/directives';
40+
41+
import { introspectionTypes, TypeKind } from '../type/introspection';
42+
43+
import { specifiedScalarTypes } from '../type/scalars';
44+
6245
import type {
6346
IntrospectionQuery,
6447
IntrospectionType,
@@ -74,7 +57,6 @@ import type {
7457
IntrospectionNamedTypeRef,
7558
} from './introspectionQuery';
7659

77-
7860
/**
7961
* Build a GraphQLSchema for use by client tools.
8062
*
@@ -103,21 +85,10 @@ export function buildClientSchema(
10385
// A cache to use to store the actual GraphQLType definition objects by name.
10486
// Initialize to the GraphQL built in scalars. All functions below are inline
10587
// so that this type def cache is within the scope of the closure.
106-
const typeDefCache = {
107-
String: GraphQLString,
108-
Int: GraphQLInt,
109-
Float: GraphQLFloat,
110-
Boolean: GraphQLBoolean,
111-
ID: GraphQLID,
112-
__Schema,
113-
__Directive,
114-
__DirectiveLocation,
115-
__Type,
116-
__Field,
117-
__InputValue,
118-
__EnumValue,
119-
__TypeKind,
120-
};
88+
const typeDefCache = keyMap(
89+
specifiedScalarTypes.concat(introspectionTypes),
90+
type => type.name
91+
);
12192

12293
// Given a type reference in introspection, return the GraphQLType instance.
12394
// preferring cached instances before building new instances.

0 commit comments

Comments
 (0)