mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-04-24 16:00:53 +08:00
fix(engine): Fix Request action response.
Response included full GraphQL response.
This commit is contained in:
parent
5ccd272715
commit
45aaa18545
packages/engine
@ -74,14 +74,14 @@ class Requests {
|
||||
return this.fetch({ requestId, event, arrayIndices });
|
||||
}
|
||||
|
||||
fetch({ requestId, event, arrayIndices }) {
|
||||
async fetch({ requestId, event, arrayIndices }) {
|
||||
this.context.requests[requestId].loading = true;
|
||||
if (this.context.RootBlocks) {
|
||||
this.context.RootBlocks.setBlocksLoadingCache();
|
||||
}
|
||||
|
||||
return this.context.lowdefy.client
|
||||
.query({
|
||||
try {
|
||||
const gqlResponse = await this.context.lowdefy.client.query({
|
||||
query: CALL_REQUEST,
|
||||
fetchPolicy: 'network-only',
|
||||
variables: {
|
||||
@ -97,24 +97,22 @@ class Requests {
|
||||
urlQuery: serializer.serialize(this.context.lowdefy.urlQuery),
|
||||
},
|
||||
},
|
||||
})
|
||||
.then((result) => {
|
||||
this.context.requests[requestId].response = serializer.deserialize(
|
||||
get(result, 'data.request.response', {
|
||||
default: null,
|
||||
})
|
||||
);
|
||||
this.context.requests[requestId].error.unshift(null);
|
||||
return result;
|
||||
})
|
||||
.catch((error) => {
|
||||
this.context.requests[requestId].error.unshift(error);
|
||||
throw error;
|
||||
})
|
||||
.finally(() => {
|
||||
this.context.requests[requestId].loading = false;
|
||||
this.context.update();
|
||||
});
|
||||
const response = serializer.deserialize(
|
||||
get(gqlResponse, 'data.request.response', {
|
||||
default: null,
|
||||
})
|
||||
);
|
||||
this.context.requests[requestId].response = response;
|
||||
this.context.requests[requestId].loading = false;
|
||||
this.context.update();
|
||||
return response;
|
||||
} catch (error) {
|
||||
this.context.requests[requestId].error.unshift(error);
|
||||
this.context.requests[requestId].loading = false;
|
||||
this.context.update();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -132,12 +132,28 @@ test('Request call one request', async () => {
|
||||
loading: true,
|
||||
response: null,
|
||||
});
|
||||
await promise;
|
||||
const res = await promise;
|
||||
expect(context.requests.req_one).toEqual({
|
||||
error: [null],
|
||||
error: [],
|
||||
loading: false,
|
||||
response: 1,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
blockId: 'button',
|
||||
event: undefined,
|
||||
eventName: 'onClick',
|
||||
responses: [
|
||||
{
|
||||
actionId: 'a',
|
||||
actionType: 'Request',
|
||||
response: [1],
|
||||
},
|
||||
],
|
||||
success: true,
|
||||
timestamp: {
|
||||
date: 0,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('Request call all requests', async () => {
|
||||
@ -190,19 +206,35 @@ test('Request call all requests', async () => {
|
||||
response: null,
|
||||
},
|
||||
});
|
||||
await promise;
|
||||
const res = await promise;
|
||||
expect(context.requests).toEqual({
|
||||
req_one: {
|
||||
error: [null],
|
||||
error: [],
|
||||
loading: false,
|
||||
response: 1,
|
||||
},
|
||||
req_two: {
|
||||
error: [null],
|
||||
error: [],
|
||||
loading: false,
|
||||
response: 2,
|
||||
},
|
||||
});
|
||||
expect(res).toEqual({
|
||||
blockId: 'button',
|
||||
event: undefined,
|
||||
eventName: 'onClick',
|
||||
responses: [
|
||||
{
|
||||
actionId: 'a',
|
||||
actionType: 'Request',
|
||||
response: [1, 2],
|
||||
},
|
||||
],
|
||||
success: true,
|
||||
timestamp: {
|
||||
date: 0,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('Request call array of requests', async () => {
|
||||
@ -255,19 +287,35 @@ test('Request call array of requests', async () => {
|
||||
response: null,
|
||||
},
|
||||
});
|
||||
await promise;
|
||||
const res = await promise;
|
||||
expect(context.requests).toEqual({
|
||||
req_one: {
|
||||
error: [null],
|
||||
error: [],
|
||||
loading: false,
|
||||
response: 1,
|
||||
},
|
||||
req_two: {
|
||||
error: [null],
|
||||
error: [],
|
||||
loading: false,
|
||||
response: 2,
|
||||
},
|
||||
});
|
||||
expect(res).toEqual({
|
||||
blockId: 'button',
|
||||
event: undefined,
|
||||
eventName: 'onClick',
|
||||
responses: [
|
||||
{
|
||||
actionId: 'a',
|
||||
actionType: 'Request',
|
||||
response: [1, 2],
|
||||
},
|
||||
],
|
||||
success: true,
|
||||
timestamp: {
|
||||
date: 0,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('Request pass if params are none', async () => {
|
||||
|
@ -93,7 +93,7 @@ test('callRequest', async () => {
|
||||
await context.Requests.callRequest({ requestId: 'req_one' });
|
||||
expect(context.requests).toEqual({
|
||||
req_one: {
|
||||
error: [null],
|
||||
error: [],
|
||||
loading: false,
|
||||
response: 1,
|
||||
},
|
||||
@ -156,7 +156,7 @@ test('callRequests all requests', async () => {
|
||||
}
|
||||
expect(context.requests).toEqual({
|
||||
req_one: {
|
||||
error: [null],
|
||||
error: [],
|
||||
loading: false,
|
||||
response: 1,
|
||||
},
|
||||
@ -166,7 +166,7 @@ test('callRequests all requests', async () => {
|
||||
response: null,
|
||||
},
|
||||
req_watch: {
|
||||
error: [null],
|
||||
error: [],
|
||||
loading: false,
|
||||
response: 2,
|
||||
},
|
||||
@ -190,7 +190,7 @@ test('callRequests', async () => {
|
||||
await promise;
|
||||
expect(context.requests).toEqual({
|
||||
req_one: {
|
||||
error: [null],
|
||||
error: [],
|
||||
loading: false,
|
||||
response: 1,
|
||||
},
|
||||
@ -269,6 +269,21 @@ test('update function should be called', async () => {
|
||||
expect(updateFunction).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('update function should be called if error', async () => {
|
||||
const updateFunction = jest.fn();
|
||||
const context = testContext({
|
||||
lowdefy,
|
||||
rootBlock,
|
||||
});
|
||||
context.update = updateFunction;
|
||||
try {
|
||||
await context.Requests.callRequest({ requestId: 'req_error' });
|
||||
} catch (e) {
|
||||
// catch thrown errors
|
||||
}
|
||||
expect(updateFunction).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('fetch should set blocks loading and call query every time it is called', async () => {
|
||||
const context = testContext({
|
||||
lowdefy,
|
||||
|
Loading…
x
Reference in New Issue
Block a user