Hello all,
I am having this error when I run my program.
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"strconv"
graphql "github.com/neelance/graphql-go"
"github.com/neelance/graphql-go/relay"
_ "time"
_ "github.com/lib/pq"
)
const (
DB_HOST = "localhost"
DB_PORT = "5433"
DB_USER = "postgres"
DB_PASSWORD = "admin"
DB_NAME = "testdb"
)
var schema *graphql.Schema
var Schema = `schema {
query: Query
}
type Query {
student(id:ID!): Student
}
type Student {
id: Int!
name: String!
}
` // end of student marker
type student struct {
ID int
Name string
}
var students = []*student{
{
ID: 01,
Name: "Rahul",
},
{
ID: 02,
Name: "Ramesh",
},
}
var studentData = make(map[int]*student)
func init() {
for _, s := range students {
studentData[s.ID] = s
}
schema = graphql.MustParseSchema(Schema, &Resolver{})
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
type Resolver struct{}
type studentResolver struct {
s *student
}
func (r *studentResolver) ID() int {
return r.s.ID
}
func (r *studentResolver) Name() string {
return r.s.Name
}
func (r *Resolver) Student(args struct{ ID int }) *studentResolver {
//var studentID int = args.ID
//loadStudentData(studentID)
if s := studentData[args.ID]; s != nil {
return &studentResolver{s}
}
return nil
}
func main() {
// // http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// // //w.Write("Hello World")
// // }))
//
// http.Handle("/", &relay.Handler{Schema: schema})
//
// log.Fatal(http.ListenAndServe(":8081", nil))
http.Handle("/graphiql", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(page)
}))
http.Handle("/query", &relay.Handler{Schema: schema})
log.Fatal(http.ListenAndServe(":8082", nil))
}
var page = []byte(
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.10.2/graphiql.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/1.1.0/fetch.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.4/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.4/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.10.2/graphiql.js"></script>
</head>
<body style="width: 100%; height: 100%; margin: 0; overflow: hidden;">
<div id="graphiql" style="height: 100vh;">Loading...</div>
<script>
function graphQLFetcher(graphQLParams) {
alert(JSON.stringify(graphQLParams))
return fetch("/query", {
method: "post",
body: JSON.stringify(graphQLParams),
credentials: "include",
}).then(function (response) {
return response.text();
}).then(function (responseBody) {
try {
return JSON.parse(responseBody);
} catch (error) {
return responseBody;
}
});
}
ReactDOM.render(
React.createElement(GraphiQL, {fetcher: graphQLFetcher}),
document.getElementById("graphiql")
);
</script>
</body>
</html>
)
Pls Help if any one know why this error is comming.
can you format your code so it is easy to read.
sure.
example.txt
Basically the problem is I am not able to do type conversion if graphql.ID to string type (in go).
Do you know how to do type conversion from GraphQL types to go types.
thanks bsr203
graphql.ID is string. can't you do string(id) ?
func (r *studentResolver) ID() int {
return r.s.ID
}
I believe the return type here should be int32 based on your schema, instead of int.
We have to specify int32 because the GraphQL specification limits integer values to 32 bits (this is probably due to the way Javascript handles Numbers).
In go, int only guarantees that the value will be at least 32 bits, but could be more in some cases.
You can learn more about the differences in types by reading the builtin package documentation.
_NOTE:_ it is more semantic for you to use the ID scalar type for ID fields.
__Suggested schema changes:__
type Student {
- id: Int!
+ id: ID!
name: String!
}
__Corresponding resolver method:__
func (r *studentResolver) ID() graphql.ID {
id := strconv.Itoa(r.s.ID) // first, convert to string
return graphql.ID(id) // coerce the string to the graphql.ID type
}
Hello All
tonyghita thanks for your response.As you suggested first tried to changed the return type to int32, and made the resolver as
func (r *studentResolver) ID() int32 {
return r.s.ID
}
but this gives compilation error :
.\example.go:79:12: cannot use r.s.ID (type int) as type int32 in return argument,
but I liked the suggestion to use following schema:
Corresponding resolver method:
func (r *studentResolver) ID() graphql.ID {
id := strconv.Itoa(r.s.ID) // first, convert to string
return graphql.ID(id) // coerce the string to the graphql.ID type
}
This solution works. I was unaware of statement graphql.ID(id) which converts go string to graphql.ID types.
Thanks bsr203 for your reply but that statement did not work.
For the TL;DR types that need the big fat arrow and syntax highlighting:
func (r *resolvableThing) NumberThing() int32 {
return int32(r.x.NumberThing)
}
Must cast int (or int64) to int32 because JavaScript ints are only 52 (and some change) bits wide.
Most helpful comment
=> Solution <=
For the TL;DR types that need the big fat arrow and syntax highlighting:
Must cast
int(orint64) toint32because JavaScript ints are only 52 (and some change) bits wide.