Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对象作为突变中的输入变量:GraphQL-Apollo-React_Javascript_Node.js_Reactjs_Graphql_React Apollo - Fatal编程技术网

Javascript 对象作为突变中的输入变量:GraphQL-Apollo-React

Javascript 对象作为突变中的输入变量:GraphQL-Apollo-React,javascript,node.js,reactjs,graphql,react-apollo,Javascript,Node.js,Reactjs,Graphql,React Apollo,我在两个独立的repo中有一个React客户端项目和一个Node.js/graphqlapi 在我的React应用程序中,我想将一个对象作为变量类型传递到我的应用程序中。以下是我的突变情况: export const CREATE_SPEAKER = gql` input Expertise { title: String! domain: String! } mutation CreateSpeaker( $name: String! $age:

我在两个独立的repo中有一个React客户端项目和一个Node.js/graphqlapi

在我的React应用程序中,我想将一个对象作为变量类型传递到我的应用程序中。以下是我的突变情况:

export const CREATE_SPEAKER = gql`

  input Expertise {
    title: String!
    domain: String!
  }

  mutation CreateSpeaker(
    $name: String!
    $age: String!
    $nationality: String!
    $avatar: String!
    $expertise: Expertise!
  ) {
    createSpeaker(
      speakerInput: {
        name: $name
        age: $age
        nationality: $nationality
        avatar: $avatar
        expertise: $expertise
      }
    ) {
      name
      age
      nationality
      avatar
      expertise {
        title
        domain
      }
    }
  }
`;
在Node.js项目中,我有以下模式:

input SpeakerInput {
      name: String!
      age: String!
      expertise: ExpertiseInput!
      nationality: String!
      avatar: String
}

input ExpertiseInput {
      title: String!
      domain: String!
}
我的解析器:

createSpeaker: async args => {
    const { name, age, nationality, avatar, expertise } = args.speakerInput;

    const newSpeaker = new Speaker({
      name,
      age,
      nationality,
      avatar,
      expertise: {
        title: expertise.title,
        domain: expertise.domain
      }
    });

    try {
      return await newSpeaker.save();
    } catch (error) {
      throw ("Failed to create speaker:: ", error);
    }
}
但我在尝试创建演讲者时遇到以下错误:

未捕获(承诺中)不变冲突:架构类型定义不可用 在查询中允许。找到:“InputObjectTypeDefinition”


有什么建议/想法吗?

在向GraphQL服务发送请求时,您不能定义其他类型,您不需要——只需使用服务器上已定义的类型(在本例中,
ExpertiseInput

$expertise: ExpertiseInput!
但是,首先不需要使用这么多变量:

mutation CreateSpeaker($input: SpeakerInput!) {
  createSpeaker(speakerInput: $input) {
    name
    age
    nationality
    avatar
    expertise {
      title
      domain
    }
  }
}