反应apollo [GraphQL错误]:消息:无法在类型“ RootQueryType”上查询字段“ query”

时间:2020-03-26 20:52:15

标签: javascript reactjs graphql react-apollo apollo-client

请给我一些帮助。

我在root-dir / client / src / components中有这个组件

import React from "react";
import { useQuery } from "@apollo/react-hooks";
import { gql } from "apollo-boost";

const LAUNCHES_QUERY = gql`
  {
    query
    LaunchesQuery {
      launches {
        flight_number
        mission_name
        launch_date_local
        launch_success
      }
    }
  }
`;

function Launches() {
  const { loading, error, data } = useQuery(LAUNCHES_QUERY);

  if (loading) return <h4>Loading...</h4>;
  if (error) return <h4>Ooopss! Error :(</h4>; // * Hitting this when page loads
  console.log(data);

  return (
    <div>
      <h2>Works!</h2>
    </div>
  );
}

export default Launches;

从root-dir / server / gqlschema.js中的此文件获取数据

// Creating our Root Query here where we can have endpoints that resolve our data
const RootQuery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    // Get a list of launches
    launches: {
      type: new GraphQLList(LaunchType),
      resolve(parent, args) {
        return axios
          .get("https://api.spacexdata.com/v3/launches")
          .then(res => res.data);
      }
    },
    // Get a single launch
    launch: {
      type: LaunchType,
      args: {
        flight_number: { type: GraphQLInt }
      },
      resolve(parent, args) {
        return axios
          .get(`https://api.spacexdata.com/v3/launches/${args.flight_number}`)
          .then(res => res.data);
      }
    },
    // Get a list of rockets
    rockets: {
      type: new GraphQLList(RocketType),
      resolve(parent, args) {
        return axios
          .get("https://api.spacexdata.com/v3/rockets")
          .then(res => res.data);
      }
    },
    // Get a single rocket
    rocket: {
      type: RocketType,
      args: {
        id: { type: GraphQLString }
      },
      resolve(parent, args) {
        return axios
          .get(`https://api.spacexdata.com/v3/rockets/${args.id}`)
          .then(res => res.data);
      }
    }
  }
});

module.exports = new GraphQLSchema({ query: RootQuery });

但是当我加载浏览器时,我遇到了一个错误(请参阅*以获得参考)并在我的控制台中看到以下内容

[GraphQL error]: Message: Cannot query field "query" on type "RootQueryType"., Location: [object Object], Path: undefined
[GraphQL error]: Message: Cannot query field "LaunchesQuery" on type "RootQueryType". Did you mean "launches"?, Location: [object Object], Path: undefined
[Network error]: ServerError: Response not successful: Received status code 400

有什么主意为什么我会出现此错误而不用控制台记录任何数据吗?

0 个答案:

没有答案