Open worksofliam opened 4 years ago
A property without an exclamation mark means it could return as null
Sounds almost double negative to me. Took me a bit to parse.
Perhaps flip it: "A property with an exclamation mark means it cannot be null; properties are nullable by default."
Go check out the updated version of this post.
My friend Connor has been using GraphQL for some time and always tells me how good it is. GraphQL seems very popular over Mongo and Postgres. I thought it was about time I finally write an example that uses Db2 for i to get data. I am happy to say GraphQL makes serving data just that little bit easier. If you want to learn more about GraphQL before reading on, here are some links:
Database
For this post, I will be using the Db2 for i sample tables, which you can generate on your own systems. Read more about that here.
Pieces to GraphQL
There are two pieces that matter in my example:
Of course, there also needs to be a part that connects to the database - but that is separate from GraphQL. The point of resolvers is that data can be gathered from anywhere. Note: This post does not solve the n+1 problem. There will be another post for that.
Type definitions
For this example, I will be using two tables from the sample schema:
DEPARTMENT
andEMPLOYEE
. This means I will create two models for GraphQL: Notes from this:
In a GraphQL schema, the root basically calls your JavaScript resolvers and we also have to define those in the type definition:
Resolvers
For each of the definitions in the
RootQuery
, we need to create resolvers for. This is the part that would connect to the data source to fetch data and in this case, I am using Db2 for i. As you can see, I am using methods like
Get
andFind
. I didn't write these manually and they were generated using db2Model. db2Model is a tool to generate classes based on Db2 for i tables, creating all the methods for CRUD actions. This is usually where you would use something like TypeORM or Sequelize too. What's important to note in this example is that the properties returning from your sources must match the properties you defined in your GQL type defintions. Now, to make a web API out of this, we just need to setup our launch script with the relavent info: Not only can we send queries to
/graphql
now, but running on/graphiql
is a useful UI tool to help you write queries. Now that we have outlined all our models and connected them up to data sources, we can send queries to our endpoint. To get a list of Departments, we would just send the following - but don't forget, because it's GraphQL, we also have to specify the properties we want!
some items omitted
If we wanted to get an Employee by their ID:
We can also call multiple resolvers in one query:
Model relationships
There may be instances where you want to get a list of items based on another piece of data. In my definiton for Department, I had these two properties:
managerObject
will return an Employee model based on the manager column andEmployee
will return a list of Employees' in that Department. In our resolvers, we can specify what these property need to do to get this information inside of the Employee model: Which means in our queries we can now get this information in one request: