Skip to content

Commit

Permalink
nodeDefinitions: Allow to pass number as ID values
Browse files Browse the repository at this point in the history
Fixes #219
Fixes #218
  • Loading branch information
IvanGoncharov committed Jun 22, 2021
1 parent 14753dd commit 735164f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
18 changes: 13 additions & 5 deletions src/node/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ export interface GraphQLNodeDefinitions<TContext> {
* interface without a provided `resolveType` method.
*/
export function nodeDefinitions<TContext>(
idFetcher: (id: string, context: TContext, info: GraphQLResolveInfo) => any,
fetchById: (
id: string,
context: TContext,
info: GraphQLResolveInfo,
) => unknown,
typeResolver?: GraphQLTypeResolver<any, TContext>,
): GraphQLNodeDefinitions<TContext>;

Expand All @@ -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
Expand All @@ -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<TContext>(
typeName?: string,
idFetcher?: (object: any, context: any, info: GraphQLResolveInfo) => string,
): GraphQLFieldConfig<any, any>;
idFetcher?: (
obj: any,
context: TContext,
info: GraphQLResolveInfo,
) => string | number,
): GraphQLFieldConfig<any, TContext>;
20 changes: 12 additions & 8 deletions src/node/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type GraphQLNodeDefinitions<TContext> = {|
* interface without a provided `resolveType` method.
*/
export function nodeDefinitions<TContext>(
idFetcher: (id: string, context: TContext, info: GraphQLResolveInfo) => any,
fetchById: (id: string, context: TContext, info: GraphQLResolveInfo) => mixed,
typeResolver?: GraphQLTypeResolver<any, TContext>,
): GraphQLNodeDefinitions<TContext> {
const nodeInterface = new GraphQLInterfaceType({
Expand All @@ -54,7 +54,7 @@ export function nodeDefinitions<TContext>(
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 = {
Expand All @@ -69,7 +69,7 @@ export function nodeDefinitions<TContext>(
},
},
resolve: (_obj, { ids }, context, info) =>
ids.map((id) => idFetcher(id, context, info)),
ids.map((id) => fetchById(id, context, info)),
};

return { nodeInterface, nodeField, nodesField };
Expand All @@ -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(':'));
}

/**
Expand All @@ -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<TContext>(
typeName?: string,
idFetcher?: (object: any, context: any, info: GraphQLResolveInfo) => string,
): GraphQLFieldConfig<any, mixed> {
idFetcher?: (
obj: any,
context: TContext,
info: GraphQLResolveInfo,
) => string | number,
): GraphQLFieldConfig<any, TContext> {
return {
description: 'The ID of an object',
type: new GraphQLNonNull(GraphQLID),
Expand Down

0 comments on commit 735164f

Please sign in to comment.