0
votes

I want to return my result as a JSON OBJECT in the metas field on the graphiql interface, and how can I define a custom scalar for JSON OBJECT if JSON OBJECT can not be defined.

I have tried GraphQLJSON from graphql-type-json as an npm package but when I use it, it throws an error message that field type must be an output type

const PostType = new GraphQLObjectType({
    name : "Post",
    fields : () => ({
        id : { type:  GraphQLInt }, 
        title : { type:  GraphQLString },
        slug : { type:  GraphQLString },
        content : { type:  GraphQLString },
        type : { type:  GraphQLString },
        template : { type:  GraphQLString },
        link : { type:  GraphQLString },
        doctor_id : { type:  GraphQLInt },
        categories : { type:  GraphQLString },
        publishdate: {type: GraphQLDate},
        metas: {type:  GraphQLJSON  }

    })
})

const mutation = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
        /********* Post */
        createPost: {
            type: schemaObjects.PostType,
            args: {
                title: { type: GraphQLString },
                slug: { type: GraphQLString },
                content: { type: GraphQLString },
                type: { type: GraphQLString },
                status: { type: GraphQLString },
                template: { type: GraphQLString },
                categories: { type: GraphQLString },
                doctor_id: { type: GraphQLInt },
                link: { type: GraphQLString },
                publishdate: { type: GraphQLDate},
                metas: {type:GraphQLJSON }
                ,
            resolve(parentValue, args) {
                let post = PostController.create(args);

                return post;
            }
        },
1
How are you importing GraphQLJSON? - Daniel Rearden
Thanks Daniel for reaching out. I am using const GraphQLJSON = require('graphql-type-json'); but I have tried it with import GraphQLJSON from 'graphql-type-json' which is not working as well. - Hamza J
Try const { GraphQLJSON } = require('graphql-type-json') - Daniel Rearden
@DanielRearden Thanks alot! This worked for me. - Hamza J

1 Answers

0
votes

The graphql-type-json module exports an object, which contains the two types added by the library. Assuming you're using plain old Node.js, you can import the type like this:

const GraphQLJSON = require('graphql-type-json').GraphQLJSON

or using destructuring syntax:

const { GraphQLJSON } = require('graphql-type-json')