GraphQL: TypeError: Cannot read property of undefined

GraphQL: TypeError: Cannot read property of undefined


2

I have an Apollo GraphQL projects where I have created my own Query and Mutations. I have done using mock data and Query and Mutation works fine. But when I am trying to do with Sequelize ORM, I am getting the error

"TypeError: Cannot read property 'getListings' of undefined",
            "    at listings (/home/ayman/Desktop/apollo-graphql/graphql-app/functions/graphql.js:50:19)",
            "    at field.resolve (/home/ayman/Desktop/apollo-graphql/graphql-app/node_modules/graphql-extensions/dist/index.js:134:26)"

Query and Mutations in graphql.js:

const { ApolloServer, gql} = require("apollo-server-lambda");
const { Listing, User } = require("../db");
const jwt = require("jsonwebtoken");

const typeDefs = gql`
  type Query {
    listings: [Listing!]!
  }
  type Mutation {
    createListing(input: CreateListingInput!): Listing!
  }
  input CreateListingInput {
    title: String!
    description: String
    url: String!
    notes: String
  }
  type Contact {
    id: ID!
    name: String!
    company: Company
    email: String
    notes: String
  }
  type Company {
    id: ID!
    name: String!
    logo: String
    listings: [Listing!]!
    url: String
  }
  type Listing {
    id: ID!
    title: String!
    description: String
    url: String!
    notes: String
    company: Company
    contacts: [Contact!]!
  }
`;

const resolvers = {
  Query: {
    listings(_, __, { user }) {
      return user.getListings();
    },
  },
  Mutation: {
    createListing(_, { input }, { user }) {
      return Listing.create({ ...input, userId: user.id });
    },
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
});

exports.handler = server.createHandler();

I have Sequilize along with Postgres database:

db.js

const Sequelize = require("sequelize");

const sequelize = new Sequelize(process.env.DB_CONNECTION_STRING, {
  dialect: "pg",
  dialectModule: require('pg'),
  dialectOptions: {
    ssl: true,
  },
});

class User extends Sequelize.Model {}
User.init(
  {
    email: Sequelize.STRING,
    password: Sequelize.STRING,
  },
  {
    sequelize,
    modelName: "user",
  }
);

class Listing extends Sequelize.Model {}
Listing.init(
  {
    title: Sequelize.STRING,
    description: Sequelize.TEXT,
    url: Sequelize.STRING,
    notes: Sequelize.TEXT,
  },
  {
    sequelize,
    modelName: "listing",
  }
);

Listing.belongsTo(User);
User.hasMany(Listing);

exports.sequelize = sequelize;
exports.User = User;
exports.Listing = Listing;

Github Link

  1. Run using netlify dev

  2. Go to URL: https://localhost:8888/.netlify/functions/graphql

  3. Sample GraphQL query

{
 listings {
     id
     title
     description
     url
     company {
        name
        url
     }
    }
}

Share
Improve this question

4

  • Where are you creating your context object?

    – goto

    Aug 16, 2020 at 12:09

  • I am using user from db.js as part of Apollo's resolver.

    – Ayman Arif

    Aug 16, 2020 at 14:30

  • Looks like you're trying to use the third argument of the resolver, which is contextapollographql.com/docs/apollo-server/data/resolvers/…, but you're never adding a context initialization function to the ApolloServer constructor.

    – goto

    Aug 16, 2020 at 16:14


  • did you manage to find a solution?

    – M.Ionut

    Mar 3, 2022 at 20:13

2 Answers
2

Reset to default


0

return user.getListings();

you probably mean User, because user is undefined

Share
Improve this answer

1

  • Tried it but gives same error. return user.getListings(); comes under custom Apollo Resolver to fetch details from datasources.

    – Ayman Arif

    Aug 16, 2020 at 11:29


0

I see, you are trying to access user object from context. Please check the context definition. It should return an object containing user object explicitly.

Share
Improve this answer

2

  • How do i do that?

    – Ayman Arif

    Aug 16, 2020 at 14:29

  • Please check the code where the Apollo server is confugured. Context is passed as a key to apollo server configuration.

    – Dheeraj Bharsiya

    Aug 16, 2020 at 16:59




Not the answer you're looking for? Browse other questions tagged

or ask your own question.

Leave a Reply

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