Graphql-go: define scalar type in schema

Created on 20 Dec 2016  路  7Comments  路  Source: graph-gophers/graphql-go

I use custom scalar, and it needed to be defined to use with other javascript backend.

scalar Time
scalar JSON

Otherwise, it gives error along the lines of, unreferenced type Time, etc.

but, graphql-go gives error

ERROR: graphql: syntax error: unexpected "scalar", expecting "schema", "type", "enum", "interface", "union" or "input" (line 7, column 9)

can we allow scalar to be valid type?

Most helpful comment

Ah yes, that might work right now since time.Time ist just fed to json.Marshal unchecked. However, I'll soon only allow basic mappings, e.g. Int to int32 and types with ImplementsGraphQLType, so that will not work any more. This is so we have a proper check that the Go type is actually meant for implementing a certain GraphQL type. Maybe there will also be a MarshalGraphQL method, since I'd like to keep GraphQL separate from JSON semantics (which is how the GraphQL spec is designed).

All 7 comments

Isn't this just resolved by my latest change?

sorry, haven't tried with it yet. will get back tomorrow.

Thanks it works great. One question. Is it strictly require the resolver output type to be graphql.Time. It works if I use time.Time. I may change later, but wonder is it ok for now?

ref

The Go type has to implement ImplementsGraphQLType and UnmarshalGraphQL, which time.Time does not. If it works in your case, then something is weird.

sorry for not being clear. In my case, it does implement the interface, but the resolver returns the raw type.

so instead of the new way

 func (r *ageResolver) Time() graphql.Time {
   return graphql.Time{Time: r.Time}
 }

the old way of returning non graphql type works

func (r *ageResolver) Time() time.Time {
    return r.Time 

lot of changes to re-write all the resolvers now, and wonder it's ok to keep that way for some time.

Ah yes, that might work right now since time.Time ist just fed to json.Marshal unchecked. However, I'll soon only allow basic mappings, e.g. Int to int32 and types with ImplementsGraphQLType, so that will not work any more. This is so we have a proper check that the Go type is actually meant for implementing a certain GraphQL type. Maybe there will also be a MarshalGraphQL method, since I'd like to keep GraphQL separate from JSON semantics (which is how the GraphQL spec is designed).

thanks for the explanation. I would soon change to follow the right type to return. thanks again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndreasBackx picture AndreasBackx  路  4Comments

saward picture saward  路  6Comments

estutzenberger picture estutzenberger  路  4Comments

linuxmonk picture linuxmonk  路  3Comments

tonyghita picture tonyghita  路  3Comments