Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scalar type error - Not supported #350

Closed
rodbau opened this issue Aug 29, 2019 · 4 comments
Closed

Scalar type error - Not supported #350

rodbau opened this issue Aug 29, 2019 · 4 comments

Comments

@rodbau
Copy link

rodbau commented Aug 29, 2019

Not support map[string]interface{} type.

example:

scalar Map
    schema {
        query: Query
    }
    
    type Query {
        run(data: Map): Map
    } 

I have this code:

type Map struct {
    value interface{}
}
// ImplementsGraphQLType i
func (Map) ImplementsGraphQLType(name string) bool {
    return name == “Map”
}
// UnmarshalGraphQL is
func (j *Map) UnmarshalGraphQL(input interface{}) error {
    j.value = input.(map[string]interface{})
    return nil
}
// MarshalJSON is
func (j Map) MarshalJSON() ([]byte, error) {
    return json.Marshal(j.value)
}

type Resolver struct{}
// Run is
func (r *Resolver) Run(ctx context.Context, args struct {
    Data *Map
}) *Map {
    return args.Data
}

The query is:

{
 run(data:{a:123456} )
}

The error is:

{
 “errors”: [
   {
     “message”: “Argument \“data\” has invalid value {a: 123456}.\nExpected type \“Map\“, found {a: 123456}.“,
     “locations”: [
       {
         “line”: 2,
         “column”: 12
       }
     ]
   }
 ]
}

And I expect this:

{
 “data”: {
   “run”: {
     “a”: 123456
   }
 }
}

@arturoeanton
Copy link

I solved it. I commenting on these lines of "graphql-go/internal/validation/validation.go"

   case *schema.Scalar, *schema.Enum:
   	return true, ""
   	/*if lit, ok := v.(*common.BasicLit); ok {
   		if validateBasicLit(lit, t) {
   			return true, ""
   		}
   	}*/

In Line 775

@pavelnikolov
Copy link
Member

AFAIK, maps are not supported in the GraphQL specification: graphql/graphql-spec#101
My recommendation would be rethink your schema design. Maybe it's better to go with arrays in order to keep it predicable and type-safe. Is this supported by the reference implementation?

@hampusohlsson
Copy link

@arturoeanton FWIW this should work (it is working for us at least) if you write your query using variables instead.

query myQuery($run: Map) {
    run(data: $run)
}

variables:

{
  "run": {
    "foo": "hello world",
    "bar": 123
  }
}

@cpunekar
Copy link

cpunekar commented Aug 10, 2020

I also using a map interface in my sample, getting below error when I run:

panic: can not use map[string]interface {} as Map

My GraphQL type:

scalar Map

type Test {
  props: Map
}

Go struct

type Sample struct {
  Props *map[string]interface{}
}

type Map struct {
    props interface{}
}

Can someone please let me know what I'm doing wrong here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants