@enteral/fhir-schema
Version:
FHIR Schema for enteral.io
244 lines (229 loc) • 7.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeResolvers = exports.typeDefs = void 0;
const fetch_1 = require("./fetch");
exports.typeDefs = `
type ListInfo {
totalCount: Int
"""
Pass the value of this as \`after\` to the relevant field in order to
fetch the next page of results.
"""
nextPageToken: String
}
type PatientList {
edges: [Patient]
nodes: [Patient]
listInfo: ListInfo
}
type ObservationList {
edges: [Observation]
nodes: [Observation]
listInfo: ListInfo
}
type AllergyIntoleranceList {
edges: [Observation]
nodes: [AllergyIntolerance]
listInfo: ListInfo
}
type NutritionOrderList {
edges: [Observation]
nodes: [NutritionOrder]
listInfo: ListInfo
}
enum PatientOrderBy {
ID_DESC
ID_ASC
BIRTHDATE_DESC
BIRTHDATE_ASC
}
type Query {
allPatients(
"""
The value of \`nextPageToken\` from a list such that the next page
may be fetched.
"""
after: String
first: Int
offset: Int
orderBy: PatientOrderBy
searchTerms: String
): PatientList
allNutritionOrders(
"""
The value of \`nextPageToken\` from a list such that the next page
may be fetched.
"""
after: String
): NutritionOrderList
allObservations(
"""
The value of \`nextPageToken\` from a list such that the next page
may be fetched.
"""
after: String
): ObservationList
allAllergyIntoleranceLists(
"""
The value of \`nextPageToken\` from a list such that the next page
may be fetched.
"""
after: String
): AllergyIntoleranceList
fhirPatientById(id: String!): Patient
fhirNutritionOrderById(id: String!): NutritionOrder
fhirObservationById(id: String!): Observation
fhirAllergyIntoleranceById(id: String!): AllergyIntolerance
fhirNutritionOrdersByPatientId(id: String!): NutritionOrderList
fhirObservationsByPatientId(id: String!): ObservationList
fhirAllergyIntolerancesByPatientId(id: String!): AllergyIntoleranceList
}
extend type Patient {
allergyIntolerances(
"""
The value of \`nextPageToken\` from a list such that the next page
may be fetched.
"""
after: String
): AllergyIntoleranceList
observations(
"""
The value of \`nextPageToken\` from a list such that the next page
may be fetched.
"""
after: String
): ObservationList
nutritionOrders(
"""
The value of \`nextPageToken\` from a list such that the next page
may be fetched.
"""
after: String
): NutritionOrderList
}
`;
function makeResolvers(options) {
const { fetchFhirList, fetchFhirSingle, makeFhirMutation } = fetch_1.makeFetchers(options);
return {
Query: {
async allPatients(_, args) {
const { searchTerms } = args;
// http://hl7.org/fhir/search.html
// http://hl7.org/fhir/patient.html#search
if (searchTerms) {
return await fetchFhirList(`/Patient/_search?name=${encodeURIComponent(searchTerms)}`, args);
}
else {
return await fetchFhirList("/Patient", args);
}
},
async fhirPatientById(_, args) {
const { id } = args;
//validateFhirId(id);
return await fetchFhirSingle(`/Patient/${id}`);
},
async allObservations(_, args) {
return await fetchFhirList("/Observation", args);
},
async allNutritionOrders(_, args) {
return await fetchFhirList("/NutritionOrder", args);
},
async allAllergyIntoleranceLists(_, args) {
return await fetchFhirList("/AllergyIntolerance", args);
},
async fhirNutritionOrdersByPatientId(_, args) {
const { id } = args;
return await fetchFhirList(`/NutritionOrder?patient=${id}`, args);
},
async fhirObservationsByPatientId(_, args) {
const { id } = args;
return await fetchFhirList(`/Observation?patient=${id}`, args);
},
async fhirAllergyIntolerancesByPatientId(_, args) {
const { id } = args;
return await fetchFhirList(`/AllergyIntolerance?patient=${id}`, args);
},
async fhirNutritionOrderById(_, args) {
const { id } = args;
console.log("id", id);
console.log("args", args);
return await fetchFhirSingle(`/NutritionOrder/${id}`);
},
async fhirObservationById(_, args) {
const { id } = args;
return await fetchFhirSingle(`/Observation/${id}`);
},
async fhirAllergyIntoleranceById(_, args) {
const { id } = args;
return await fetchFhirSingle(`/AllergyIntolerance/${id}`);
},
},
/*Mutation: {
createNutritionOrder: makeFhirMutation(
"create",
"/NutritionOrder",
(nutritionOrder) => {
return {
patient: referenceFromPatientId(nutritionOrder.patient),
};
}
),
updateNutritionOrder: makeFhirMutation(
"update",
"/NutritionOrder",
(nutritionOrder) => {
return {
patient: referenceFromPatientId(nutritionOrder.patient),
};
}
),
deleteNutritionOrder: makeFhirMutation("delete", "/NutritionOrder"),
},*/
Patient: {
async observations(patient, args) {
const path = `/Observation?patient=${patient.id}`;
return await fetchFhirList(path, args);
},
async allergyIntolerances(patient, args) {
const path = `/AllergyIntolerance?patient=${patient.id}`;
return await fetchFhirList(path, args);
},
async nutritionOrders(patient, args) {
const path = `/NutritionOrder?patient=${patient.id}`;
return await fetchFhirList(path, args);
},
},
NutritionOrder: {
async patient(order) {
return await fetchFhirSingle(`/Patient/${patientIdFromReference(order.patient)}`);
},
},
NutritionOrderpatient_patient_Union: {
__resolveType(record) {
if (record.resourceType === "Patient") {
return "Patient";
}
else {
return null;
}
},
},
};
function referenceFromPatientId(patientId) {
if (typeof patientId === "object") {
// Already of the right format?
return patientId;
}
return {
reference: `/Patient/${patientId}`,
type: `Patient`,
};
}
function patientIdFromReference(ref) {
if (ref && ref.reference) {
return ref.reference.replace(/^Patient/, "");
}
throw new Error(`Reference '${JSON.stringify(ref)}' not understood`);
}
}
exports.makeResolvers = makeResolvers;