Skip to content

Commit 8c0535e

Browse files
nodeDefinitions: Allow to pass number as ID values (#344)
Fixes #219 Fixes #218
1 parent 14753dd commit 8c0535e

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/node/node.d.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ export interface GraphQLNodeDefinitions<TContext> {
2323
* interface without a provided `resolveType` method.
2424
*/
2525
export function nodeDefinitions<TContext>(
26-
idFetcher: (id: string, context: TContext, info: GraphQLResolveInfo) => any,
26+
fetchById: (
27+
id: string,
28+
context: TContext,
29+
info: GraphQLResolveInfo,
30+
) => unknown,
2731
typeResolver?: GraphQLTypeResolver<any, TContext>,
2832
): GraphQLNodeDefinitions<TContext>;
2933

@@ -37,7 +41,7 @@ export interface ResolvedGlobalId {
3741
* Takes a type name and an ID specific to that type name, and returns a
3842
* "global ID" that is unique among all types.
3943
*/
40-
export function toGlobalId(type: string, id: string): string;
44+
export function toGlobalId(type: string | number, id: string): string;
4145

4246
/**
4347
* Takes the "global ID" created by toGlobalID, and returns the type name and ID
@@ -51,7 +55,11 @@ export function fromGlobalId(globalId: string): ResolvedGlobalId;
5155
* by calling idFetcher on the object, or if not provided, by accessing the `id`
5256
* property on the object.
5357
*/
54-
export function globalIdField(
58+
export function globalIdField<TContext>(
5559
typeName?: string,
56-
idFetcher?: (object: any, context: any, info: GraphQLResolveInfo) => string,
57-
): GraphQLFieldConfig<any, any>;
60+
idFetcher?: (
61+
obj: any,
62+
context: TContext,
63+
info: GraphQLResolveInfo,
64+
) => string | number,
65+
): GraphQLFieldConfig<any, TContext>;

src/node/node.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type GraphQLNodeDefinitions<TContext> = {|
3030
* interface without a provided `resolveType` method.
3131
*/
3232
export function nodeDefinitions<TContext>(
33-
idFetcher: (id: string, context: TContext, info: GraphQLResolveInfo) => any,
33+
fetchById: (id: string, context: TContext, info: GraphQLResolveInfo) => mixed,
3434
typeResolver?: GraphQLTypeResolver<any, TContext>,
3535
): GraphQLNodeDefinitions<TContext> {
3636
const nodeInterface = new GraphQLInterfaceType({
@@ -54,7 +54,7 @@ export function nodeDefinitions<TContext>(
5454
description: 'The ID of an object',
5555
},
5656
},
57-
resolve: (_obj, { id }, context, info) => idFetcher(id, context, info),
57+
resolve: (_obj, { id }, context, info) => fetchById(id, context, info),
5858
};
5959

6060
const nodesField = {
@@ -69,7 +69,7 @@ export function nodeDefinitions<TContext>(
6969
},
7070
},
7171
resolve: (_obj, { ids }, context, info) =>
72-
ids.map((id) => idFetcher(id, context, info)),
72+
ids.map((id) => fetchById(id, context, info)),
7373
};
7474

7575
return { nodeInterface, nodeField, nodesField };
@@ -84,8 +84,8 @@ type ResolvedGlobalId = {|
8484
* Takes a type name and an ID specific to that type name, and returns a
8585
* "global ID" that is unique among all types.
8686
*/
87-
export function toGlobalId(type: string, id: string): string {
88-
return base64([type, id].join(':'));
87+
export function toGlobalId(type: string, id: string | number): string {
88+
return base64([type, GraphQLID.serialize(id)].join(':'));
8989
}
9090

9191
/**
@@ -107,10 +107,14 @@ export function fromGlobalId(globalId: string): ResolvedGlobalId {
107107
* by calling idFetcher on the object, or if not provided, by accessing the `id`
108108
* property on the object.
109109
*/
110-
export function globalIdField(
110+
export function globalIdField<TContext>(
111111
typeName?: string,
112-
idFetcher?: (object: any, context: any, info: GraphQLResolveInfo) => string,
113-
): GraphQLFieldConfig<any, mixed> {
112+
idFetcher?: (
113+
obj: any,
114+
context: TContext,
115+
info: GraphQLResolveInfo,
116+
) => string | number,
117+
): GraphQLFieldConfig<any, TContext> {
114118
return {
115119
description: 'The ID of an object',
116120
type: new GraphQLNonNull(GraphQLID),

0 commit comments

Comments
 (0)