Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use graphql-type-json package with GraphQl

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?

like image 432
Telitos Avatar asked Feb 20 '17 19:02

Telitos


People also ask

Does GraphQL use JSON?

GraphQL services typically respond using JSON, however the GraphQL spec does not require it. JSON may seem like an odd choice for an API layer promising better network performance, however because it is mostly text, it compresses exceptionally well with GZIP.

How do I invoke a GraphQL API?

To use the explorer, we'll head to studio.apollographql.com/dev and create an account (using either GitHub or your email). Finally, choose a name for your graph, select the “Development” graph type, add your localhost endpoint (Apollo Server's default is http://localhost:4000), and click “Create Graph”. And that's it!

What other types can be used in a GraphQL schema?

The GraphQL schema language supports the scalar types of String , Int , Float , Boolean , and ID , so you can use these directly in the schema you pass to buildSchema . By default, every type is nullable - it's legitimate to return null as any of the scalar types.

How do you query a schema in GraphQL?

How To Get The Schema — Introspection Queries. Some GraphQL servers don't provide a convenient GraphQL API explorer. Instead, to get the schema we need to send a HTTP request to the GraphQL server endpoint asking for the GraphQL schema. This type of HTTP request is called a GraphQL introspection query.


1 Answers

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

like image 139
Seena V P Avatar answered Sep 28 '22 22:09

Seena V P