admin管理员组

文章数量:1323335

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!
    pany: 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
    pany: 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/lify/functions/graphql

  3. Sample GraphQL query

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

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!
    pany: 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
    pany: 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/lify/functions/graphql

  3. Sample GraphQL query

{
 listings {
     id
     title
     description
     url
     pany {
        name
        url
     }
    }
}
Share Improve this question asked Aug 16, 2020 at 10:23 Ayman ArifAyman Arif 1,6993 gold badges19 silver badges43 bronze badges 4
  • Where are you creating your context object? – goto Commented Aug 16, 2020 at 12:09
  • I am using user from db.js as part of Apollo's resolver. – Ayman Arif Commented Aug 16, 2020 at 14:30
  • Looks like you're trying to use the third argument of the resolver, which is context - apollographql./docs/apollo-server/data/resolvers/…, but you're never adding a context initialization function to the ApolloServer constructor. – goto Commented Aug 16, 2020 at 16:14
  • did you manage to find a solution? – M.Ionut Commented Mar 3, 2022 at 20:13
Add a ment  | 

2 Answers 2

Reset to default 0
return user.getListings();

you probably mean User, because user is undefined

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.

本文标签: javascriptGraphQL TypeError Cannot read property of undefinedStack Overflow