diff --git a/src/node/node.d.ts b/src/node/node.d.ts index 1e1b8f2..3976400 100644 --- a/src/node/node.d.ts +++ b/src/node/node.d.ts @@ -23,7 +23,11 @@ export interface GraphQLNodeDefinitions { * interface without a provided `resolveType` method. */ export function nodeDefinitions( - idFetcher: (id: string, context: TContext, info: GraphQLResolveInfo) => any, + fetchById: ( + id: string, + context: TContext, + info: GraphQLResolveInfo, + ) => unknown, typeResolver?: GraphQLTypeResolver, ): GraphQLNodeDefinitions; @@ -37,7 +41,7 @@ export interface ResolvedGlobalId { * Takes a type name and an ID specific to that type name, and returns a * "global ID" that is unique among all types. */ -export function toGlobalId(type: string, id: string): string; +export function toGlobalId(type: string | number, id: string): string; /** * Takes the "global ID" created by toGlobalID, and returns the type name and ID @@ -51,7 +55,11 @@ export function fromGlobalId(globalId: string): ResolvedGlobalId; * by calling idFetcher on the object, or if not provided, by accessing the `id` * property on the object. */ -export function globalIdField( +export function globalIdField( typeName?: string, - idFetcher?: (object: any, context: any, info: GraphQLResolveInfo) => string, -): GraphQLFieldConfig; + idFetcher?: ( + obj: any, + context: TContext, + info: GraphQLResolveInfo, + ) => string | number, +): GraphQLFieldConfig; diff --git a/src/node/node.js b/src/node/node.js index b3f0513..0efefae 100644 --- a/src/node/node.js +++ b/src/node/node.js @@ -30,7 +30,7 @@ type GraphQLNodeDefinitions = {| * interface without a provided `resolveType` method. */ export function nodeDefinitions( - idFetcher: (id: string, context: TContext, info: GraphQLResolveInfo) => any, + fetchById: (id: string, context: TContext, info: GraphQLResolveInfo) => mixed, typeResolver?: GraphQLTypeResolver, ): GraphQLNodeDefinitions { const nodeInterface = new GraphQLInterfaceType({ @@ -54,7 +54,7 @@ export function nodeDefinitions( description: 'The ID of an object', }, }, - resolve: (_obj, { id }, context, info) => idFetcher(id, context, info), + resolve: (_obj, { id }, context, info) => fetchById(id, context, info), }; const nodesField = { @@ -69,7 +69,7 @@ export function nodeDefinitions( }, }, resolve: (_obj, { ids }, context, info) => - ids.map((id) => idFetcher(id, context, info)), + ids.map((id) => fetchById(id, context, info)), }; return { nodeInterface, nodeField, nodesField }; @@ -84,8 +84,8 @@ type ResolvedGlobalId = {| * Takes a type name and an ID specific to that type name, and returns a * "global ID" that is unique among all types. */ -export function toGlobalId(type: string, id: string): string { - return base64([type, id].join(':')); +export function toGlobalId(type: string, id: string | number): string { + return base64([type, GraphQLID.serialize(id)].join(':')); } /** @@ -107,10 +107,14 @@ export function fromGlobalId(globalId: string): ResolvedGlobalId { * by calling idFetcher on the object, or if not provided, by accessing the `id` * property on the object. */ -export function globalIdField( +export function globalIdField( typeName?: string, - idFetcher?: (object: any, context: any, info: GraphQLResolveInfo) => string, -): GraphQLFieldConfig { + idFetcher?: ( + obj: any, + context: TContext, + info: GraphQLResolveInfo, + ) => string | number, +): GraphQLFieldConfig { return { description: 'The ID of an object', type: new GraphQLNonNull(GraphQLID),