Service Component Creation for GraphQL
Learn how to create a service component in the GraphQL application.
Create a service component
Before creating a service component, we create a new directory inside the graph
directory called service
. Then, we add a service component in the service.go
.
package serviceimport ("errors""github.com/google/uuid""go-gql-blogs/graph/model")// BlogService represents service componenttype BlogService struct{}// create a local storagevar storage []*model.Blog
Service component for the application
In the code above, there are two main components:
-
BlogService
: Acts as a service component that is used by the resolver. -
storage
: Acts as local storage for the application.
Get all blogs
We create a function to get all blogs called GetAllBlogs
.
// other codes..func (b *BlogService) GetAllBlogs() []*model.Blog {return storage}
Get all blogs
In the code above, the function simply returns all blogs. ...