GraphQL Swapi api – getting error when fetching

GraphQL Swapi api – getting error when fetching


1

Im kinda new to graphql but 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?

    GraphQL Swapi api - getting error when fetching

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(`https://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 https://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."
    }
  ]
}

Thank you for all the helpers (:

1 Answer
1


0

Typo issue args id :

before :

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

after:

const RootQuery = new GraphQLObjectType({
  name: "RootQuery",
  fields: {
    film: {
      type: filmType,
      args: {
        id: { type: GraphQLID },
      },
      resolve: (parent, args) => {
        return axios
          .get(`https://swapi.co/api/films/${args.id}`)
          .then((res) => res.data)

          .catch((err) => {
            console.log("error getching from swapi/films: ", err);
          });
      },
    },
  },
});

the query now work but i get no data just plain null on every field :

{
  "data": {
    "film": {
      "title": null,
      "episode_id": null,
      "opening_crawl": null,
      "director": null
    }
  }
}

why is that ?



Leave a Reply

Your email address will not be published. Required fields are marked *