graph-gophers / graphql-go

GraphQL server with a focus on ease of use
BSD 2-Clause "Simplified" License
4.65k stars 492 forks source link

Question on separate resolvers for different operations #579

Closed danny-xx closed 1 year ago

danny-xx commented 1 year ago

Hi folks! I'm having some trouble getting the "separate resolvers for different operations" to work. I followed the example code in README but got this error message:

panic: *presentation.RootResolver does not resolve "Query": missing method for field "hello"

goroutine 1 [running]:
github.com/graph-gophers/graphql-go.MustParseSchema(...)
...

here's my handler code:

func gqlHandler() http.Handler {
    sdl := `
    schema {
      query: Query
      mutation: Mutation
    }

    type Query {
      hello: String!
    }

    type Mutation {
      hello: String!
    }
        `
    schema := graphql.MustParseSchema(sdl, &RootResolver{})
    return &relay.Handler{Schema: schema}
}

type RootResolver struct{}
type QueryResolver struct{}
type MutationResolver struct{}

func (r *RootResolver) Query() *QueryResolver {
    return &QueryResolver{}
}

func (r *RootResolver) Mutation() *MutationResolver {
    return &MutationResolver{}
}

func (*QueryResolver) Hello() string {
    return "Hello query!"
}

func (*MutationResolver) Hello() string {
    return "Hello mutation!"
}

Everything works fine on the query resolver (no root resolver). I'm wondering if I did something wrong on the code above. Thanks in advance if anyone could take a look!

pavelnikolov commented 1 year ago

Which version of the library are you using? I haven't cut a new release yet. These will be available in version v1.6.0. I'm waiting for one more issue with directives to be resolved and I'm going to cut a release. Check this unit test for example: https://github.com/graph-gophers/graphql-go/blob/master/graphql_test.go#L5200-L5353 You need to be on the master branch to make it work now.

danny-xx commented 1 year ago

Thanks for the quick reply @pavelnikolov! I'm using version v1.4.0 - is there an issue with this version? I was using v1.0.0 before and didn't run into this issue.

pavelnikolov commented 1 year ago

The provided change is in the latest master branch and it will be available in version v1.6.0. I expect to release that in the next week or two. There are a few remaining things left before this version can be released. I just tested with these file and it works:

go.mod:

module github.com/pavelnikolov/myapp

go 1.20

require github.com/graph-gophers/graphql-go v1.5.1-0.20230216224648-5aa631d05992

main.go:

package main

import (
    "context"
    "encoding/json"
    "os"

    "github.com/graph-gophers/graphql-go"
)

type RootResolver struct{}
type QueryResolver struct{}
type MutationResolver1 struct{}

func (r *RootResolver) Query() *QueryResolver {
    return &QueryResolver{}
}

func (r *RootResolver) Mutation() *MutationResolver1 {
    return &MutationResolver1{}
}

func (*QueryResolver) Hello() string {
    return "Hello query!"
}

func (*MutationResolver1) Hello() string {
    return "Hello mutation!"
}

func main() {
    sdl := `
    schema {
        query: Query
        mutation: Mutation
    }

    type Query {
        hello: String!
    }

    type Mutation {
        hello: String!
    }
    `
    schema := graphql.MustParseSchema(sdl, &RootResolver{})
    reqs := []string{
        `query { hello }`,
        `mutation { hello }`,
    }

    for _, r := range reqs {
        res := schema.Exec(context.Background(), r, "", nil)

        enc := json.NewEncoder(os.Stdout)
        enc.SetIndent("", "  ")
        err := enc.Encode(res)
        if err != nil {
            panic(err)
        }
    }
}

This is my result:

$ go run main.go
{
  "data": {
    "hello": "Hello query!"
  }
}
{
  "data": {
    "hello": "Hello mutation!"
  }
}
danny-xx commented 1 year ago

thanks for the confirmation! I'll try it again once v1.6.0 is released