Skip to content

Commit bc87d29

Browse files
committed
feat: allow selfNodeId to work on nullable values
In some cases we want a nullable ID, for those the validator should ignore instead of throwing.
1 parent 8c7172a commit bc87d29

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/selfNodeId.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ directive @${name} on FIELD_DEFINITION | OBJECT
8888
type Type3 {
8989
id: ID!
9090
}
91+
type TypeNullable {
92+
id: ID @selfNodeId # test nullable
93+
}
9194
type ${type4} @selfNodeId {
9295
id: ID!
9396
anotherField: Float!
@@ -102,6 +105,7 @@ directive @${name} on FIELD_DEFINITION | OBJECT
102105
type2: ${type2}
103106
type3: Type3
104107
type4: ${type4}
108+
typeNullable: TypeNullable
105109
shouldFail: ShouldFail
106110
}
107111
type Query {
@@ -128,6 +132,9 @@ directive @${name} on FIELD_DEFINITION | OBJECT
128132
anotherField
129133
yetAnotherField
130134
}
135+
typeNullable {
136+
id
137+
}
131138
shouldFail {
132139
float
133140
array
@@ -155,6 +162,9 @@ directive @${name} on FIELD_DEFINITION | OBJECT
155162
id: type4Id,
156163
yetAnotherField: 'asd',
157164
},
165+
typeNullable: {
166+
id: null,
167+
},
158168
},
159169
};
160170

@@ -184,6 +194,9 @@ directive @${name} on FIELD_DEFINITION | OBJECT
184194
id: toNodeId(type4, type4Id),
185195
yetAnotherField: rootValue.test.type4.yetAnotherField,
186196
},
197+
typeNullable: {
198+
id: null,
199+
},
187200
},
188201
},
189202
errors: [
@@ -211,6 +224,9 @@ directive @${name} on FIELD_DEFINITION | OBJECT
211224
id: '2',
212225
},
213226
type4: null,
227+
typeNullable: {
228+
id: null,
229+
},
214230
},
215231
},
216232
errors: [

lib/selfNodeId.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ export default class SelfNodeIdDirective<
2121
> extends ValidateDirectiveVisitor<{}, SelfNodeIdContext> {
2222
public getValidationForArgs(): ValidateFunction<SelfNodeIdContext> {
2323
const errorMessage = `${this.name} directive only works on strings`;
24-
return (value: unknown, _, { name }, { toNodeId }): string => {
24+
return (
25+
value: unknown,
26+
_,
27+
{ name },
28+
{ toNodeId },
29+
): string | undefined | null => {
2530
if (typeof value !== 'string') {
31+
if (value === undefined || value === null) {
32+
return value;
33+
}
2634
throw new ValidationError(errorMessage);
2735
}
2836
const encodedId = toNodeId(name, value);

0 commit comments

Comments
 (0)