github.com/Desuuuu/genqlient@v0.5.3/internal/integration/server/server.go (about)

     1  package server
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http/httptest"
     7  	"strconv"
     8  
     9  	"github.com/99designs/gqlgen/graphql"
    10  	"github.com/99designs/gqlgen/graphql/handler"
    11  	"github.com/99designs/gqlgen/graphql/handler/transport"
    12  )
    13  
    14  func strptr(v string) *string { return &v }
    15  func intptr(v int) *int       { return &v }
    16  
    17  var users = []*User{
    18  	{
    19  		ID: "1", Name: "Yours Truly", LuckyNumber: intptr(17),
    20  		Birthdate: strptr("2025-01-01"),
    21  		Hair:      &Hair{Color: strptr("Black")},
    22  	},
    23  	{ID: "2", Name: "Raven", LuckyNumber: intptr(-1), Hair: nil},
    24  }
    25  
    26  func init() {
    27  	users[0].Friends = []*User{users[1]} // (obviously a lie, but)
    28  	users[1].Friends = users             // try to crash the system
    29  }
    30  
    31  var animals = []*Animal{
    32  	{
    33  		ID: "3", Name: "Fido", Species: SpeciesDog, Owner: userByID("1"),
    34  		Hair: &BeingsHair{HasHair: true},
    35  	},
    36  	{
    37  		ID: "4", Name: "Old One", Species: SpeciesCoelacanth, Owner: nil,
    38  		Hair: &BeingsHair{HasHair: false},
    39  	},
    40  }
    41  
    42  func userByID(id string) *User {
    43  	for _, user := range users {
    44  		if id == user.ID {
    45  			return user
    46  		}
    47  	}
    48  	return nil
    49  }
    50  
    51  func usersByBirthdates(dates []string) []*User {
    52  	var retval []*User
    53  	for _, date := range dates {
    54  		for _, user := range users {
    55  			if user.Birthdate != nil && *user.Birthdate == date {
    56  				retval = append(retval, user)
    57  			}
    58  		}
    59  	}
    60  	return retval
    61  }
    62  
    63  func beingByID(id string) Being {
    64  	for _, user := range users {
    65  		if id == user.ID {
    66  			return user
    67  		}
    68  	}
    69  	for _, animal := range animals {
    70  		if id == animal.ID {
    71  			return animal
    72  		}
    73  	}
    74  	return nil
    75  }
    76  
    77  func getNewID() string {
    78  	maxID := 0
    79  	for _, user := range users {
    80  		intID, _ := strconv.Atoi(user.ID)
    81  		if intID > maxID {
    82  			maxID = intID
    83  		}
    84  	}
    85  	for _, animal := range animals {
    86  		intID, _ := strconv.Atoi(animal.ID)
    87  		if intID > maxID {
    88  			maxID = intID
    89  		}
    90  	}
    91  	newID := maxID + 1
    92  	return strconv.Itoa(newID)
    93  }
    94  
    95  func (r *queryResolver) Me(ctx context.Context) (*User, error) {
    96  	return userByID("1"), nil
    97  }
    98  
    99  func (r *queryResolver) User(ctx context.Context, id *string) (*User, error) {
   100  	if id == nil {
   101  		return userByID("1"), nil
   102  	}
   103  	return userByID(*id), nil
   104  }
   105  
   106  func (r *queryResolver) Being(ctx context.Context, id string) (Being, error) {
   107  	return beingByID(id), nil
   108  }
   109  
   110  func (r *queryResolver) Beings(ctx context.Context, ids []string) ([]Being, error) {
   111  	ret := make([]Being, len(ids))
   112  	for i, id := range ids {
   113  		ret[i] = beingByID(id)
   114  	}
   115  	return ret, nil
   116  }
   117  
   118  func (r *queryResolver) LotteryWinner(ctx context.Context, number int) (Lucky, error) {
   119  	for _, user := range users {
   120  		if user.LuckyNumber != nil && *user.LuckyNumber == number {
   121  			return user, nil
   122  		}
   123  	}
   124  	return nil, nil
   125  }
   126  
   127  func (r *queryResolver) UsersBornOn(ctx context.Context, date string) ([]*User, error) {
   128  	return usersByBirthdates([]string{date}), nil
   129  }
   130  
   131  func (r *queryResolver) UsersBornOnDates(ctx context.Context, dates []string) ([]*User, error) {
   132  	return usersByBirthdates(dates), nil
   133  }
   134  
   135  func (r *queryResolver) UserSearch(ctx context.Context, birthdate *string, id *string) ([]*User, error) {
   136  	switch {
   137  	case birthdate == nil && id != nil:
   138  		return []*User{userByID(*id)}, nil
   139  	case birthdate != nil && id == nil:
   140  		return usersByBirthdates([]string{*birthdate}), nil
   141  	default:
   142  		return nil, fmt.Errorf("need exactly one of birthdate or id")
   143  	}
   144  }
   145  
   146  func (r *queryResolver) Fail(ctx context.Context) (*bool, error) {
   147  	f := true
   148  	return &f, fmt.Errorf("oh no")
   149  }
   150  
   151  func (m mutationResolver) CreateUser(ctx context.Context, input NewUser) (*User, error) {
   152  	newUser := User{ID: getNewID(), Name: input.Name, Friends: []*User{}}
   153  	users = append(users, &newUser)
   154  	return &newUser, nil
   155  }
   156  
   157  func RunServer() *httptest.Server {
   158  	gqlgenServer := handler.New(NewExecutableSchema(Config{Resolvers: &resolver{}}))
   159  	gqlgenServer.AddTransport(transport.POST{})
   160  	gqlgenServer.AddTransport(transport.GET{})
   161  	gqlgenServer.AroundResponses(func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
   162  		graphql.RegisterExtension(ctx, "foobar", "test")
   163  		return next(ctx)
   164  	})
   165  	return httptest.NewServer(gqlgenServer)
   166  }
   167  
   168  type (
   169  	resolver         struct{}
   170  	queryResolver    struct{}
   171  	mutationResolver struct{}
   172  )
   173  
   174  func (r *resolver) Mutation() MutationResolver {
   175  	return &mutationResolver{}
   176  }
   177  
   178  func (r *resolver) Query() QueryResolver { return &queryResolver{} }
   179  
   180  //go:generate go run github.com/99designs/gqlgen