GraphQL Swapi api - getting error when fetching

63 views Asked by At

I tried my luck with this mini graphql api,

but before getting into the point of this question i have some questions before:

  1. When using swapi api for example and more specific i want to fetch films , when i write the filmType GraphQLObjectType do i have to add into fields all of the attributes that is shown under films? enter image description here

Or i can choose the data i want to fetch even though when querying I will do it anyway

And now lets see the code and the error message im getting:

film.js:

const axios = require("axios");

const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLID,
  GraphQLInt,
  GraphQLSchema,
} = require("graphql");

const filmType = new GraphQLObjectType({
  name: "Film",
  fields: () => ({
    title: {
      type: GraphQLString,
    },
    episode_id: {
      type: GraphQLInt,
    },
    opening_crawl: {
      type: GraphQLString,
    },
  }),
});

const RootQuery = new GraphQLObjectType({
  name: "RootQuery",
  fields: {
    film: {
      type: filmType,
      args: {
        id: GraphQLInt,
      },
      resolve: (parent, args) => {
        try {
          return axios
            .get(`http://swapi.co/api/films/${args.id}`)
            .then((res) => res.data);
        } catch (err) {
          throw new Error("Error fetching film from swapi.dev");
        }
      },
    },
  },
});

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

And App.js:

const express = require("express");
const { graphqlHTTP } = require("express-graphql");
const { graphql } = require("graphql");

const graphqlSchema = require("./graphql/film");

const app = express();

app.use(
  "/graphql",
  graphqlHTTP({
    schema: graphqlSchema,
    graphiql: true,
  })
);

app.listen(3000);
console.log(`Listening to port 3000`);

And the query inside http://localhost:3000/graphiql

query {
  film(id: 1){
    title
    episode_id
    opening_crawl
  }
}

The error Im getting:

{
  "errors": [
    {
      "message": "The type of RootQuery.film(id:) must be Input Type but got: undefined."
    }
  ]
}
0

There are 0 answers