简体   繁体   English

在 Express GraphQL 中出错:架构必须包含唯一命名的类型,但包含多个名为“String”的类型

[英]Getting error in Express GraphQL: Schema must contain uniquely named types but contains multiple types named "String"

So I was trying to create a Pinterest Clone using GraphQL and I am stuck with this error.所以我试图使用 GraphQL 创建一个 Pinterest 克隆,但我遇到了这个错误。

Project Structure looks something like this项目结构看起来像这样

vscode项目结构

There are 2 models, User and Pins有 2 个模型,User 和 Pins

Pin Model引脚型号

const mongoose = require('mongoose');

const pinSchema = new mongoose.Schema({
  title: {
    type: String,
    required: [true, 'Title is Required'],
    unique: [true, 'Title should be unique'],
  },
  imageUrl: {
    type: String,
    required: [true, 'Image URL is Required'],
  },
  description: {
    type: String,
  },
  link: {
    type: String,
  },
  userId: {
    type: mongoose.Schema.Types.ObjectId, // to store which user
    required: true,
  },
  createdAt: {
    type: Date,
    default: Date.now(),
  },
});

const Pin = mongoose.model('Pin', pinSchema);
module.exports = Pin;

User Model用户模型

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, 'Name is Required'],
  },
  userName: {
    type: String,
    required: [true, 'Username is Required'],
    unique: [true, 'Username should be unique'],
  },
  email: {
    type: String,
    required: [true, 'Email is Required'],
    unique: [true, 'Email should be unique'],
  },
  password: {
    type: String,
    required: [true, 'Password is Required'],
  },
  createdPins: {
    type: [mongoose.Schema.Types.ObjectId], // store all pins created by this user
  },
  savedPins: {
    type: [mongoose.Schema.Types.ObjectId], // store all pins saved by this user
  },
});

const User = mongoose.model('User', userSchema);
module.exports = User;

Graphql PinType Graphql 引脚类型

const { GraphQLObjectType, GraphQLID, GraphQLString } = require('graphql');
const User = require('../models/UserModel');

const PinType = new GraphQLObjectType({
  name: 'Pin',
  fields: () => ({
    id: { type: GraphQLID },
    title: { type: GraphQLString },
    imageUrl: { type: GraphQLString },
    description: { type: GraphQLString },
    link: { type: GraphQLString },
    user: {
      type: UserType,
      resolve(parent, args) {
        return User.findById(parent.userId);
      },
    },
    createdAt: { type: String },
  }),
});

module.exports = PinType;

const UserType = require('./UserSchema');

Graphql UserType Graphql 用户类型

const {
  GraphQLObjectType,
  GraphQLID,
  GraphQLString,
  GraphQLList,
} = require('graphql');

const UserType = new GraphQLObjectType({
  name: 'User',
  fields: () => ({
      id: { type: GraphQLID },
      name: { type: GraphQLString },
      userName: { type: GraphQLString },
      createdPins: { type: new GraphQLList(PinType) },
      savedPins: { type: new GraphQLList(PinType) },
  }),
});

module.exports = UserType;

const PinType = require('./PinSchema');

Query and Mutation: schema.js file查询和变更:schema.js 文件

const {
  GraphQLObjectType,
  GraphQLList,
  GraphQLID,
  GraphQLSchema,
  GraphQLNonNull,
  GraphQLString,
} = require('graphql');

const User = require('../models/UserModel');
const Pin = require('../models/PinModel');

const UserType = require('./UserSchema');
const PinType = require('./PinSchema');

// Query
const RootQuery = new GraphQLObjectType({
  name: 'RootQuery',
  fields: {
    // Get all Users
    users: {
      type: new GraphQLList(UserType),
      resolve(parent, args) {
        return User.find();
      },
    },
    // Get a Single User
    user: {
      type: UserType,
      args: { id: { type: GraphQLID } },
      resolve(parent, args) {
        return User.findById(args.id);
      },
    },
    // Get all Pins
    pins: {
      type: new GraphQLList(PinType),
      resolve(parent, args) {
        return Pin.find();
      },
    },
    // Get a Single Pin
    pin: {
      type: PinType,
      args: { id: { type: GraphQLID } },
      resolve(parent, args) {
        return Pin.findById(args.id);
      },
    },
  },
});

