Skip to content

Commit aaf9bd5

Browse files
committed
refactor .withImage to remove confusing couplings
1 parent 8d4ff2c commit aaf9bd5

File tree

5 files changed

+77
-18
lines changed

5 files changed

+77
-18
lines changed

src/graphql/explorer.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import NearText, { NearTextArgs } from './nearText';
22
import NearVector, { NearVectorArgs } from './nearVector';
3+
import NearImage, { NearImageArgs } from './nearImage';
34
import NearObject, { NearObjectArgs } from './nearObject';
45
import NearMedia, {
56
NearAudioArgs,
67
NearDepthArgs,
78
NearIMUArgs,
8-
NearImageArgs,
99
NearMediaArgs,
1010
NearMediaType,
1111
NearThermalArgs,
@@ -95,11 +95,17 @@ export default class Explorer extends CommandBase {
9595
};
9696

9797
withNearImage = (args: NearImageArgs) => {
98-
return this.withNearMedia({
99-
...args,
100-
media: args.image ? args.image : 'UNSET',
101-
type: NearMediaType.Image,
102-
});
98+
if (this.includesNearMediaFilter) {
99+
throw new Error('cannot use multiple near<Media> filters in a single query');
100+
}
101+
try {
102+
this.nearMediaString = new NearImage(args).toString();
103+
this.nearMediaType = NearMediaType.Image;
104+
this.includesNearMediaFilter = true;
105+
} catch (e: any) {
106+
this.addError(e.toString());
107+
}
108+
return this;
103109
};
104110

105111
withNearAudio = (args: NearAudioArgs) => {

src/graphql/getter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { NearObjectArgs } from './nearObject';
44
import { AskArgs } from './ask';
55
import { SortArgs } from './sort';
66
import { NearTextArgs } from './nearText';
7-
import { NearImageArgs, NearMediaType } from './nearMedia';
7+
import { NearImageArgs } from './nearImage';
88

99
test('a simple query without params', () => {
1010
const mockClient: any = {

src/graphql/getter.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import NearText, { NearTextArgs } from './nearText';
33
import NearVector, { NearVectorArgs } from './nearVector';
44
import Bm25, { Bm25Args } from './bm25';
55
import Hybrid, { HybridArgs } from './hybrid';
6+
import NearImage, { NearImageArgs } from './nearImage';
67
import NearObject, { NearObjectArgs } from './nearObject';
78
import NearMedia, {
89
NearAudioArgs,
910
NearDepthArgs,
1011
NearIMUArgs,
11-
NearImageArgs,
1212
NearMediaArgs,
1313
NearMediaType,
1414
NearThermalArgs,
@@ -159,11 +159,17 @@ export default class GraphQLGetter extends CommandBase {
159159
};
160160

161161
withNearImage = (args: NearImageArgs) => {
162-
return this.withNearMedia({
163-
...args,
164-
type: NearMediaType.Image,
165-
media: args.image ? args.image : 'UNSET',
166-
});
162+
if (this.includesNearMediaFilter) {
163+
throw new Error('cannot use multiple near<Media> filters in a single query');
164+
}
165+
try {
166+
this.nearMediaString = new NearImage(args).toString();
167+
this.nearMediaType = NearMediaType.Image;
168+
this.includesNearMediaFilter = true;
169+
} catch (e: any) {
170+
this.addError(e.toString());
171+
}
172+
return this;
167173
};
168174

169175
withNearAudio = (args: NearAudioArgs) => {

src/graphql/nearImage.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { NearMediaBase } from './nearMedia';
2+
3+
export interface NearImageArgs extends NearMediaBase {
4+
image?: string;
5+
}
6+
7+
export default class GraphQLNearImage {
8+
private certainty?: number;
9+
private distance?: number;
10+
private image?: string;
11+
12+
constructor(args: NearImageArgs) {
13+
this.certainty = args.certainty;
14+
this.distance = args.distance;
15+
this.image = args.image;
16+
}
17+
18+
toString(wrap = true) {
19+
this.validate();
20+
21+
let args: string[] = [];
22+
23+
if (this.image) {
24+
let img = this.image;
25+
if (img.startsWith('data:')) {
26+
const base64part = ';base64,';
27+
img = img.substring(img.indexOf(base64part) + base64part.length);
28+
}
29+
args = [...args, `image:${JSON.stringify(img)}`];
30+
}
31+
32+
if (this.certainty) {
33+
args = [...args, `certainty:${this.certainty}`];
34+
}
35+
36+
if (this.distance) {
37+
args = [...args, `distance:${this.distance}`];
38+
}
39+
40+
if (!wrap) {
41+
return `${args.join(',')}`;
42+
}
43+
return `{${args.join(',')}}`;
44+
}
45+
46+
validate() {
47+
if (!this.image) {
48+
throw new Error('nearImage filter: image field must be present');
49+
}
50+
}
51+
}

src/graphql/nearMedia.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface NearMediaArgs extends NearMediaBase {
77
type: NearMediaType;
88
}
99
export interface NearImageArgs extends NearMediaBase {
10-
image?: string;
10+
image: string;
1111
}
1212
export interface NearAudioArgs extends NearMediaBase {
1313
audio: string;
@@ -50,10 +50,6 @@ export default class GraphQLNearMedia {
5050
toString(wrap = true) {
5151
let args: string[] = [];
5252

53-
if (this.media === 'UNSET') {
54-
throw new Error(`near${this.type} filter: ${this.type.toLowerCase()} field must be present`);
55-
}
56-
5753
if (this.media.startsWith('data:')) {
5854
const base64part = ';base64,';
5955
this.media = this.media.substring(this.media.indexOf(base64part) + base64part.length);

0 commit comments

Comments
 (0)