Mutation

时间:2018-05-10 11:13:04

标签: node.js graphql apollo-server

在使用GraphQL之后,我试图深入挖掘一下。

我设法使用一个简单的对象进行变异,但是一旦我尝试使用嵌套对象,它就无法工作。

我试图让对象看起来像这样:

{   ID,   名称: {     第一,     持续   },   联系人:{     电话,     电子邮件   },   ... }

这是我的代码:

schema.js:

import { makeExecutableSchema } from 'graphql-tools';
import resolvers from './resolvers';

const typeDefs= [`
    type Name {
      first: String
      last: String
    }
    type Contacts {
      phone: String
      email: String
    }
    type Education {
      school: String
      graduation: Date
    }
    type Internship {
      duration: Int
      startDate: Date
    }
    type Applicant{
      id: String
      name: Name
      education: Education
      internship: Internship
      contacts: Contacts
    }
    type Query {
      allApplicants(searchTerm: String): [Applicant]
    }
    type Mutation {
      addApplicant(name: Name!, education: Education!, internship: Internship, contacts: Contacts): Applicant
    }
  `];

const schema = makeExecutableSchema({
  typeDefs,
  resolvers
});

export default schema

resolver.js:

import mongoose from 'mongoose';
import { GraphQLScalarType } from 'graphql';
import { Kind } from 'graphql/language';
import applicantModel from './models/applicant';
import technologyModel from './models/technology';
import companyModel from './models/company';


const resolvers = {
  Query: {
    allApplicants:(root,{searchTerm}) => {
      if (searchTerm !== '') {
        return applicantModel.find({$text: {$search: searchTerm}}).sort({lastName: 'asc'})
      } else {
        return applicantModel.find().sort({lastName: 'asc'})
      }
    }
  },
  Mutation: {
    addApplicant: (root,{name:{first, last}, contacts:{email, phone},education: {school},internship: {duration, startDate} }) => {
      const applicant = new applicantModel({name: {first: first, last: last} , contacts:{ email: email, phone: phone}, education: {school: school} , internship: {duration: duration ,startDate: new Date(startDate)}})
      return applicant.save();
    }
  }
}

export default resolvers;

我一直收到错误 "错误:Mutation.addApplicant(name :)的类型必须是输入类型但得到:名称!。" 或者,如果我从&#34更改类型;输入& #34;到"输入"在schema.js我得到 "错误:Mutation.addApplicant(name :)的类型必须是输出类型但得到:名称!。"

我显然错过了什么!

0 个答案:

没有答案