gpt4 book ai didi

node.js - 错误 : "user" defined in resolvers, 但不在架构中

转载 作者:搜寻专家 更新时间:2023-11-01 00:34:52 24 4
gpt4 key购买 nike

我正在尝试为 Apollo Server 设置架构,但遇到错误。

我正在尝试的实际上是尝试使用此处找到的建议来修复常见且更无用的消息:

https://github.com/apollographql/apollo-server/issues/1998

我不太确定如何减少此处的代码,因为我无法找出比这更多的错误。

架构.js

import { makeExecutableSchema } from 'apollo-server-express'
import ApplicationSchema from './types'
import Resolvers from './resolvers'

export default makeExecutableSchema({
typeDefs: ApplicationSchema,
resolvers: Resolvers
})

解析器/index.js

import user from './user'

export default {
user
}

解析器/user.js

import Joi from 'joi'
import mongoose from 'mongoose'
import { UserInputError } from 'apollo-server-express'
import { signUp, signIn } from '../validators'
import { User } from '../schemata' // mongoose schema
import * as Authentication from '../authentication'

export default {
Query: {
me: (root, args, { request }, info) => {
Authentication.checkSignedIn(request)

return User.findbyId(request.session.userId)
},

users: (root, args, { request }, info) => {
Authentication.checkSignedIn(request)
User.find({})
},

user: (root, args, { request }, info) => {
const { id } = args

Authentication.checkSignedIn(request)

if (!mongoose.Types.ObjectId.isValid(id)) {
throw new UserInputError(`${id} is not a valid user ID.`)
}

return User.findById(id)
}
},

Mutation: {
signup: async (root, args, { request }, info) => {
Authentication.checkSignedOut(request)
await Joi.validate(args, signUp, { abortEarly: false })
return User.create(args)
},

signIn: async (root, args, { request }, info) => {
const { userId } = request.session

if (userId) return User.findById(userId)

await Joi.validate(args, signIn, { abortEarly: false })

const user = await Authentication.attemptSignIn(args)

request.session.userId = user.id

return user
},

signOut: async (root, args, { request, response }, info) => {
Authentication.checkSignedIn(request)

return Authentication.signOut(request, response)
}
}
}

类型/index.js

import root from './root'
import user from './user'

export default [
root,
user
]

类型/根,js

import { gql } from 'apollo-server-express'

export default gql`
type Query {
_: String
}

type Mutation {
_: String
}

type Subscription {
_: String
}

类型/user.js

import { gql } from 'apollo-server-express'

export default gql`
type User {
id: ID!
email: String!
username: String!
name: String!
password: String!
createdAt: String!
}

extend type Query {
me: User
user(id: ID!): User
users: [User!]!
}

extend type Mutation {
signUp(email: String!, username: String!, name: String!): User
signIn(email: String!, password: String!): User
signOut: Boolean
}

希望 schema.js 运行时不会出现错误,并且生成的可执行模式能够很好地与 Apollo Server 配合使用。

最佳答案

您的 resolvers 对象的结构应该类似于 types/user.js 中的内容,即:

{
Query: {
// Query fields
},
Mutation: {
// Mutation fields
},
// etc.
}

resolvers 对象应该是类型名称的映射。相反,由于您的导入设置方式,您最终会得到:

{
user: {
Query: {
// Query fields
},
Mutation: {
// Mutation fields
},
// etc.
}
}

因为这是您作为解析器传递的内容,Apollo 假定您正在尝试为名为user 的类型映射解析器,该类型不存在在您的架构中,因此它会抛出您所看到的错误。

如果你想合并多个解析器对象,你需要使用类似 lodashmerge 的方式递归合并它们。 :

import user from './user'
import foo from './foo'
import _ from 'lodash'

export default _.merge(
user,
foo
)

关于node.js - 错误 : "user" defined in resolvers, 但不在架构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56049480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com