Я пытаюсь настроить свою схему для Apollo Server, и у меня возникает ошибка.
То, что я пытаюсь, на самом деле является попыткой исправить распространенное и еще более бесполезное сообщение, используя совет, найденный здесь:
https://github.com/apollographql/apollo-server/issues/1998
Я не совсем уверен, как уменьшить код здесь, так как я не смог изолировать ошибку больше, чем это.
schema.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
}
resolvers / 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.