Вложенный объект в мутации - PullRequest
0 голосов
/ 10 мая 2018

После игры с GraphQL я пытаюсь покопаться немного глубже.

Мне удалось обработать мутацию с простым объектом, но как только я пытаюсь использовать вложенный объект, он не работает.

Я пытаюсь получить объект, похожий на этот:

{id, name: {first, last}, контакты: {phone, email}, ...}

Вот мой код:

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 :) должен иметь тип ввода, но получил: Name!. " или, если я изменил тип с" type "на" input "в schema.js, я получил " Ошибка:Тип Mutation.addApplicant (name :) должен иметь тип Output, но получил: Name!. "

Я явно что-то упустил!

1 Ответ

0 голосов
/ 10 мая 2018

Вы должны определить 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
...