2024-05-07
In Programming we always discover new ways of doing things. It can either be an upgrade version of a software , phasing out of techniques to the introduction of better ones or a new concept overall.
Blog post by MF Mabala
Migrating GraphQL code to GraphQL Pothos.
Each an every day developers find new methods of doing things. Being a web developer means continuously figuring out the best methods to use in order to make our applications as effective as they can be. In this blog we will explore GraphQL pothos.
Pothos
GraphQL Pothos- is a plugin based GraphQL schema builder for typescript.
Migrating code would mean that you have a pre existing code that you want to move from one form/ library to another . In this blog I will show an example where I had a pre defined base model and I will be migrating it to GraphQL pothos. Below I have a pre existing GraphQL code that has types explained from it I will define a pothos schema and the object types. Lets start with an enum type.
enum DebitCycle {
MONTHLY
QUARTERLY
BIANUALLY
ANNUALY
}
Starting with all the enum we will have to describe the schema using pothos and this is how you do it.
export const DebitCycleSchema = builder.enumType(DebitCycle, {
name: 'DebitCycle',
description: 'A representation of cycles a user can be debited on',
});
The above is the structure of how to define an enum type.
1. We gave the enum a name DebitCycleSchema.
2. builder.enumtype is a pothos merhod of defining enum types.
3.The bracketed DebitCycle is the base model and it is imported from where it initially exists.
4. We give the schema a description and name.
Explaining the object type and creating a schema.
Schema- can be defined as a contract between the server and the client. You can look at what the difference is between the client and server on the internet.
Below is a pre existing GraphQL type for a Plan. It could be anything that you are working on
type Plan {
id: ID!
tenantId: ID!
code: String!
name: String!
price: Int!
debitCycle: DebitCycle!
subscribers: [Subscribers]!
}
With the above code I will be able to write a pothos schema. We will be using an object type to build the schema. The sign (!) is an indication that the field is a requirement and when defining the pothos schema we will set nullable to false for those fields . See example below
export const PlanSchema = builder.objectType(Plan, {
name: 'Plan',
description: 'A template for a subscription',
fields: (t) => ({
id: t.exposeID('id'),
tenantId: t.exposeID('tenantId'),
code: t.exposeString('code', { nullable: false }),
name: t.exposeString('name', { nullable: false }),
price: t.exposeInt('price', { nullable: false }),
debitCycle: t.expose('debitCycle', { type: DebitCycleSchema, nullable: false }),
subscribers: t.expose('subscribers', { type: [Subscriber] }),
}),
});
The above schema explained
1. Give it a name according to its function in the above case it is a PlanSchema.
2. We will use builder.objectType because it is an object.
3. The 'Plan' following number 2 is the base model and it is imported accordingly
4. Set the name and description.
5 Defining the fields.
In GraphQL there are default types which are :string, int, boolean float and ID. When setting the fields we want to expose them to their type. Lets look at field 'code' it is of type string the we will expose it to string. You do the same with the remaining fields. If you look at the type Plan you would see that ID and tenantId are required but when we define the pothos schema we are not setting nullable to false. This is because Id by default is required in GraphQL and there is no need to further specify that.
In the next blog I will cover migrating GraphQL queries and mutations to pothos. I will attach a link of where you can learn about pothos.
Thank you for reading my blog