Comments (11)
Bumped into this issue. Any update on supporting this?
Thanks
from graphql.
I have gone ahead and introduced the issue into the boilerplate code you provided above.
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"github.com/shurcooL/graphql"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/graphql", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"data": {"viewer": {"login": "gopher", "bio": "The Go gopher.", "meta": {"foo": "bar"}}}}`)
})
client := graphql.NewClient("/graphql", &http.Client{Transport: localRoundTripper{handler: mux}})
var query struct {
Viewer struct {
Login string
Biography string `graphql:"bio"`
Meta json.RawMessage
}
}
err := client.Query(context.Background(), &query, nil)
fmt.Println("error:", err)
fmt.Printf("query: %+v\n", query)
// Output:
// error: <nil>
// query: {Viewer:{Login:gopher Biography:The Go gopher.}}
}
// localRoundTripper is an http.RoundTripper that executes HTTP transactions
// by using handler directly, instead of going over an HTTP connection.
type localRoundTripper struct {
handler http.Handler
}
func (l localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
w := httptest.NewRecorder()
l.handler.ServeHTTP(w, req)
return w.Result(), nil
}
from graphql.
@cor My #41 branch is still here... https://github.com/Navops/graphql/tree/support-raw-json (the v0.0.2 tag is pointing at this branches current HEAD/tip). You can use that directly, fork it, etc. We do something similar in our project by adding the following line to our go.mod
replace github.com/shurcooL/graphql => github.com/navops/graphql v0.0.2
from graphql.
This is now supported in my transport-agnostic fork: https://github.com/deref/graphql-go
The specific changes are here: deref/graphql-go@c7885fd...e2b4739
from graphql.
Thanks for the report. How can I reproduce this?
from graphql.
@dmitshur I have same issue too, My GQL like struct below
query { example(id:"123456") { id jsondata } }
jsondata result example is {foo: bar}
from graphql.
There's not enough information there for me to reproduce this. Can you please post a minimal working Go program that triggers that error? Or at least include the query (or a minified version of it) you wrote, and the JSON response from the GraphQL server?
Here's a starting point to make it easier; you just need to replace the query and JSON response:
package main
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"github.com/shurcooL/graphql"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/graphql", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"data": {"viewer": {"login": "gopher", "bio": "The Go gopher."}}}`)
})
client := graphql.NewClient("/graphql", &http.Client{Transport: localRoundTripper{handler: mux}})
var query struct {
Viewer struct {
Login string
Biography string `graphql:"bio"`
}
}
err := client.Query(context.Background(), &query, nil)
fmt.Println("error:", err)
fmt.Printf("query: %+v\n", query)
// Output:
// error: <nil>
// query: {Viewer:{Login:gopher Biography:The Go gopher.}}
}
// localRoundTripper is an http.RoundTripper that executes HTTP transactions
// by using handler directly, instead of going over an HTTP connection.
type localRoundTripper struct {
handler http.Handler
}
func (l localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
w := httptest.NewRecorder()
l.handler.ServeHTTP(w, req)
return w.Result(), nil
}
from graphql.
I'm also running into this issue. I am using Prisma as my GraphQL Server, which provides a scalar Json type. Consider the following schema:
type Query {
metaObjectType(id:ID!): MetaObjectType
}
type MetaObjectType {
parentId: String!
meta: Json!
}
The following query is valid.
query {
metaObjectType(id:$id) {
parentId
meta
}
}
The following query is invalid, and produces an error: "Field 'meta' of type 'Json' must not have a sub selection."
query {
metaObjectType(id:$id) {
parentId
meta {
someMetaKey
}
}
}
I'd like to define my struct like this:
type MyQuery struct {
MetaObjectType struct {
ParentID string
Meta json.RawMessage
} `graphql:"metaObjectType(id:$id)"`
}
However, as reported above, the "no place to unmarshal" error is returned when the response json contains more fields than the provided struct.
My vote is to queries to be specified as json.RawMessage
for this use case.
from graphql.
With the changes from #41:
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"github.com/shurcooL/graphql"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/graphql", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"data": {"viewer": {"login": "gopher", "bio": "The Go gopher.", "meta": {"foo": "bar"}}}}`)
})
client := graphql.NewClient("/graphql", &http.Client{Transport: localRoundTripper{handler: mux}})
var query struct {
Viewer struct {
Login string
Biography string `graphql:"bio"`
Meta json.RawMessage
}
}
err := client.Query(context.Background(), &query, nil)
fmt.Println("error:", err)
fmt.Printf("query: %+v\n", query)
fmt.Printf("raw: %+v\n", string(query.Viewer.Meta))
// Output:
// error: <nil>
// query: {Viewer:{Login:gopher Biography:The Go gopher. Meta:[123 34 102 111 111 34 58 34 98 97 114 34 125]}}
// raw: {"foo":"bar"}
}
// localRoundTripper is an http.RoundTripper that executes HTTP transactions
// by using handler directly, instead of going over an HTTP connection.
type localRoundTripper struct {
handler http.Handler
}
func (l localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
w := httptest.NewRecorder()
l.handler.ServeHTTP(w, req)
return w.Result(), nil
}
cbrunner-mac:graphql cbrunner$ go run cmd/main.go
error: <nil>
query: {Viewer:{Login:gopher Biography:The Go gopher. Meta:[123 34 102 111 111 34 58 34 98 97 114 34 125]}}
raw: {"foo":"bar"}
from graphql.
Thank you @adamgoose for providing additional information and the reproducible snippet. It has helped a lot, I'm able to understand this issue now. I haven't run into it myself because GitHub API v4 doesn't have a JSON scalar type.
Thanks for working on a PR @cameronbrunner, and sorry about the slow response there. I don't have a lot of bandwidth left to do reviews in this repo unfortunately.
Using json.RawMessage
seems reasonable, but I'll want to think if there any other solutions, and then I'll try to find some time for code review. I just wanted to post this update here for now.
from graphql.
Is there a workaround for this? I really need to insert a jsonb
into a Hasura database
from graphql.
Related Issues (20)
- query with circular dependencies leads to infinite recursion HOT 1
- Any interest in native go type support? HOT 1
- I want to add where condition in the query structure, how to do this
- How to define struct HOT 3
- Query and Mutation methods ignore query content when it's wrapped in an interface HOT 3
- Query for Github Projects in an organization HOT 2
- How to deal with integer overflow when using variables HOT 2
- How to make one condition in filter optional? HOT 1
- My stacks
- Question: How to implement an enumeration type HOT 1
- Question: Example of doing pagination (especially on nested resources that are paginated) HOT 6
- Nested struct tag not applied to query HOT 2
- Using alternative rest clients
- OAuth2 Bearer Token with Customer HttpClient HOT 1
- Please make "constructQuery" exported HOT 3
- Allow building mutations from maps instead of structs
- I love this library but it seems unmaintained? HOT 1
- FaunaDB graphql _id defined as _ID not exported HOT 4
- Message: x-hasura-admin-secret but not found,
- Return more verbose error to the caller HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql.