Mutation 中的嵌套对象

Nested object in Mutation

在尝试了 GraphQL 之后,我想深入挖掘一下。

我设法用一个简单的对象进行了修改,但是当我尝试使用嵌套对象时它就不起作用了。

我正在尝试让对象看起来像这样:

{ ID, 姓名: { 第一的, 最后的 }, 联系人:{ phone, 电子邮件 }, ... }

这是我的代码:

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;

我一直收到错误 "Error: The type of Mutation.addApplicant(name:) must be Input Type but got: Name!." 或者,如果我在 schema.js 中将类型从 "type" 更改为 "input" 我得到 "Error: The type of Mutation.addApplicant(name:) must be Output Type but got: Name!."

我显然遗漏了一些东西!

您需要为突变定义 input 而不是像

这样的类型
import { makeExecutableSchema } from 'graphql-tools';
import resolvers from './resolvers';

const typeDefs= [`
    type Name {
      first: String
      last: String
    }
    input NameInput {
      first: String
      last: String
    }
    type Contacts {
      phone: String
      email: String
    }
    input ContactsInput {
      phone: String
      email: String
    }
    type Education {
      school: String
      graduation: Date
    }
    input EducationInput {
      school: String
      graduation: Date
    }
    type Internship {
      duration: Int
      startDate: Date
    }
    input InternshipInput {
      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: NameInput!, education: EducationInput!, internship: InternshipInput, contacts: ContactsInput): Applicant
    }
  `];

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

export default schema