You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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:
Type definitions - this tells GraphQL what data it is going to serve.
Resolvers - this tells GraphQL how to actually get the data. Each resolver would probably call a function to get data from the source (TypeORM, Sequelize, db2Model)
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 and EMPLOYEE. This means I will create two models for GraphQL:
Where we reference a type, we can reference other GraphQL models
When a type or model is wrapped with square brackets, that indicates an array will be returned
A property with an exclamation mark means it cannot be null; properties are nullable by default
In a GraphQL schema, the root basically calls your JavaScript resolvers and we also have to define those in the type definition:
typeRootQuery {
# Fetches a single departmentDepartment(id: ID!): Department # Fetches all departmentsDepartments: [Department]
# Fetches a single employeeEmployee(id: ID!): Employee
# Fetches all employeesEmployees: [Employee]
}
schema {
query: RootQuery
}
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.
RootQuery: {Department(root,{ id },context){returnDepartment.Get(id);},Departments(root,args,context){returnDepartment.Find();},Employee(root,{ id },context){returnEmployee.Get(id);},Employees(root,args,context){returnEmployee.Find();}}
As you can see, I am using methods like Get and Find. 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!
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: Employee
employees: [Employee]
managerObject will return an Employee model based on the manager column and Employee 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:
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:
The text was updated successfully, but these errors were encountered: