Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The sample code in getting started cannot be compiled #1274

Closed
ksoda opened this issue Aug 9, 2020 · 4 comments
Closed

The sample code in getting started cannot be compiled #1274

ksoda opened this issue Aug 9, 2020 · 4 comments
Labels

Comments

@ksoda
Copy link

ksoda commented Aug 9, 2020

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?
@ksoda
Copy link
Author

ksoda commented Aug 9, 2020

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)

@stale
Copy link

stale bot commented Nov 7, 2020

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.

@stale stale bot added the stale label Nov 7, 2020
@stale stale bot closed this as completed Nov 15, 2020
@hayashi-ay
Copy link

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

dangolbeeker added a commit to dangolbeeker/gqlgen-todos that referenced this issue May 10, 2021
chaitanyapantheor added a commit to chaitanyamaili/gqlgen that referenced this issue Oct 25, 2022
@chaitanyamaili
Copy link
Contributor

Here is the fix PR #2411

StevenACoffman pushed a commit that referenced this issue Nov 4, 2022
Co-authored-by: Chaitanya Maili <chaitanya.maili@pantheon.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants