Graphql-go: Resolver: Pointer Type vs Value Type

Created on 16 Jul 2018  路  4Comments  路  Source: graph-gophers/graphql-go

I am very confused by what kind of resovlers are allowed to exist on pointer types and what are not.

Can someone gives me examples (both Go code and GraphQL schemas)

This is worth to document as well.

Most helpful comment

@CreatCodeBuild
if one field is optional, it must be pointer in resolvers, and struct must be pointer. Otherwise, you should use value type

All 4 comments

This killed me as well. Was stuck forever wondering why it seemed I was only allowed to resolve to pointers of anything

@CreatCodeBuild
if one field is optional, it must be pointer in resolvers, and struct must be pointer. Otherwise, you should use value type

This had me stuck for a while too. This doesn't seem like correct behavior.

Say I have a query like the following.

type Query {
    user: [User]
}
type Users {
    name: String!
    pets: [Pets!]!
}
type Pets  {
    name: String!
}
query {
    users {
        name
        pets {
            name
        }
    }
}
func (u User) Pets(ctx context.Context) ([]Pet, error) {
    // DB Access and return
}

First of all, slices are already a nilable value. Second of all, error is handled in GraphQL by making the first nullable query result in null.

In the example above, if Pets returns an error, the individual User should be null. Because of situations like this, the library shouldn't care if it's a pointer type or not, it should respond. A nullable GraphQL field resolver returning non-pointer value shouldn't be a problem and a non-nullable field that returns an error shouldn't be a problem.

@CreatCodeBuild, @nanozuki is correct. In general, optional fields are pointers and required fields are value types.
@duckbrain I understand the confusion with lists. In the example that you have given above, indeed the individual user needs to be null in case one of the Pets' resolvers returns an error or nil value. This is called error propagation and a proper fix for it was implemented and merged recently.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

0xTanvir picture 0xTanvir  路  5Comments

saward picture saward  路  6Comments

estutzenberger picture estutzenberger  路  4Comments

aquiseb picture aquiseb  路  4Comments

Hagbarth picture Hagbarth  路  6Comments