Skip to content

Commit c812b85

Browse files
committed
Implement toJSON and inspect on all types.
This adds toJSON() and inspect() methods on all types which shares the same function as toString(). This means that converting a GraphQL type to JSON or inspecting it with console.log (in Node) will produce something more meaningful than the implementation details of the type. Closes #568
1 parent 7eae7aa commit c812b85

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/type/definition.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,16 @@ export class GraphQLScalarType {
314314
toString(): string {
315315
return this.name;
316316
}
317+
318+
toJSON: () => string;
319+
inspect: () => string;
317320
}
318321

322+
// Also provide toJSON and inspect aliases for toString.
323+
GraphQLScalarType.prototype.toJSON =
324+
GraphQLScalarType.prototype.inspect =
325+
GraphQLScalarType.prototype.toString;
326+
319327
export type GraphQLScalarTypeConfig<TInternal, TExternal> = {
320328
name: string;
321329
description?: ?string;
@@ -402,8 +410,16 @@ export class GraphQLObjectType {
402410
toString(): string {
403411
return this.name;
404412
}
413+
414+
toJSON: () => string;
415+
inspect: () => string;
405416
}
406417

418+
// Also provide toJSON and inspect aliases for toString.
419+
GraphQLObjectType.prototype.toJSON =
420+
GraphQLObjectType.prototype.inspect =
421+
GraphQLObjectType.prototype.toString;
422+
407423
function defineInterfaces(
408424
type: GraphQLObjectType,
409425
interfacesThunk: Thunk<?Array<GraphQLInterfaceType>>
@@ -643,8 +659,16 @@ export class GraphQLInterfaceType {
643659
toString(): string {
644660
return this.name;
645661
}
662+
663+
toJSON: () => string;
664+
inspect: () => string;
646665
}
647666

667+
// Also provide toJSON and inspect aliases for toString.
668+
GraphQLInterfaceType.prototype.toJSON =
669+
GraphQLInterfaceType.prototype.inspect =
670+
GraphQLInterfaceType.prototype.toString;
671+
648672
export type GraphQLInterfaceTypeConfig<TSource, TContext> = {
649673
name: string,
650674
fields: Thunk<GraphQLFieldConfigMap<TSource, TContext>>,
@@ -715,8 +739,16 @@ export class GraphQLUnionType {
715739
toString(): string {
716740
return this.name;
717741
}
742+
743+
toJSON: () => string;
744+
inspect: () => string;
718745
}
719746

747+
// Also provide toJSON and inspect aliases for toString.
748+
GraphQLUnionType.prototype.toJSON =
749+
GraphQLUnionType.prototype.inspect =
750+
GraphQLUnionType.prototype.toString;
751+
720752
function defineTypes(
721753
unionType: GraphQLUnionType,
722754
typesThunk: Thunk<Array<GraphQLObjectType>>
@@ -852,8 +884,16 @@ export class GraphQLEnumType/* <T> */ {
852884
toString(): string {
853885
return this.name;
854886
}
887+
888+
toJSON: () => string;
889+
inspect: () => string;
855890
}
856891

892+
// Also provide toJSON and inspect aliases for toString.
893+
GraphQLEnumType.prototype.toJSON =
894+
GraphQLEnumType.prototype.inspect =
895+
GraphQLEnumType.prototype.toString;
896+
857897
function defineEnumValues(
858898
type: GraphQLEnumType,
859899
valueMap: GraphQLEnumValueConfigMap/* <T> */
@@ -988,8 +1028,16 @@ export class GraphQLInputObjectType {
9881028
toString(): string {
9891029
return this.name;
9901030
}
1031+
1032+
toJSON: () => string;
1033+
inspect: () => string;
9911034
}
9921035

1036+
// Also provide toJSON and inspect aliases for toString.
1037+
GraphQLInputObjectType.prototype.toJSON =
1038+
GraphQLInputObjectType.prototype.inspect =
1039+
GraphQLInputObjectType.prototype.toString;
1040+
9931041
export type GraphQLInputObjectTypeConfig = {
9941042
name: string;
9951043
fields: Thunk<GraphQLInputFieldConfigMap>;
@@ -1051,8 +1099,16 @@ export class GraphQLList<T: GraphQLType> {
10511099
toString(): string {
10521100
return '[' + String(this.ofType) + ']';
10531101
}
1102+
1103+
toJSON: () => string;
1104+
inspect: () => string;
10541105
}
10551106

1107+
// Also provide toJSON and inspect aliases for toString.
1108+
GraphQLList.prototype.toJSON =
1109+
GraphQLList.prototype.inspect =
1110+
GraphQLList.prototype.toString;
1111+
10561112

10571113
/**
10581114
* Non-Null Modifier
@@ -1089,4 +1145,12 @@ export class GraphQLNonNull<T: GraphQLNullableType> {
10891145
toString(): string {
10901146
return this.ofType.toString() + '!';
10911147
}
1148+
1149+
toJSON: () => string;
1150+
inspect: () => string;
10921151
}
1152+
1153+
// Also provide toJSON and inspect aliases for toString.
1154+
GraphQLNonNull.prototype.toJSON =
1155+
GraphQLNonNull.prototype.inspect =
1156+
GraphQLNonNull.prototype.toString;

0 commit comments

Comments
 (0)