Home > Backend Development > Golang > How to Unit Test GraphQL Queries and Mutations in Go with gqgen?

How to Unit Test GraphQL Queries and Mutations in Go with gqgen?

Mary-Kate Olsen
Release: 2024-10-30 08:52:02
Original
1076 people have browsed it

How to Unit Test GraphQL Queries and Mutations in Go with gqgen?

Unit Testing GraphQL in Go

When working with GraphQL and Go, unit testing is crucial for ensuring the correctness and reliability of your code. Here's a simple example demonstrating how to effectively unit test GraphQL queries and mutations using Go, taking into consideration the frameworks you mentioned:

<code class="go">package resolver_test

import (
    "testing"
    "github.com/99designs/gqlgen/client"
    "github.com/99designs/gqlgen/graphql/handler"
    "github.com/mrdulin/gqlgen-cnode/graph/generated"
    "github.com/mrdulin/gqlgen-cnode/graph/model"
    "github.com/mrdulin/gqlgen-cnode/graph/resolver"
    "github.com/mrdulin/gqlgen-cnode/mocks"
    "github.com/stretchr/testify/mock"
    "github.com/stretchr/testify/require"
)

// Define mock objects for UserService
type MockedUserService struct {
    mock.Mock
}

func (s *MockedUserService) GetUserByLoginname(loginname string) *model.UserDetail {
    args := s.Called(loginname)
    return args.Get(0).(*model.UserDetail)
}

func (s *MockedUserService) ValidateAccessToken(accesstoken string) *model.UserEntity {
    args := s.Called(accesstoken)
    return args.Get(0).(*model.UserEntity)
}

// Test MutationResolver_ValidateAccessToken
func TestMutationResolver_ValidateAccessToken(t *testing.T) {

    t.Run("should validate accesstoken correctly", func(t *testing.T) {
        // Create a mock UserService
        testUserService := new(mocks.MockedUserService)
        resolvers := resolver.Resolver{UserService: testUserService}

        // Create a GraphQL client
        c := client.New(handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &resolvers})))

        // Define the expected user entity
        ue := model.UserEntity{ID: "123", User: model.User{Loginname: &loginname, AvatarURL: &avatarURL}}

        // Set the mock UserService to return the expected user entity
        testUserService.On("ValidateAccessToken", "abc").Return(&ue)

        // Define the query to test
        var resp struct {
            ValidateAccessToken struct{ ID, Loginname, AvatarUrl string }
        }
        q := `
      mutation { 
        validateAccessToken(accesstoken: "abc") { 
          id, 
          loginname, 
          avatarUrl 
        } 
      }
    `

        // Execute the query and assert the results
        c.MustPost(q, &resp)
        testUserService.AssertExpectations(t)
        require.Equal(t, "123", resp.ValidateAccessToken.ID)
        require.Equal(t, loginname, resp.ValidateAccessToken.Loginname)
        require.Equal(t, avatarURL, resp.ValidateAccessToken.AvatarUrl)
    })
}

// Test QueryResolver_User
func TestQueryResolver_User(t *testing.T) {

    t.Run("should query user correctly", func(t *testing.T) {
        // Create a mock UserService
        testUserService := new(mocks.MockedUserService)
        resolvers := resolver.Resolver{UserService: testUserService}

        // Create a GraphQL client
        c := client.New(handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &resolvers})))

        // Define the expected user detail
        u := model.UserDetail{User: model.User{Loginname: &loginname, AvatarURL: &avatarURL}, Score: &score, CreateAt: &createAt}
</code>
Copy after login

The above is the detailed content of How to Unit Test GraphQL Queries and Mutations in Go with gqgen?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template