// Mutation
const Mutation = new GraphQLObjectType({
  name: 'Mutation',
  fields: {
    // Create User
    createUser: {
      type: UserType,
      args: {
        name: { type: new GraphQLNonNull(GraphQLString) },
        userName: { type: new GraphQLNonNull(GraphQLString) },
        email: { type: new GraphQLNonNull(GraphQLString) },
        password: { type: new GraphQLNonNull(GraphQLString) },
      },
      resolve(parent, args) {
        return User.create({
          name: args.name,
          userName: args.userName,
          email: args.email,
          password: args.password,
        });
      },
    },
    // Delete User
    deleteUser: {
      type: UserType,
      args: {
        id: { type: new GraphQLNonNull(GraphQLID) },
      },
      resolve(parent, args) {
        // delete all pins created by this user
        Pin.find({ userId: args.id }).then((pins) => {
          pins.forEach((pin) => {
            pin.remove();
          });
        });
        return User.findByIdAndRemove(args.id);
      },
    },
    // Create a Pin
    createPin: {
      type: PinType,
      args: {
        title: { type: new GraphQLNonNull(GraphQLString) },
        imageUrl: { type: new GraphQLNonNull(GraphQLString) },
        description: { type: GraphQLString },
        link: { type: GraphQLString },
        userId: { type: new GraphQLNonNull(GraphQLID) },
      },
      resolve(parent, args) {
        return Pin.create({
          title: args.title,
          imageUrl: args.imageUrl,
          description: args.description,
          link: args.link,
          userId: args.userId,
        });
      },
    },
    // Update a Pin
    updatePin: {
      type: PinType,
      args: {
        id: { type: new GraphQLNonNull(GraphQLID) },
        title: { type: GraphQLString },
        imageUrl: { type: GraphQLString },
        description: { type: GraphQLString },
        link: { type: GraphQLString },
      },
      resolve(parent, args) {
        return Pin.findByIdAndUpdate(
          args.id,
          {
            $set: {
              title: args.title,
              imageUrl: args.imageUrl,
              description: args.description,
              link: args.link,
            },
          },
          { new: true }
        );
      },
    },
    // Delete a Pin
    deletePin: {
      type: PinType,
      args: {
        id: { type: new GraphQLNonNull(GraphQLID) },
      },
      resolve(parent, args) {
        // remove this pin from the createdPins of the user
        User.updateMany(
          {},
          {
            $pullAll: {
              createdPins: [args.id],
            },
          }
        );
        // delete this pin
        return Pin.findByIdAndRemove(args.id);
      },
    },
  },
});

const schema = new GraphQLSchema({
  query: RootQuery,
  mutation: Mutation,
});

module.exports = schema;

Getting this error收到此错误

Error: Schema must contain uniquely named types but contains multiple types named "String".错误:架构必须包含唯一命名的类型,但包含多个名为“String”的类型。 at new GraphQLSchema (D:\Projects\Pinterest Clone\server\node_modules\graphql\type\schema.js:219:15) at Object.<anonymous> (D:\Projects\Pinterest Clone\server\schemas\schema.js:159:16) at Module._compile (node:internal/modules/cjs/loader:1149:14) at Module._extensions..js (node:internal/modules/cjs/loader:1203:10) at Module.load (node:internal/modules/cjs/loader:1027:32) at Module._load (node:internal/modules/cjs/loader:868:12) at Module.require (node:internal/modules/cjs/loader:1051:19) at require (node:internal/modules/cjs/helpers:103:18) at Object.<anonymous> (D:\Projects\Pinterest Clone\server\index.js:6:16) at Module._compile (node:internal/modules/cjs/loader:1149:14)在新的 GraphQLSchema (D:\Projects\Pinterest Clone\server\node_modules\graphql\type\schema.js:219:15) 在 Object.<anonymous> (D:\Projects\Pinterest Clone\server\schemas\schema.js :159:16) 在 Module._compile (node:internal/modules/cjs/loader:1149:14) 在 Module._extensions..js (node:internal/modules/cjs/loader:1203:10) 在 Module.load (node:internal/modules/cjs/loader:1027:32) 在 Module._load (node:internal/modules/cjs/loader:868:12) 在 Module.require (node:internal/modules/cjs/loader:1051 :19) at require (node:internal/modules/cjs/helpers:103:18) at Object.<anonymous> (D:\Projects\Pinterest Clone\server\index.js:6:16) at Module._compile (节点:内部/模块/cjs/加载器:1149:14)

Node.js v18.10.0节点.js v18.10.0

Tried to search for this and found many people faced similar kind of issues, but I couldn't solve it.试图搜索这个,发现很多人都面临类似的问题,但我无法解决。

Found the Bug.发现了错误。 It was in the Graphql PinType file.它在 Graphql PinType 文件中。 While defining the schema, I used "String" instead of "GraphqlString" in the field "createdAt" which was causing the error.在定义架构时,我在导致错误的“createdAt”字段中使用了“String”而不是“GraphqlString”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Apollo Express GraphQL 中出现错误:错误:模式必须包含唯一命名的类型,但包含多个名为“DateTime”的类型 - Getting error in Apollo Express GraphQL: Error: Schema must contain uniquely named types but contains multiple types named "DateTime" 无法在GraphQL架构中嵌套类型 - Unable to nest Types in GraphQL Schema 为什么会出现错误“必须提供查询字符串”。express-graphql? - Why I'm getting the error “Must provide query string.” express-graphql? GraphQL 指定多种类型的数组 - GraphQL specify array of multiple types 使用 AJAX 直接上传到亚马逊 S3 返回错误:Bucket POST 必须包含名为“key”的字段 - Upload Directly to amazon S3 using AJAX returning error: Bucket POST must contain a field named 'key' 如何从graphql模式获取所有类型的列表? - How can I get list of all types from graphql schema? GraphQL:为构建架构提供的类型之一缺少名称 - GraphQL: One of the provided types for building the Schema is missing a name 不包含名为的导出 - does not contain an export named FlowType:为什么函数类型具有命名参数? 他们的目的是什么? - FlowType : why function types have named parameters ? What is their purpose? 相互依赖的graphQl类型 - interdependent graphQl types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM