TypeDefs, Resolvers and Mutations

lovertab

Lovertab

Posted on May 18, 2024

TypeDefs, Resolvers and Mutations

TypeDefs define the schema and structure of your GraphQL API. They specify the types of data you can query or mutate and the operations available.
Image description

In this example, I have five different types:
User, which represents a user with the fields _id, username, email, and gameData.
Auth, which represents authentication information with a token and user.
GameData, which represents game data with fields _id, food, energy, happiness, name, createdDate, lastSaveDate, and timeAlive
Queries:
me which help fetches a user by their ID and
users which fetches a list of all users.
Next we have mutations:
addUser, login, createGameData, deleteUser, deleteGameData, and updateGameData.

Resolvers implement the logic for the operations defined in the schema. They fetch and manipulate data according to the queries and mutations.

Image description
Image description
Connecting Type Definitions and Resolvers
When setting up your Apollo Server, you connect your typeDefs and resolvers.

In a GraphQL API, mutations are used help change the state of the server, in this case we are using it to create, update, and delete data. The mutations I provided are using the gql function from @apollo/client, which helps to define and send mutation requests to a GraphQL server. Each mutation is connected to specific resolvers on the server side, which handle the actual data operations.

Image description
Image description

Connecting to Resolvers:
When this mutation is executed by a client, it sends a request to the GraphQL server. The server uses the defined schema to route the request to the appropriate resolver.

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
lovertab
Lovertab

Posted on May 18, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About