graph-gophers / graphql-go

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

2023/03/29 07:14:42 Error: The data source arguments are not valid #613

Closed RealYukiSan closed 1 year ago

RealYukiSan commented 1 year ago

I tried to create a simple resolver, but it seems to have gone wrong. Am I missing something? Here's the code.

The resolver

package app

type Resolver struct{}
package app

import (
    model "go-blog/app/database/model"
)

func (r *Resolver) Account() (*model.Account, error) {
    acc, err := model.ReadAccount(1)
    if err != nil {
        return &model.Account{}, err
    }
    return acc, nil
}

and the database

package app

import "go-blog/app/database"

type Account struct {
    Username string
    Fullname string
    Image    string
}

var err error

func ReadAccount(id uint8) (*Account, error) {
    var acc *Account
    err = app.DB.QueryRow("SELECT * EXCEPT password, id FROM Accounts WHERE id=$1", id).Scan(&acc.Username, &acc.Fullname, &acc.Image)
    if err != nil {
        return &Account{}, err
    }

    return acc, nil
}

and the entry point

package main

import (
    "log"
    "net/http"

    app "go-blog/app/controller"
    "go-blog/pkg/sdl"

    graphql "github.com/graph-gophers/graphql-go"
    "github.com/graph-gophers/graphql-go/relay"
    _ "github.com/lib/pq"
)

func main() {
    opts := []graphql.SchemaOpt{graphql.UseFieldResolvers()}
    schema := graphql.MustParseSchema(sdl.Schema, &app.Resolver{}, opts...)
    // schema := graphql.MustParseSchema(sdl.Schema, &app.Resolver{})
    http.Handle("/query", &relay.Handler{Schema: schema})
    http.HandleFunc("/graphiql", func(w http.ResponseWriter, r *http.Request) { w.Write(sdl.Page) })
    log.Fatal(http.ListenAndServe(":8080", nil))
}

and the schema

type Account {
  username: String!
  fullname: String!
  image: String!
}

type Query {
    account: Account
}

full error

reyuki@weebs:~/Desktop/MyHost/github/go-blog$ go run server/main.go 
2023/03/29 07:14:42 Error: The data source arguments are not valid
exit status 1

full directory structure:

.
├── app
│   ├── controller
│   │   ├── account.go
│   │   ├── guest.go
│   │   ├── post.go
│   │   ├── resolver.go
│   │   └── tag.go
│   └── database
│       ├── db.go
│       ├── migration
│       │   ├── 000001_init_schema.down.sql
│       │   └── 000001_init_schema.up.sql
│       └── model
│           ├── account.go
│           ├── guest.go
│           ├── post.go
│           └── tag.go
├── go.mod
├── go.sum
├── pkg
│   └── sdl
│       ├── index.html
│       ├── playground.go
│       ├── schema.gql
│       └── sdl.go
└── server
    └── main.go

8 directories, 19 files
RealYukiSan commented 1 year ago

I think the error appears because the return type (in this case is *model.Account) comes from another file, but I don't know what causes it to happen, I would really appreciate it if someone could tell me what actually happens and what causes it.

pavelnikolov commented 1 year ago

@Reyuki-san I don't think that your error is related to the library. It is an error from your data layer. You can confirm this if you return a hard-coded instance of this struct (which is not populated by the data layer). Alternatively, you can run the DB code which executes the query without the GraphQL resolver and confirm that you get the same error. The fact that the struct is defined in another file is definitely not the problem in this case. I am closing this issue as it is not an issue with the library.

RealYukiSan commented 1 year ago

@pavelnikolov Thank you for your response and for helping me troubleshoot the issue. I apologize for any inconvenience or confusion caused by my initial assumption that the error was related to the library. I will try your suggestions and confirm if the error is coming from my data layer. Thank you again for your assistance.