Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config and a basic token handling structure. #7

Open
wants to merge 1 commit into
base: connect-uhn-test-server
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const config = {
serverUrl: 'http://hapi.fhir.org/baseDstu3/'
}

export default config
26 changes: 20 additions & 6 deletions connector.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import fetch from 'node-fetch'
import config from './config'

// Define your fhir-hapi server url
const serverUrl = 'http://hapi.fhir.org/baseDstu3/'

const serverUrl = config.serverUrl
const headers = ({
token
}) => {
let result = {}
if (token) {
result.Authorization = 'Bearer ' + token
}
return result
}
// Allow reducers to call for a resource type, and pass url search parameters
const Fhir = {
getAll({ resource, searchParams }) {
getAll({ resource, searchParams, token }) {
const url = serverUrl + resource + (searchParams ? '?' + searchParams : '')
return fetch(url)
return fetch(url, {
headers: headers({ token })
})
.then(res => res.json())
.catch(err => console.log(err))
},
getOne({ resource, id, searchParams }) {
getOne({ resource, id, searchParams, token }) {
const url = serverUrl + resource + '/' + id + (searchParams ? '?' + searchParams : '')
return fetch(url)
return fetch(url, {
headers: headers({ token })
})
.then(res => res.json())
}
}
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ const app = express();
const baseName = process.env.FHIR_GRAPHQL_BASENAME || '';

// bodyParser is needed just for POST.
app.use(baseName + '/graphql', bodyParser.json(), graphqlExpress({ schema }));
app.use(baseName + '/graphql', bodyParser.json(), graphqlExpress(req => ({
schema ,
context: {
token: req.headers.authorization
}
})));

app.get(baseName + '/graphiql', graphiqlExpress({ endpointURL: baseName + '/graphql' })); // if you want GraphiQL enabled

console.log('listening on port ', PORT)
console.log('View your fhir graphql server at http://localhost:' + PORT + (baseName ? '/'+baseName : '') + '/graphiql')
app.listen(PORT);
10 changes: 8 additions & 2 deletions resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ const questionnaireResponse = {

const resolvers = {
Query: {
questionnaireResponses() {
return Fhir.getAll({ resource: 'QuestionnaireResponse' }).then(res => {
questionnaireResponses(root, args, context) {
let request = {
resource: 'QuestionnaireResponse'
}
if (context.token) {
request.token = context.token
}
return Fhir.getAll(request).then(res => {
// Extract resource out of the bundle.
const result = res.entry.map(entry => entry.resource)
return result;
Expand Down