Gqlgen: The sample code in getting started cannot be compiled

Created on 9 Aug 2020  ยท  3Comments  ยท  Source: 99designs/gqlgen

What happened?

The following command failed. This happens when you implement the code according to Implement the resolvers

โฏ go run server.go
graph/schema.resolvers.go:18:36: not enough arguments in call to "crypto/rand".Int
        have ()
        want (io.Reader, *big.Int)
graph/schema.resolvers.go:18:36: multiple-value "crypto/rand".Int() in single-value context

What did you expect?

The server is successfully started.

Minimal graphql.schema and models to reproduce

graphql.schema and models

# GraphQL schema example
#
# https://gqlgen.com/getting-started/

type Todo {
  id: ID!
  text: String!
  done: Boolean!
  user: User!
}

type User {
  id: ID!
  name: String!
}

type Query {
  todos: [Todo!]!
}

input NewTodo {
  text: String!
  userId: String!
}

type Mutation {
  createTodo(input: NewTodo!): Todo!
}

// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.

package model

type NewTodo struct {
    Text   string `json:"text"`
    UserID string `json:"userId"`
}

type Todo struct {
    ID   string `json:"id"`
    Text string `json:"text"`
    Done bool   `json:"done"`
    User *User  `json:"user"`
}

type User struct {
    ID   string `json:"id"`
    Name string `json:"name"`
}

versions

  • gqlgen version: v0.11.3
  • go version: go version go1.13.8 linux/amd64
  • dep or go modules?
stale

Most helpful comment

This patch makes it boot up.

diff --git a/server-next/graph/schema.resolvers.go b/server-next/graph/schema.resolvers.go
index d14d5d1..ffa99c8 100644
--- a/server-next/graph/schema.resolvers.go
+++ b/server-next/graph/schema.resolvers.go
@@ -7,15 +7,18 @@ import (
        "context"
        "crypto/rand"
        "fmt"
+       "math/big"

        "github.com/ksoda/todo-app/graph/generated"
        "github.com/ksoda/todo-app/graph/model"
 )

 func (r *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (*model.Todo, error) {
+       rand, _ := rand.Int(rand.Reader, big.NewInt(100))
        todo := &model.Todo{
+
                Text: input.Text,
-               ID:   fmt.Sprintf("T%d", rand.Int()),
+               ID:   fmt.Sprintf("T%d", rand),
                User: &model.User{ID: input.UserID, Name: "user " + input.UserID},
        }
        r.todos = append(r.todos, todo)

All 3 comments

This patch makes it boot up.

diff --git a/server-next/graph/schema.resolvers.go b/server-next/graph/schema.resolvers.go
index d14d5d1..ffa99c8 100644
--- a/server-next/graph/schema.resolvers.go
+++ b/server-next/graph/schema.resolvers.go
@@ -7,15 +7,18 @@ import (
        "context"
        "crypto/rand"
        "fmt"
+       "math/big"

        "github.com/ksoda/todo-app/graph/generated"
        "github.com/ksoda/todo-app/graph/model"
 )

 func (r *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (*model.Todo, error) {
+       rand, _ := rand.Int(rand.Reader, big.NewInt(100))
        todo := &model.Todo{
+
                Text: input.Text,
-               ID:   fmt.Sprintf("T%d", rand.Int()),
+               ID:   fmt.Sprintf("T%d", rand),
                User: &model.User{ID: input.UserID, Name: "user " + input.UserID},
        }
        r.todos = append(r.todos, todo)

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Simply import "math/rand" instead of "crypto/rand"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

huanghantao picture huanghantao  ยท  3Comments

jacksontj picture jacksontj  ยท  4Comments

cemremengu picture cemremengu  ยท  3Comments

jwatte picture jwatte  ยท  4Comments

lynntobing picture lynntobing  ยท  3Comments