How to use graphql-type-json package with GraphQl

I cannot get GraphQL to recognize a scalar JSON type.

I followed [apollo docs] ( http://dev.apollodata.com/tools/graphql-tools/scalars.html#Using-a-package ) to determine the scalar Graphson JSON type for my schema:

Scheme:

const SchemaDefinition = `
   scalar JSON
   schema {
     query: Query
     mutation: Mutation
  }
`

export default [
  SchemaDefinition,
  Query,
  Mutation,
  ...
]

Type of test:

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 am using PostgreSQL, and the column I am querying (body_json) has a jsonb data type.

If I test my schema via GraphiQL when I return the value directly from db (I use Knex for the query), I get this error message from GraphQL:

The expected value is of type \ "JSON \", but received: [object Object]

If I use JSON.stringify first on the return value, I get this error:

" \" JSON\ ", : {\" key\ ": \" test\ "}"

, ?

+4
2

JSON, resolvers

 JSON: {

__serialize(value) {
    return GraphQLJSON.parseValue(value);
} }

. ,

+2

, ( buildSchema('...') graphql), , ( new GraphQLSchema(...)).

0

Source: https://habr.com/ru/post/1670332/


All Articles