Skip to content

Commit

Permalink
feat: Adds errors response as promise rejection
Browse files Browse the repository at this point in the history
  • Loading branch information
Roboroads committed Jan 23, 2022
1 parent 92804d1 commit 8c1296d
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 10 deletions.
6 changes: 5 additions & 1 deletion example/graphtonGeneratedJavascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ class GraphtonBaseQuery {
...requestOptions?.headers
},
});
return {
const returnData = {
...response.data,
response
};
if (returnData.errors) {
return Promise.reject(returnData);
}
return returnData;
}
}
class GraphtonBaseReturnTypeBuilder {
Expand Down
9 changes: 8 additions & 1 deletion example/graphtonGeneratedTypescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface QueryResponse {
[key: string]: unknown,
data: Record<string, unknown>,
extensions?: Record<string, unknown>,
errors?: Record<string, unknown>[],
response: AxiosResponse
}
export interface ReturnTypeInfo {
Expand Down Expand Up @@ -102,10 +103,16 @@ abstract class GraphtonBaseQuery<T> {
},
});

return {
const returnData = {
...response.data,
response
};

if(returnData.errors) {
return Promise.reject(returnData);
}

return returnData;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/stubs/GraphtonBaseQuery.stub.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { GraphtonBaseReturnTypeBuilder } from './GraphtonBaseReturnTypeBuilder.s
import { QueryResponse, RequestOptions } from './GraphtonTypes.stub';
export declare type RootType = 'query' | 'mutation' | 'subscription' | '/*ROOTTYPE*/';
export declare abstract class GraphtonBaseQuery<T> {
protected abstract queryName: string;
protected abstract rootType: RootType;
abstract readonly queryName: string;
abstract readonly rootType: RootType;
protected abstract returnType: GraphtonBaseReturnTypeBuilder<any, any> | null;
abstract setArgs(queryArgs: Partial<T>): void;
protected abstract toArgString(): string;
Expand Down
6 changes: 5 additions & 1 deletion src/stubs/GraphtonBaseQuery.stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ import axios from 'axios';
...requestOptions?.headers
},
});
return {
const returnData = {
...response.data,
response
};
if (returnData.errors) {
return Promise.reject(returnData);
}
return returnData;
}
}
8 changes: 7 additions & 1 deletion src/stubs/GraphtonBaseQuery.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ import axios from 'axios';
},
});

return {
const returnData = {
...response.data,
response
};

if(returnData.errors) {
return Promise.reject(returnData);
}

return returnData;
}
}
11 changes: 8 additions & 3 deletions src/stubs/GraphtonQueryTypeInterfaces.stub.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { RequestOptions } from './GraphtonTypes.stub';
export interface GraphtonQuery<T> {
import { RootType } from './GraphtonBaseQuery.stub';
export interface GraphtonQueryBuilder {
queryName: string;
rootType: RootType;
}
export interface GraphtonQuery<T> extends GraphtonQueryBuilder {
execute(requestOptions?: RequestOptions): Promise<T>;
}
export interface GraphtonMutation<T> {
export interface GraphtonMutation<T> extends GraphtonQueryBuilder {
execute(requestOptions?: RequestOptions): Promise<T>;
}
export interface GraphtonSubscription<T> {
export interface GraphtonSubscription<T> extends GraphtonQueryBuilder {
execute(requestOptions?: RequestOptions): Promise<T>;
}
1 change: 1 addition & 0 deletions src/stubs/GraphtonTypes.stub.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface QueryResponse {
[key: string]: unknown;
data: Record<string, unknown>;
extensions?: Record<string, unknown>;
errors?: Record<string, unknown>[];
response: AxiosResponse;
}
export interface ReturnTypeInfo {
Expand Down
1 change: 1 addition & 0 deletions src/stubs/GraphtonTypes.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface QueryResponse {
[key: string]: unknown,
data: Record<string, unknown>,
extensions?: Record<string, unknown>,
errors?: Record<string, unknown>[],
response: AxiosResponse
}
export interface ReturnTypeInfo {
Expand Down
2 changes: 1 addition & 1 deletion src/stubs/Query.stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { _t_TYPENAME_t_ReturnTypeBuilder as _t_RETURNTYPEBUILDER_t_ } from './Re
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class _t_QUERYCLASSNAME_t_ extends GraphtonBaseQuery /*IF:ADDEXECUTOR*/ /*!implements!*/ /*IMPLEMENTS*/ /*ENDIF:ADDEXECUTOR*/ {
queryName = '/*QUERYNAME*/';
queryArgs = {};
rootType = '/*ROOTTYPE*/';
queryArgs = {};
returnType = /*IF:RETURNTYPEOBJECT*/ new /*ENDIF:RETURNTYPEOBJECT*/ _t_RETURNTYPEBUILDER_t_ /*IF:RETURNTYPEOBJECT*/() /*ENDIF:RETURNTYPEOBJECT*/;
/*IF:ARGUMENTS*/
constructor(queryArgs) {
Expand Down

0 comments on commit 8c1296d

Please sign in to comment.