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

nodeDefinitions: Allow to pass number as ID values #344

Merged
merged 1 commit into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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