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;
-
Run using
netlify dev -
Go to URL: https://localhost:8888/.netlify/functions/graphql
-
Sample GraphQL query
{
listings {
id
title
description
url
company {
name
url
}
}
}
4
2 Answers
Reset to default
0
return user.getListings();
you probably mean User, because user is undefined
1
-
Tried it but gives same error.
return user.getListings();comes under custom Apollo Resolver to fetch details from datasources.– Ayman ArifAug 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.
2
-
How do i do that?
– Ayman ArifAug 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 BharsiyaAug 16, 2020 at 16:59
Not the answer you're looking for? Browse other questions tagged
or ask your own question.
or ask your own question.



Where are you creating your
contextobject?Aug 16, 2020 at 12:09
I am using
userfromdb.jsas part of Apollo's resolver.Aug 16, 2020 at 14:30
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 acontextinitialization function to theApolloServerconstructor.Aug 16, 2020 at 16:14
did you manage to find a solution?
Mar 3, 2022 at 20:13
|