首页 > 解决方案 > GraphQL:错误:为构建模式提供的类型之一缺少名称

问题描述

所以我有这个奇怪的问题,我会很感激这里的每一个输入。我正在尝试在后端为我的 graphql 构建架构。它抛出错误为构建模式提供的类型之一缺少名称。但是每种类型都有一个名字。

我还在线检查了其他一些答案,并遵循这些答案,我认为我做的一切都是正确的,但我仍然遇到错误。

此外,如果我将 WatchQuery 中的getAllWatches和类型更改为例如,它可以正常工作。这意味着,如果我不使用自己创建的 GraphQL 对象,它可以正常工作。getOneWatchGraphQLString

我真的很感激这里有第二个敏锐的眼睛。所有的 ObjectTypes 也在同一个文件中。所以我只在这里导入了模型。以下是完整的错误:

/Users/charlesikulayo/Projects/watchy/GraphQLServer/node_modules/graphql/jsutils/devAssert.js:12
    throw new Error(message);
    ^

Error: One of the provided types for building the Schema is missing a name.
    at devAssert (/Users/charlesikulayo/Projects/watchy/GraphQLServer/node_modules/graphql/jsutils/devAssert.js:12:11)
    at new GraphQLSchema (/Users/charlesikulayo/Projects/watchy/GraphQLServer/node_modules/graphql/type/schema.js:191:42)
    at Object.<anonymous> (/Users/charlesikulayo/Projects/watchy/GraphQLServer/Schema/Schema.js:176:18)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
    at Module.require (node:internal/modules/cjs/loader:1013:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.<anonymous> (/Users/charlesikulayo/Projects/watchy/GraphQLServer/app.js:4:16)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47

下面是我的架构代码。提前为庞大的帖子道歉:):

const graphql = require("graphql");
const basicTD = require("../Models/BasicTDModel");
const technicalDetails = require("../Models/TechnicalDetails");
const image = require("../Models/ImageModel");
const watch = require("../Models/WatchModel");

const {
   GraphQLObjectType,
   GraphQLSchema,
   GraphQLID,
   GraphQLString,
   GraphQLInt,
   GraphQLList,
   GraphQLNonNull,
} = graphql;

//create the object type
const WatchType = new GraphQLObjectType({
   name: "Watch",
   fields: () => ({
      id: { type: { GraphQLID } },
      title: { type: { GraphQLString } },
      description: { type: { GraphQLString } },
      price: { type: { GraphQLInt } },
      images: {
         type: new GraphQLList(ImageType),
         resolve(parent, args) {
            return image.findByID(parent.imageId);
         },
      },
      model: { type: { GraphQLString } },
      collections: { type: { GraphQLString } },
      barcode: { type: { GraphQLString } },
      basicTD: {
         type: BasicTDType,
         resolve(parent, args) {
            return basicTD.findById(parent.basicTDID);
         },
      },
      technicalDetails: {
         type: TechnicalDetailsType,
         resolve(parent, args) {
            return technicalDetails.findById(parent.technicalDetailsID);
         },
      },
   }),
});

const ImageType = new GraphQLObjectType({
   name: "Images",
   fields: () => ({
      id: { type: { GraphQLID } },
      images: { type: GraphQLString },
      watch: {
         type: new GraphQLList(WatchType),
         resolve(parent, args) {
            return watch.find({ imageId: parent.id });
         },
      },
   }),
});
const BasicTDType = new GraphQLObjectType({
   name: "BasicTD",
   fields: () => ({
      id: { type: { GraphQLID } },
      battery: { type: { GraphQLString } },
      calender: { type: { GraphQLString } },
      waterProof: { type: { GraphQLString } },
      productOrigin: { type: { GraphQLString } },
      watch: {
         type: new GraphQLList(WatchType),
         resolve(parent, args) {
            return watch.find({ basicTDID: parent.id });
         },
      },
   }),
});
const TechnicalDetailsType = new GraphQLObjectType({
   name: "TechnicalDetails",
   fields: () => ({
      id: { type: { GraphQLID } },
      watchCase: { type: { GraphQLString } },
      clock: { type: { GraphQLString } },
      bracelet: { type: { GraphQLString } },
      calender: { type: { GraphQLString } },
      functions: { type: new GraphQLList(GraphQLString) },
      waterTightness: { type: { GraphQLString } },
      extraFunctions: { type: new GraphQLList(GraphQLString) },
      warranty: { type: { GraphQLString } },
      caseDiameter: { type: { GraphQLString } },
      scopeofDelivery: { type: { GraphQLString } },
      manufacturer: { type: { GraphQLString } },
      countryOfmanufacture: { type: { GraphQLString } },
      watch: {
         type: new GraphQLList(WatchType),
         resolve(parent, args) {
            return watch.find({ technicalDetailsID: parent.id });
         },
      },
   }),
});

//create the query
const WatchQuery = new GraphQLObjectType({
   name: "WatchQueryType",
   fields: {
      getAllWatches: {
         type: new GraphQLList(WatchType),
         resolve(parents, args) {
            return watch.find({});
         },
      },
      getOneWatch: {
         type: WatchType,
         args: { id: { type: GraphQLID } },
         resolve(parent, args) {
            return watch.findById(args.id);
         },
      },
   },
});

//create the mutation
const Mutation = new GraphQLObjectType({
   name: "Mutation",
   fields: {
      addWatch: {
         type: WatchType,
         args: {
            id: { type: new GraphQLNonNull(GraphQLID) },
            title: { type: new GraphQLNonNull(GraphQLString) },
            description: { type: new GraphQLNonNull(GraphQLString) },
            price: { type: new GraphQLNonNull(GraphQLInt) },
            images: { type: new GraphQLNonNull(GraphQLString) },
            model: { type: new GraphQLNonNull(GraphQLString) },
            collection: { type: new GraphQLNonNull(GraphQLString) },
            barcode: { type: new GraphQLNonNull(GraphQLString) },
            basicTD: { type: new GraphQLNonNull(GraphQLString) },
            technicalDetails: { type: new GraphQLNonNull(GraphQLString) },
         },
         resolve(parent, args) {
            let watch = new Watch({
               id: args.id,
               title: args.title,
               description: args.description,
               price: args.price,
               images: args.images,
               model: args.model,
               collection: args.collection,
               barcode: args.barcode,
               basicTD: args.basicTD,
               technicalDetails: args.technicalDetails,
            });
            watch.save();
         },
      },
   },
});

module.exports = new GraphQLSchema({
   query: WatchQuery,
   mutation: Mutation,
});```

标签: javascriptnode.jsexpressmongoosegraphql

解决方案


推荐阅读