I can't get GraphQL to recognize the JSON scalar type.
I followed the [apollo docs] ( http://dev.apollodata.com/tools/graphql-tools/scalars.html#Using-a-package ) to define a JSON GraphQL scalar type for my schema:
Schema:
const SchemaDefinition = `
scalar JSON
schema {
query: Query
mutation: Mutation
}
`
export default [
SchemaDefinition,
Query,
Mutation,
...
]
Test type:
const Test = `
type Test {
bodyJson: JSON
}`
Resolver:
import GraphQLJSON from 'graphql-type-json'
const QueryResolver = {
Query: {
viewer(root, args, ctx) {
return User.query()
.where('id', ctx.state.user)
.first()
}
}
}
const scalarJSON = {
JSON: GraphQLJSON
}
export default {
...QueryResolver,
...ViewerResolver,
...scalarJSON
...
}
I'm using PostgreSQL and the column I'm querying (body_json) is of data type jsonb.
If I test my schema through GraphiQL, when I return the value straight from the db (I use Knex to query) I get this error message from GraphQL:
Expected a value of type \\"JSON\\" but received: [object Object]
If I use JSON.stringify first on the returned value, I get this error:
"Expected a value of type \\"JSON\\" but received: {\\"key\\":\\"test\\"}"
Any suggestion as to what I might be doing wrong?
I resolved custom scalar JSON like this in resolvers
JSON: {
__serialize(value) {
return GraphQLJSON.parseValue(value);
} }
And It worked fine for me. I think it will help you
我无法使用基于文本的模式使用自定义标量(使用来自核心“graphql”库的buildSchema('...')
),但是当我从头开始以编程方式构建模式时(使用new GraphQLSchema(...)
)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.