0

我无法在我的应用程序中添加对象,该应用程序基于一些教程(但是我可以使用 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)中获取任何特定信息。

4

1 回答 1

0

好吧,这比我想的更简单。问题是我忘记将 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
    }
  }
`;
于 2020-06-05T09:18:18.080 回答