Skip to content

Commit

Permalink
added new GraphQL findTask method; hooked LM reports to it
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelchadwick committed Nov 21, 2024
1 parent 73e4f22 commit 55dc4d0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default class ReportsSubjectLearningMaterialComponent extends Component {
@service graphql;
@service intl;

controller = new AbortController();
signal = this.controller.signal;

@cached
get data() {
return new TrackedAsyncData(
Expand Down Expand Up @@ -58,7 +61,19 @@ export default class ReportsSubjectLearningMaterialComponent extends Component {
prepositionalObjectTableRowId,
school,
);
const result = await this.graphql.find('learningMaterials', filters, 'id, title');

if (this.graphql.findTask.isRunning) {
this.controller.abort('running query canceled so new one could run');
}
this.controller = new AbortController();
this.signal = this.controller.signal;

const result = await this.graphql.findTask.perform(
'learningMaterials',
filters,
'id, title',
this.signal,
);
return result.data.learningMaterials.map(({ title }) => title);
}

Expand Down
38 changes: 38 additions & 0 deletions packages/frontend/app/services/graphql.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Service, { service } from '@ember/service';
import { task } from 'ember-concurrency';

export default class GraphqlService extends Service {
@service session;
Expand Down Expand Up @@ -39,4 +40,41 @@ export default class GraphqlService extends Service {
const filterString = filters.length ? '(' + filters.join(', ') + ')' : '';
return this.#query(`query { ${endpoint}${filterString} { ${attributes} } }`);
}

findTask = task(async (endpoint, filters, attributes, signal) => {
const filterString = filters.length ? '(' + filters.join(', ') + ')' : '';
const url = `${this.host}/api/graphql`;
const headers = this.authHeaders;
headers['Content-Type'] = 'application/json';
headers['Accept'] = 'application/json';
const q = `query { ${endpoint}${filterString} { ${attributes} } }`;

let response = null;
try {
response = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify({ query: q }),
signal,
});

return response.json();
} catch (e) {
if (signal.aborted) {
if (signal.reason) {
console.log(`graphql canceled with reason: ${signal.reason}`);
} else {
console.warn('graphql canceled but no reason was given.');
}
} else {
console.error('graphql failed due to unknown error', e);
}

return {
error: 'bad stuff',
};
} finally {
console.log('graphql completed');
}
});
}

0 comments on commit 55dc4d0

Please sign in to comment.