2003003 ooo
2003003 ooo

Reputation: 1

KeystoneJs virtual field cannot use a custom field type

I need to display a json/object in a readonly form and I wrote a custom field type for it but then when I use a virtual to transform it to a string for passing it to the custom field type. I put the custom type into the args but it shows Error: Unknown type "JsonViewer".

Any idea of how to make it work?

const { Virtual } = require("@keystonejs/fields");
const JsonViewer = require("@/components/fields/jsonViewer");

module.exports = {
  fields: {
    requestData: {
      type: Virtual,
      args: [{ name: "requestData", type: "JsonViewer" }],
      resolver: async (json) => {
        return JSON.stringify(json);
      },
    },
  }
}

Upvotes: 0

Views: 192

Answers (1)

Gautam Singh
Gautam Singh

Reputation: 1138

You have to provide complex type details if they do not exist in the generated schema for graphql.

In your case as you are doing JSON.stringify you can use String return type like this. BTW String type is default return type and you should not need any type declaration there for string type.

Also there is no args option in keystone Virtual field.

const { Virtual } = require("@keystonejs/fields");

module.exports = {
  fields: {
    requestData: {
      type: Virtual,
      graphQLReturnType: `String`,
      resolver: async (json) => {
        return JSON.stringify(json);
      },
    },
  }
}

Upvotes: 1

Related Questions