Graphql-go: panic: string is not a pointer

Created on 10 Jun 2017  路  7Comments  路  Source: graph-gophers/graphql-go

Hi, I had this code

package user

type UserResolver struct {
    User *User
}

func (r *UserResolver) Name() string {
    return r.User.Name
}

and it raise an error when build

panic: string is not a pointer
    returned by (*user.Resolver).Name
    returned by (*data.Resolver).User

But when I edit schema name: String to name: String! it work?

Most helpful comment

Hey @luongthanhlam, i've had some problems as well at the start, i hope i can help.
The problem is that a name: String could be nullable, instead a String! is not optional.
So your name resolver should be like this instead

func (r *UserResolver) Name() *string {``
  name := &r.User.Name
  return name
}

What i like to do is this. Given that all fields in go are initialized automatically, a string is initialized to "", so i just return nil if that's the case

func (r *UserResolver) Name() *string {``
  name := &r.User.Name
  if name == "" {
    return nil
  }
  return name
}

Also, int should be 32 bit according to the spec that's why. As well as floats are float64

All 7 comments

plus, why can't I use int/int64 as Int?

Hey @luongthanhlam, i've had some problems as well at the start, i hope i can help.
The problem is that a name: String could be nullable, instead a String! is not optional.
So your name resolver should be like this instead

func (r *UserResolver) Name() *string {``
  name := &r.User.Name
  return name
}

What i like to do is this. Given that all fields in go are initialized automatically, a string is initialized to "", so i just return nil if that's the case

func (r *UserResolver) Name() *string {``
  name := &r.User.Name
  if name == "" {
    return nil
  }
  return name
}

Also, int should be 32 bit according to the spec that's why. As well as floats are float64

Thanks, that makes a lot of sense

Does this mean you should always have ! in the schema?

Only when it makes sense for the field to be non-nullable.

If you make every field non-nullable, you'll get a brittle API if any field fails to resolve due to the way errors propagate in GraphQL.

If you make every field nullable, you'll have to ensure you handle missing values on the client side.

If you're thoughtful about which fields can fail to resolve, and which never make sense to fail (maybe something like User.id), you can toe the line between too brittle and too much work for clients.

But how do I make it nullable? I couldn鈥檛 get the other answer to work, I kept getting the same error. I am still new to Go so maybe this is the wrong forum..

I figured it out, I didn't have a string pointer in my args, therefore it still expected not null. Thank you so much for the good explanations!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zeitxx picture zeitxx  路  5Comments

LeeWong picture LeeWong  路  4Comments

rogchap picture rogchap  路  5Comments

dengliu picture dengliu  路  5Comments

AndreasBackx picture AndreasBackx  路  4Comments