突变中的嵌套对象

Posted

技术标签:

【中文标题】突变中的嵌套对象【英文标题】:Nested object in Mutation 【发布时间】:2018-10-20 15:33:24 【问题描述】:

在玩过 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:) 的类型必须是 Input Type 但得到:Name!。” 或者,如果我在 schema.js 中将类型从“type”更改为“input”我明白了 “错误:Mutation.addApplicant(name:) 的类型必须是输出类型,但得到了:Name!。”

我显然错过了什么!

【问题讨论】:

【参考方案1】:

你需要为突变定义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

【讨论】:

我试过了,我得到错误错误:申请人的类型必须是输出类型但得到:名称 更新了代码,我们需要不同的名称以及类型是名称,输入是名称输入 谢谢!我不明白我需要输入和类型

以上是关于突变中的嵌套对象的主要内容,如果未能解决你的问题,请参考以下文章

突变中的 GraphQL 嵌套节点 - 对象文字只能指定已知属性,并且类型中不存在“天”

突变中的 GraphQL 嵌套数据

GraphQL 嵌套文档在突变时返回 null

type-Graphql 中的嵌套查询和变异

如何处理 GraphQL 中的嵌套输入

嵌套对象的 Hasura GraphQL UPSERT 突变