exercise graphql, fhir, and javascript · graphql is, how it works with fhir, and how to get...

4
Organized by HL7®, FHIR® and the flame Design mark are the registered trademarks of Health Level Seven International and are used with permission. Exercise GraphQL, FHIR, and JavaScript Track lead: Robert Winterbottom ([email protected]) During the hands-on session of the GraphQL, FHIR, and JavaScript tutorial, you will learn at a basic level what GraphQL is, how it works with FHIR, and how to get started with GraphQL, FHIR, and JavaScript. After completing this tutorial, you will be able to: Load the Graphiql explorer in your browser to explore DSTU2 and STU3 fhir schemas and documentation as well as test GraphQL queries. Write your own resolvers to return real data through our GraphQL endpoint Getting started with GraphQL and Express Requirements You should have the following installed: Node.js & NPM (https://nodejs.org) Git (https://git-scm.com) (Optional) Yarn package manager (https://yarnpkg.com) Exercise In this exercise we will be cloning Asymmetrik’s open source grahpql-fhir repository, installing all of its dependencies, and getting the server up and running. Upon completion, you will be able to view the Graphiql explorer at http://localhost:3000/3_0_1/$graphiql. 1. Open up Terminal of Command Prompt, and clone the repository by running git clone https://github.com/Asymmetrik/graphql-fhir.git. This will create a graphql-fhir directory on your machine. Navigate to this directory by running cd graphql-fhir. 2. Install all the project dependencies by running npm install or yarn install, depending on which package manager you have installed. 3. Start the server in development mode by running npm run nodemon or yarn nodemon. 4. Open http://localhost:3000/3_0_1/$graphiql in your browser. Feel free to play around with the application and explore the documentation panel on the right hand side. 5. Copy the query on the next page and paste it into the query section (left most section of the UI) and hit the execute query button in the top left (looks like a triangle pointing to the right).

Upload: others

Post on 23-May-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Organized by

HL7®, FHIR® and the flame Design mark are the registered trademarks of Health Level Seven International and are used with permission.

Exercise GraphQL, FHIR, and JavaScript Track lead: Robert Winterbottom ([email protected]) During the hands-on session of the GraphQL, FHIR, and JavaScript tutorial, you will learn at a basic level what GraphQL is, how it works with FHIR, and how to get started with GraphQL, FHIR, and JavaScript. After completing this tutorial, you will be able to:

● Load the Graphiql explorer in your browser to explore DSTU2 and STU3 fhir schemas and documentation as well as test GraphQL queries.

● Write your own resolvers to return real data through our GraphQL endpoint

Getting started with GraphQL and Express

Requirements

You should have the following installed:

● Node.js & NPM (https://nodejs.org) ● Git (https://git-scm.com) ● (Optional) Yarn package manager (https://yarnpkg.com)

Exercise

In this exercise we will be cloning Asymmetrik’s open source grahpql-fhir repository, installing all of its dependencies, and getting the server up and running. Upon completion, you will be able to view the Graphiql explorer at http://localhost:3000/3_0_1/$graphiql.

1. Open up Terminal of Command Prompt, and clone the repository by running git clone

https://github.com/Asymmetrik/graphql-fhir.git. This will create a graphql-fhir

directory on your machine. Navigate to this directory by running cd graphql-fhir.

2. Install all the project dependencies by running npm install or yarn install, depending on which package manager you have installed.

3. Start the server in development mode by running npm run nodemon or yarn nodemon. 4. Open http://localhost:3000/3_0_1/$graphiql in your browser. Feel free to play around with the

application and explore the documentation panel on the right hand side. 5. Copy the query on the next page and paste it into the query section (left most section of the UI) and

hit the execute query button in the top left (looks like a triangle pointing to the right).

HL7®, FHIR® and the flame Design mark are the registered trademarks of Health Level Seven International and are used with permission.

query {

Patient {

id

name {

given

}

}

}

Your Graphiql explorer should now look like this:

Congratulations, you have a GraphQL FHIR server up and running and are able to run queries. However, there is no data. By default, our resolvers return an empty object. They are essentially placeholders until we add some code to retrieve actual data. So let’s do just that in this next exercise.

Writing your first resolver

Requirements

You should have completed the Getting started with GraphQL and Express exercise from above.

Exercise

For this exercise, we are going to update the current patient resolver to return actual FHIR data from a patient-example.json file that we are going to create. We will use this file as our mock backend, a real service will need

HL7®, FHIR® and the flame Design mark are the registered trademarks of Health Level Seven International and are used with permission.

to connect to a database and run some queries. Please see our FAQ section here, https://github.com/Asymmetrik/graphql-fhir#frequently-asked-questions, for information on how to connect to a database and many other common tasks.

1. Start your server with npm run nodemon or yarn nodemon. You can skip this step if your server is still up and running from the first exercise.

2. Open the patient resolver in your editor of choice. The patient resolver will be located at src/resources/3_0_1/profiles/patient/resolver.js. The code we will be

replacing is located in this file, and the function is called patientResolver. 3. Add the following resolver logic:

module.exports.patientResolver = function patientResolver (root, args, context, info) {

let { server, req, res, version } = context;

let patient = {};

let id = args._id;

try {

patient = require(`./patient-${id}.json`);

} catch (err) {

server.logger.error(err);

let error = errorUtils.notFound(version, err.message);

return errorUtils.formatErrorForGraphQL(error);

}

return patient;

};

4. For the code above to work, we will need to add a patient-example.json file. Create a

patient-example.json in the same directory as the patient resolver from above. The path

should be src/resources/3_0_1/profiles/patient/patient-example.json. Copy

the JSON from https://www.hl7.org/fhir/patient-example.json and paste it into your patient-

example.json file. 5. Open up the Graphiql explorer and let’s run a new query.

query {

Patient(_id:"example") {

id

name {

given

}

}

}

Your Graphiql explorer should now look like this:

HL7®, FHIR® and the flame Design mark are the registered trademarks of Health Level Seven International and are used with permission.

Congratulations, you have written your first resolver for this server. If you would like to learn more, please check out our FAQ, https://github.com/Asymmetrik/graphql-fhir#frequently-asked-questions, for some instructions on some common next steps or come ask any of us from the Asymmetrik team. We are very responsive to issues on Github, https://github.com/Asymmetrik/graphql-fhir/issues, so if you have questions or comments, please feel free to file an issue there as well. The open source repository for this code can be found at: https://github.com/Asymmetrik/graphql-fhir Have fun, and remember to ask for help if you get stuck!