Skip to content

Commit

Permalink
Create queries to return the complete report and the an analysis by id
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasAndrad committed Jun 22, 2019
1 parent 33e1601 commit 4490526
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ function down (knex) {
export {
up,
down
}
}
2 changes: 1 addition & 1 deletion migrations/20190525232550_add_cable_to_report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ function down (knex) {
export {
up,
down
}
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function main () {
console.log('FTP server is running on ftp://localhost:30003')
break
} catch (err) {
console.err('Error on creating server.', err)
console.log('Error on creating server.', err)
console.log('Retrying connection...')
sleep(1000)
}
Expand Down
9 changes: 9 additions & 0 deletions src/queries/analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,14 @@ export default {
} catch (err) {
throw new Error(err.message)
}
},
analysis: async (root, { id }, context, info) => {
try {
let analysis = await knexInstance('analysis')
.where({ id })
return analysis[0]
} catch (err) {
throw new Error(err.message)
}
}
}
16 changes: 15 additions & 1 deletion src/queries/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default {
try {
let report = await knexInstance('reports')
.where({ id })

return report[0]
} catch (err) {
throw new Error(err.message)
Expand All @@ -21,5 +20,20 @@ export default {
} catch (err) {
throw new Error(err.message)
}
},
reportComplete: async (root, { id }, context, info) => {
try {
let reportComplete = await knexInstance('reports')
.select('reports.*')
.select(knexInstance.raw('json_agg(analysis.*) as analysis'))
.leftJoin('analysis', 'analysis.report_id', 'reports.id')
.where('reports.id', id)
.groupBy('reports.id')
.first()

return reportComplete
} catch (err) {
throw new Error(err.message)
}
}
}
3 changes: 3 additions & 0 deletions src/type-defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Report {
created_at: DateTime
updated_at: DateTime
cable_id: ID
analysis: [Analysis]
}
type Analysis {
Expand All @@ -46,11 +47,13 @@ scalar Upload
# Queries
type Query {
analysis (id: ID!): Analysis
cable (id: ID!): Cable
cables: [Cable]
report (id: ID!): Report
reports (cableId: ID!): [Report]
reportAnalysis(reportId: ID!): [Analysis]
reportComplete (id: ID!): Report
}
# Mutations
Expand Down

0 comments on commit 4490526

Please sign in to comment.