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

feat(scalar): add generics to scalars #1522

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/type/definition.js
Expand Up @@ -46,7 +46,7 @@ import type { MaybePromise } from '../jsutils/MaybePromise';
* These are all of the possible kinds of types.
*/
export type GraphQLType =
| GraphQLScalarType
| GraphQLScalarType<>
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
Expand Down Expand Up @@ -84,7 +84,7 @@ export function isScalarType(type) {
return instanceOf(type, GraphQLScalarType);
}

export function assertScalarType(type: mixed): GraphQLScalarType {
export function assertScalarType(type: mixed): GraphQLScalarType<> {
invariant(
isScalarType(type),
`Expected ${inspect(type)} to be a GraphQL Scalar type.`,
Expand Down Expand Up @@ -201,12 +201,12 @@ export function assertNonNullType(type: mixed): GraphQLNonNull<any> {
* These types may be used as input types for arguments and directives.
*/
export type GraphQLInputType =
| GraphQLScalarType
| GraphQLScalarType<>
| GraphQLEnumType
| GraphQLInputObjectType
| GraphQLList<GraphQLInputType>
| GraphQLNonNull<
| GraphQLScalarType
| GraphQLScalarType<>
| GraphQLEnumType
| GraphQLInputObjectType
| GraphQLList<GraphQLInputType>,
Expand All @@ -233,14 +233,14 @@ export function assertInputType(type: mixed): GraphQLInputType {
* These types may be used as output types as the result of fields.
*/
export type GraphQLOutputType =
| GraphQLScalarType
| GraphQLScalarType<>
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
| GraphQLEnumType
| GraphQLList<GraphQLOutputType>
| GraphQLNonNull<
| GraphQLScalarType
| GraphQLScalarType<>
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
Expand Down Expand Up @@ -270,7 +270,7 @@ export function assertOutputType(type: mixed): GraphQLOutputType {
/**
* These types may describe types which may be leaf values.
*/
export type GraphQLLeafType = GraphQLScalarType | GraphQLEnumType;
export type GraphQLLeafType = GraphQLScalarType<> | GraphQLEnumType;

export function isLeafType(type: mixed): boolean %checks {
return isScalarType(type) || isEnumType(type);
Expand Down Expand Up @@ -423,7 +423,7 @@ export function assertWrappingType(type: mixed): GraphQLWrappingType {
* These types can all accept null as a value.
*/
export type GraphQLNullableType =
| GraphQLScalarType
| GraphQLScalarType<>
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
Expand Down Expand Up @@ -458,7 +458,7 @@ export function getNullableType(type) {
* These named types do not include modifiers like List or NonNull.
*/
export type GraphQLNamedType =
| GraphQLScalarType
| GraphQLScalarType<>
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
Expand Down Expand Up @@ -532,12 +532,12 @@ function resolveThunk<+T>(thunk: Thunk<T>): T {
* });
*
*/
export class GraphQLScalarType {
export class GraphQLScalarType<TInternal = any, TExternal = TInternal> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain what the purpose of / difference between TInternal and TExternal is?

name: string;
description: ?string;
serialize: GraphQLScalarSerializer<*>;
parseValue: GraphQLScalarValueParser<*>;
parseLiteral: GraphQLScalarLiteralParser<*>;
serialize: GraphQLScalarSerializer<TExternal>;
parseValue: GraphQLScalarValueParser<TInternal>;
parseLiteral: GraphQLScalarLiteralParser<TInternal>;
astNode: ?ScalarTypeDefinitionNode;
extensionASTNodes: ?$ReadOnlyArray<ScalarTypeExtensionNode>;

Expand Down
10 changes: 5 additions & 5 deletions src/type/scalars.js
Expand Up @@ -58,7 +58,7 @@ function coerceInt(value: mixed): number {
return value;
}

export const GraphQLInt = new GraphQLScalarType({
export const GraphQLInt = new GraphQLScalarType<number>({
name: 'Int',
description:
'The `Int` scalar type represents non-fractional signed whole numeric ' +
Expand Down Expand Up @@ -102,7 +102,7 @@ function coerceFloat(value: mixed): number {
return value;
}

export const GraphQLFloat = new GraphQLScalarType({
export const GraphQLFloat = new GraphQLScalarType<number>({
name: 'Float',
description:
'The `Float` scalar type represents signed double-precision fractional ' +
Expand Down Expand Up @@ -146,7 +146,7 @@ function coerceString(value: mixed): string {
return value;
}

export const GraphQLString = new GraphQLScalarType({
export const GraphQLString = new GraphQLScalarType<string>({
name: 'String',
description:
'The `String` scalar type represents textual data, represented as UTF-8 ' +
Expand Down Expand Up @@ -180,7 +180,7 @@ function coerceBoolean(value: mixed): boolean {
return value;
}

export const GraphQLBoolean = new GraphQLScalarType({
export const GraphQLBoolean = new GraphQLScalarType<boolean>({
name: 'Boolean',
description: 'The `Boolean` scalar type represents `true` or `false`.',
serialize: serializeBoolean,
Expand Down Expand Up @@ -214,7 +214,7 @@ function coerceID(value: mixed): string {
throw new TypeError(`ID cannot represent value: ${inspect(value)}`);
}

export const GraphQLID = new GraphQLScalarType({
export const GraphQLID = new GraphQLScalarType<string>({
name: 'ID',
description:
'The `ID` scalar type represents a unique identifier, often used to ' +
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/buildClientSchema.js
Expand Up @@ -201,7 +201,7 @@ export function buildClientSchema(

function buildScalarDef(
scalarIntrospection: IntrospectionScalarType,
): GraphQLScalarType {
): GraphQLScalarType<> {
return new GraphQLScalarType({
name: scalarIntrospection.name,
description: scalarIntrospection.description,
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/extendSchema.js
Expand Up @@ -426,7 +426,7 @@ export function extendSchema(
return newValueMap;
}

function extendScalarType(type: GraphQLScalarType): GraphQLScalarType {
function extendScalarType(type: GraphQLScalarType<>): GraphQLScalarType<> {
const name = type.name;
const extensionASTNodes = typeExtensionsMap[name]
? type.extensionASTNodes
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/schemaPrinter.js
Expand Up @@ -179,7 +179,7 @@ export function printType(type: GraphQLNamedType, options?: Options): string {
throw new Error(`Unknown type: ${(type: empty)}.`);
}

function printScalar(type: GraphQLScalarType, options): string {
function printScalar(type: GraphQLScalarType<>, options): string {
return printDescription(options, type) + `scalar ${type.name}`;
}

Expand Down