Skip to content

Add predicates to for built-in types. #924

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

Merged
merged 1 commit into from
Dec 4, 2017
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
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export {
DirectiveLocation,

// Scalars
builtInScalars,
GraphQLInt,
GraphQLFloat,
GraphQLString,
Expand All @@ -80,6 +81,7 @@ export {
TypeNameMetaFieldDef,

// GraphQL Types for introspection.
introspectionTypes,
__Schema,
__Directive,
__DirectiveLocation,
Expand All @@ -97,6 +99,9 @@ export {
isCompositeType,
isAbstractType,
isNamedType,
isBuiltInScalar,
isIntrospectionType,
isSpecDirective,

// Assertions
assertType,
Expand Down
23 changes: 23 additions & 0 deletions src/type/builtins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* @flow */
/**
* Copyright (c) 2017, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

import type { GraphQLNamedType } from './definition';
import { introspectionTypes } from './introspection';
import { builtInScalars } from './scalars';

export const builtInTypes: Array<GraphQLNamedType> = [
...introspectionTypes,
...builtInScalars,
];

const builtInTypeNames = builtInTypes.map(x => x.name);
export function isBuiltInType(type: GraphQLNamedType): boolean %checks {
return builtInTypeNames.includes(type.name);
}
5 changes: 5 additions & 0 deletions src/type/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,8 @@ export const specifiedDirectives: Array<GraphQLDirective> = [
GraphQLSkipDirective,
GraphQLDeprecatedDirective,
];

const specifiedDirectivesNames = specifiedDirectives.map(x => x.name);
export function isSpecDirective(directive: GraphQLDirective): boolean %checks {
return specifiedDirectivesNames.includes(directive.name);
}
9 changes: 9 additions & 0 deletions src/type/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export {
} from './definition';

export {
isSpecDirective,

// "Enum" of Directive Locations
DirectiveLocation,

Expand All @@ -68,13 +70,18 @@ export {
GraphQLString,
GraphQLBoolean,
GraphQLID,
builtInScalars,
isBuiltInScalar,
} from './scalars';

export {
isIntrospectionType,

// "Enum" of Type Kinds
TypeKind,

// GraphQL Types for introspection.
introspectionTypes,
__Schema,
__Directive,
__DirectiveLocation,
Expand Down Expand Up @@ -128,3 +135,5 @@ export type {
GraphQLTypeResolver,
GraphQLUnionTypeConfig,
} from './definition';

export { builtInTypes, isBuiltInType } from './builtins';
18 changes: 17 additions & 1 deletion src/type/introspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from './definition';
import { GraphQLString, GraphQLBoolean } from './scalars';
import { DirectiveLocation } from './directives';
import type { GraphQLField } from './definition';
import type { GraphQLField, GraphQLNamedType } from './definition';


export const __Schema = new GraphQLObjectType({
Expand Down Expand Up @@ -451,3 +451,19 @@ export const TypeNameMetaFieldDef: GraphQLField<*, *> = {
args: [],
resolve: (source, args, context, { parentType }) => parentType.name
};

export const introspectionTypes: Array<GraphQLNamedType> = [
__Schema,
__Directive,
__DirectiveLocation,
__Type,
__Field,
__InputValue,
__EnumValue,
__TypeKind,
];

const introspectionTypeNames = introspectionTypes.map(x => x.name);
export function isIntrospectionType(type: GraphQLNamedType): boolean %checks {
return introspectionTypeNames.includes(type.name);
}
13 changes: 13 additions & 0 deletions src/type/scalars.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,16 @@ export const GraphQLID = new GraphQLScalarType({
undefined;
}
});

export const builtInScalars: Array<GraphQLScalarType> = [
GraphQLString,
GraphQLInt,
GraphQLFloat,
GraphQLBoolean,
GraphQLID,
];

const builtInScalarNames = builtInScalars.map(x => x.name);
export function isBuiltInScalar(type: GraphQLScalarType): boolean %checks {
return builtInScalarNames.includes(type.name);
}
38 changes: 4 additions & 34 deletions src/utilities/buildASTSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import invariant from '../jsutils/invariant';
import keyMap from '../jsutils/keyMap';
import keyValMap from '../jsutils/keyValMap';
import type {ObjMap} from '../jsutils/ObjMap';
import { valueFromAST } from './valueFromAST';
Expand Down Expand Up @@ -41,14 +42,6 @@ import type {

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

import {
GraphQLString,
GraphQLInt,
GraphQLFloat,
GraphQLBoolean,
GraphQLID,
} from '../type/scalars';

import {
GraphQLScalarType,
GraphQLObjectType,
Expand Down Expand Up @@ -81,16 +74,7 @@ import type {
DirectiveLocationEnum
} from '../type/directives';

import {
__Schema,
__Directive,
__DirectiveLocation,
__Type,
__Field,
__InputValue,
__EnumValue,
__TypeKind,
} from '../type/introspection';
import { builtInTypes } from '../type/builtins';

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

Expand Down Expand Up @@ -260,7 +244,7 @@ export class ASTDefinitionBuilder {
_typeDefinitionsMap: TypeDefinitionsMap;
_options: ?Options;
_resolveType: TypeResolver;
_cache: { [typeName: string]: GraphQLNamedType };
_cache: ObjMap<GraphQLNamedType>;

constructor(
typeDefinitionsMap: TypeDefinitionsMap,
Expand All @@ -271,21 +255,7 @@ export class ASTDefinitionBuilder {
this._options = options;
this._resolveType = resolveType;
// Initialize to the GraphQL built in scalars and introspection types.
this._cache = {
String: GraphQLString,
Int: GraphQLInt,
Float: GraphQLFloat,
Boolean: GraphQLBoolean,
ID: GraphQLID,
__Schema,
__Directive,
__DirectiveLocation,
__Type,
__Field,
__InputValue,
__EnumValue,
__TypeKind,
};
this._cache = keyMap(builtInTypes, type => type.name);
}

_buildType(typeName: string, typeNode?: ?NamedTypeNode): GraphQLNamedType {
Expand Down
36 changes: 2 additions & 34 deletions src/utilities/buildClientSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,6 @@ import {
GraphQLNonNull,
} from '../type/definition';

import {
__Schema,
__Directive,
__DirectiveLocation,
__Type,
__Field,
__InputValue,
__EnumValue,
__TypeKind,
} from '../type/introspection';

import {
GraphQLInt,
GraphQLFloat,
GraphQLString,
GraphQLBoolean,
GraphQLID
} from '../type/scalars';

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

import { TypeKind } from '../type/introspection';
Expand All @@ -72,6 +53,7 @@ import type {
IntrospectionNamedTypeRef,
} from './introspectionQuery';

import { builtInTypes } from '../type/builtins';

/**
* Build a GraphQLSchema for use by client tools.
Expand Down Expand Up @@ -101,21 +83,7 @@ export function buildClientSchema(
// A cache to use to store the actual GraphQLType definition objects by name.
// Initialize to the GraphQL built in scalars. All functions below are inline
// so that this type def cache is within the scope of the closure.
const typeDefCache = {
String: GraphQLString,
Int: GraphQLInt,
Float: GraphQLFloat,
Boolean: GraphQLBoolean,
ID: GraphQLID,
__Schema,
__Directive,
__DirectiveLocation,
__Type,
__Field,
__InputValue,
__EnumValue,
__TypeKind,
};
const typeDefCache = keyMap(builtInTypes, type => type.name);

// Given a type reference in introspection, return the GraphQLType instance.
// preferring cached instances before building new instances.
Expand Down
50 changes: 15 additions & 35 deletions src/utilities/schemaPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import isInvalid from '../jsutils/isInvalid';
import { astFromValue } from '../utilities/astFromValue';
import { print } from '../language/printer';
import type { GraphQLSchema } from '../type/schema';
import type { GraphQLType } from '../type/definition';
import type { GraphQLType, GraphQLNamedType } from '../type/definition';
import {
GraphQLScalarType,
GraphQLObjectType,
Expand All @@ -23,7 +23,14 @@ import {
GraphQLInputObjectType,
} from '../type/definition';
import { GraphQLString } from '../type/scalars';
import { DEFAULT_DEPRECATION_REASON } from '../type/directives';
import {
GraphQLDirective,
DEFAULT_DEPRECATION_REASON,
isSpecDirective,
} from '../type/directives';

import { isIntrospectionType } from '../type/introspection';
import { isBuiltInType } from '../type/builtins';

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

Expand All @@ -38,7 +45,7 @@ export function printSchema(schema: GraphQLSchema, options?: Options): string {
return printFilteredSchema(
schema,
n => !isSpecDirective(n),
isDefinedType,
type => !isBuiltInType(type),
options
);
}
Expand All @@ -55,45 +62,18 @@ export function printIntrospectionSchema(
);
}

function isSpecDirective(directiveName: string): boolean {
return (
directiveName === 'skip' ||
directiveName === 'include' ||
directiveName === 'deprecated'
);
}

function isDefinedType(typename: string): boolean {
return !isIntrospectionType(typename) && !isBuiltInScalar(typename);
}

function isIntrospectionType(typename: string): boolean {
return typename.indexOf('__') === 0;
}

function isBuiltInScalar(typename: string): boolean {
return (
typename === 'String' ||
typename === 'Boolean' ||
typename === 'Int' ||
typename === 'Float' ||
typename === 'ID'
);
}

function printFilteredSchema(
schema: GraphQLSchema,
directiveFilter: (type: string) => boolean,
typeFilter: (type: string) => boolean,
directiveFilter: (type: GraphQLDirective) => boolean,
typeFilter: (type: GraphQLNamedType) => boolean,
options
): string {
const directives = schema.getDirectives()
.filter(directive => directiveFilter(directive.name));
const directives = schema.getDirectives().filter(directiveFilter);
const typeMap = schema.getTypeMap();
const types = Object.keys(typeMap)
.filter(typeFilter)
.sort((name1, name2) => name1.localeCompare(name2))
.map(typeName => typeMap[typeName]);
.map(typeName => typeMap[typeName])
.filter(typeFilter);

return [ printSchemaDefinition(schema) ].concat(
directives.map(directive => printDirective(directive, options)),
Expand Down