Expected Iterable, but did not find one for field \"Query.customers\

3.9k views Asked by At

I am trying to learn GraphQL but I am getting an error when I try to do {customers{id, name}} for example and also when I try to use the mutation or search for a specific customer. Can you help me? I console log the data on the resolvers and everything is fine but I guess the problem is with the data type. How can I fix this?

This is my typeDefs.js file:

const { gql } = require("apollo-server-express");

const typeDefs = gql`
  type Customer {
    id: ID!
    name: String!
    email: String!
    age: Int!
  }

  type Query {
    customer(id: ID!): Customer
    customers: [Customer]!
  }

  type Mutation {
    addCustomer(name: String!, email: String!, age: String!): Customer!
    editCustomer(
      id: ID!
      name: String!
      email: String!
      age: String!
    ): Customer!
    deleteCustomer(id: ID!): Customer!
  }
`;

module.exports = typeDefs;

This is my resolvers.js file:

const axios = require("axios");

const resolvers = {
  Query: {
    customer: async (_, { id }) => {
      const data = await axios.get("http://localhost:3000/customers/" + id);
      return data;
    },
    customers: async () => {
      const data = await axios.get("http://localhost:3000/customers/");
      return data;
    },
  },

  Mutation: {
    addCustomer: async (_, { name, email, age }) => {
      const response = await axios.post("http://localhost:3000/customers", {
        name,
        email,
        age,
      });
      return response.data;
    },

    editCustomer: async (_, { id, name, email, age }) => {
      const response = await axios.patch(
        "http://localhost:3000/customers" + id,
        {
          name,
          email,
          age,
        }
      );
      return response.data;
    },
  },
};

module.exports = resolvers;

My data is in a json-server with this format:

{
  "customers": [
    {
      "id": "1",
      "name": "John Doe",
      "email": "[email protected]",
      "age": 36
    },
    {
      "id": "2",
      "name": "Keith Wilson",
      "email": "[email protected]",
      "age": 50
    },
    {
      "id": "3",
      "name": "Tom Jones",
      "email": "[email protected]",
      "age": 23
    },
    {
      "id": "5",
      "name": "Jen Thompson",
      "email": "[email protected]",
      "age": 22
    }
  ]
}

0

There are 0 answers