为嵌套模式 graphql/apollo/react 添加突变

Posted

技术标签:

【中文标题】为嵌套模式 graphql/apollo/react 添加突变【英文标题】:Add mutation for nested schema graphql/apollo/react 【发布时间】:2020-09-23 04:58:05 【问题描述】:

我无法在我的应用程序中添加对象,这是基于一些教程的(但是我可以使用 graphql 游乐场来完成)。 这是我的架构并在 AddLaunch 组件中使用突变。

const ADD_LAUNCH = gql`
  mutation($flight_number: Int!, 
    $mission_name: String!,
    $launch_year: String!,
    $launch_success: Boolean!,
    $rocket: addRocket!) 
    addLaunch(flight_number: $flight_number,
      mission_name: $mission_name,
      launch_year: $launch_year,
      launch_success: $launch_success,
      rocket:
        rocket_id: $rocket_id,
        rocket_name: $rocket_name,
        rocket_type: $rocket_type
      
      ) 
        flight_number
        mission_name
        launch_year
        launch_success
        rocket
          rocket_id
          rocket_name
          rocket_type
        
    
  
`;

...

const [newLaunch, setNewLaunch] = useState(
    flight_number:'',
    mission_name: '',
    launch_year: '',
    launch_success: null,
    rocket_id:'',
    rocket_name:'',
    rocket_type:''
  );

const [addLaunch] = useMutation(ADD_LAUNCH);

...

const onSubmit = async e => 
    e.preventDefault();
    await addLaunch( variables:  
      flight_number: newLaunch.flight_number, 
      mission_name: newLaunch.mission_name,
      launch_year: newLaunch.launch_year,
      launch_success: newLaunch.launch_success,
      rocket:
        rocket_id: newLaunch.rocket_id,
        rocket_name: newLaunch.rocket_name,
        rocket_type: newLaunch.rocket_type
        
       
    ); 
    refetchLaunches()
  

我的 typeDefs:

const typeDefs = gql`
type Rocket 
    rocket_id: String
    rocket_name: String
    rocket_type: String
  ,
type Launch 
    _id: ID,
    flight_number: Int,
    mission_name: String,
    launch_year: String,
    launch_date_local: String,
    launch_success: Boolean,
    rocket: Rocket
  ,
type Query 
launches: [Launch]
    launch(flight_number:Int!): Launch
  
input addRocket 
    rocket_id: String!
    rocket_name: String!
    rocket_type: String!
   
type Mutation 
addLaunch(flight_number: Int!,
        mission_name: String!,
        launch_year: String!,
        launch_success: Boolean!,
        rocket: addRocket!
         ): Launch
`

解析器(一个突变):

addLaunch: (parent, launch) => 
      // Create a new record in the database
      const newLaunch = new Launch( 
        flight_number: launch.flight_number, 
        mission_name: launch.mission_name, 
        launch_year: launch.launch_year, 
        launch_success: launch.launch_success, 
        rocket: launch.rocket 
      );
      // Save the record and return it
      return newLaunch.save();
    

我尝试了很多方法,但我无法从错误(错误请求 400)中获取任何具体信息。

【问题讨论】:

将对象传递给rocket 变量(带有addRocket 结构)...在解析器中在newLaunch 之前在db 中创建new Rocket 对象(使用创建的火箭[id/relation]) 我已经找到了这个问题的一半答案。原来我忘记了数字类型的输入总是返回字符串,也就是说,如果我写 42 它返回“42”(它只强制写数字字符),但我仍然没有解决我的问题,但也许一些在我之前的尝试中,我正确地写了它,但是这个 flight_number 参数在一开始就阻止了它。我稍后试试 【参考方案1】:

好吧,这比我想的还要简单。 问题是我忘记将 flight_number 输入值从字符串解析为 int。变异比我想象的还要简单。

const ADD_LAUNCH = gql`
  mutation($flight_number: Int!, 
    $mission_name: String!,
    $launch_year: String!,
    $launch_success: Boolean!,
    $rocket: addRocket!) 
    addLaunch(flight_number: $flight_number,
      mission_name: $mission_name,
      launch_year: $launch_year,
      launch_success: $launch_success,
      rocket: $rocket
      ) 
        flight_number
        mission_name
        launch_year
        launch_success
    
  
`;

【讨论】:

以上是关于为嵌套模式 graphql/apollo/react 添加突变的主要内容,如果未能解决你的问题,请参考以下文章

猫鼬模式中的嵌套地图

用于多个对象数组的猫鼬嵌套模式

Meanjs 无法将图像保存到嵌套模式中

嵌套表,删除包含模式的内表

用于预订、嵌套或引用的 MongoDB 模式?

用于预订、嵌套或引用的 MongoDB 模式?