Error: NEXUS__UNKNOWN__TYPE was already defined and imported as a type, check the docs for extending types
⬆︎ DateTime 型と同じく、Json 型も追加する必要があります。
( ※ DateTime 型 設定記事は こちら )
パッケージを追加する
graphql-scalars パッケージをインストールして設定を行います。
try🐶everything backend$ yarn add graphql-scalars
Json を定義する
src/graphql/Json.ts ファイルを作成します。
// src/graphql/Json.ts
import { asNexusMethod } from "nexus";
import { JSONObjectResolver } from "graphql-scalars";
export const Json = aNexusMethod(JSONObjectResolver, "json");
src/graphql/index.ts を編集します。(Line 6)
// src/graphql/index.ts
export { DateTime } from "./DateTime";
export { User } from "./User";
export { Post } from "./Post";
export { Json } from "./Json";
export { Profile } from "./Profile";
src/graphql/Post.ts で例えると、Line 8、9 のような書き方で使えます。
// src/graphql/Post.ts
...
export const Post = objectType({
name: "Post",
definition(t) {
t.nonNull.int("id");
t.nonNull.field("jsondata1", { type: "JSONObject" });
t.nonNull.field("jsondata1", { type: "JSONObject" });
t.nonNull.string("title"
...
まとめ
注意すべきことは、{ type: "Json" } ではなく、{ type: "JSONObject" }で定義することです!

コメント