0
votes

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: http://localhost:8888/.netlify/functions/graphql

  3. Sample GraphQL query

{
 listings {
     id
     title
     description
     url
     company {
        name
        url
     }
    }
}
2
Where are you creating your context object? - goto1
I am using user from db.js as part of Apollo's resolver. - Ayman Arif
Looks like you're trying to use the third argument of the resolver, which is context - apollographql.com/docs/apollo-server/data/resolvers/…, but you're never adding a context initialization function to the ApolloServer constructor. - goto1

2 Answers

0
votes
return user.getListings();

you probably mean User, because user is undefined

0
votes

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.