This question already has answers here:
Why does a GraphQL query return null?
(6 answers)
Closed 3 years ago.
I want to have a rootquery that returns every row within my table. But currently when I call that rootquery I only receive null. But one of my rootquery that only returns one row with a specific id does work. So where i am going wrong?
Rootquery's:
This one works
aanvraag:{
type: AanvraagType,
args:{id: {type: GraphQLID}},
resolve(parentValue, args){
const query = `SELECT * FROM aanvraag where id=${args.id}`;
return db.conn.one(query)
.then(data => {
return data;
})
.catch(err => {
return 'Error is: ', err
});
}
},
This one doesn't work
aanvragen:{
type: AanvraagType,
resolve(parentValue, args){
const query = 'SELECT * from aanvraag';
return db.conn.many(query)
.then(data => {
return data;
})
.catch(err => {
return 'Error is: ', err
});
}
}
This is the whole file if someone needs it:
const graphql = require('graphql');
const pgp = require('pg-promise')();
const axios = require('axios');
const db = {}
var cn = {
host: 'localhost', // server name or IP address;
port: 5432,
database: 'admin',
user: 'admin',
password: 'admin123'
};
db.conn = pgp(cn);
const {
GraphQLObjectType,
GraphQLID,
GraphQLString,
GraphQLSchema
} = graphql;
const TeamlidType = new GraphQLObjectType({
name: 'Teamlid',
fields: {
id: { type: GraphQLID },
email: { type: GraphQLString },
naam: { type: GraphQLString }
}
})
const ProjectType = new GraphQLObjectType({
name: 'Project',
fields:{
id: {type: GraphQLID},
naam: { type: GraphQLString },
type: { type: GraphQLString },
lead_naam: { type: GraphQLString },
lead_email: { type: GraphQLString },
teamlid:{
type: TeamlidType,
resolve(parentValue, args){
console.log(parentValue.id);
const query = `SELECT * FROM teamlid WHERE project_id=${parentValue.id}`;
return db.conn.many(query)
.then(data => {
return data;
})
.catch(err => {
return 'The error is', err;
});
}
}
}
})
const AanvraagType = new GraphQLObjectType({
name: 'Aanvraag',
fields:{
id: {type: GraphQLID},
naam: { type: GraphQLString },
email: { type: GraphQLString },
divisie: { type: GraphQLString },
afdeling: { type: GraphQLString },
team: { type: GraphQLString },
project:{
type: ProjectType,
resolve(parentValue, args){
const query = `SELECT * FROM project WHERE aanvraag_id=${parentValue.id}`;
return db.conn.one(query)
.then(data => {
return data;
})
.catch(err => {
return 'The error is', err;
});
}
}
}
})
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
teamlid: {
type: TeamlidType,
args: { id: { type: GraphQLID } },
resolve(parentValue, args) {
const query = `SELECT * FROM teamlid WHERE id=${args.id}`;
return db.conn.one(query)
.then(data => {
return data;
})
.catch(err => {
return 'The error is', err;
});
}
},
aanvraag:{
type: AanvraagType,
args:{id: {type: GraphQLID}},
resolve(parentValue, args){
const query = `SELECT * FROM aanvraag where id=${args.id}`;
return db.conn.one(query)
.then(data => {
return data;
})
.catch(err => {
return 'Error is: ', err
});
}
},
project:{
type: ProjectType,
args:{id: {type: GraphQLID}},
resolve(parentValue, args){
const query = `SELECT * FROM project where id=${args.id}`;
return db.conn.one(query)
.then(data => {
return data;
})
.catch(err => {
return 'Error is: ', err
});
}
},
aanvragen:{
type: AanvraagType,
resolve(parentValue, args){
const query = 'SELECT * from aanvraag';
return db.conn.many(query)
.then(data => {
return data;
})
.catch(err => {
return 'Error is: ', err
});
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQuery,
mutation
})
So I already found my answer. I had to wrap 'aanvraag' in an GraphQLList. So this would work:
aanvragen:{
type: GraphQLList(AanvraagType),
resolve(){
const query = 'SELECT * from aanvraag';
return db.conn.many(query)
.then(data => {
return data;
})
.catch(err => {
return 'Error is: ', err
});
}
}
I am new to GrapQL but i hope this will help someone in the future.
Related
I've been struggling with this issue for a day now and can't seem to figure out a way to resolve it. This is the code I'm running
Client side:
const nameInput = document.querySelector("#nameInput");
const urlInput = document.querySelector("#urlInput");
const rowAlert = document.querySelector(".alertAppend");
const divAlert = document.createElement("div");
const nameUpdate = async (e) => {
e.preventDefault();
fetch("/auth/updateName", {
method: 'POST',
headers: {
'Content-Type' : 'application/json'
},
body: JSON.stringify({
name: nameInput,
url: urlInput,
})
})
.then(function (data) {
console.log('Request success: ', data);
})
.catch(function (error) {
console.log('Request failure: ', error);
});
};
submitName.addEventListener("click", nameUpdate);
API:
router.get("/updateName", auth, async (req, res) =>{
try {
const { name, url } = req.body;
const ime = name;
const uid = req.session.passport.user;
db.User.find({ where: { id: uid } })
.on('success', function (user) {
if (user) {
user.update({
name: ime,
webhook: url
})
.success(function () {})
}
})
res.json({ message: url});
} catch (err) {
if (err) res.status(500).json({ message: "Internal Error"})
}
});
For some reason it just runs the select query and never proceeds to update the user.
Chrome console output
Debug console output
Sequelize model in case it helps:
module.exports = function (sequelize, DataTypes) {
var User = sequelize.define("User", {
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
password: {
type: DataTypes.STRING,
allowNull: false
},
name: {
type: DataTypes.STRING
}
})
return User;
}
The issue was in the API, it's supposed to be router.post
router.post("/updateName", auth, async (req, res) =>{
const { ime, url } = req.body;
const uid = req.session.passport.user;
console.log(ime);
db.User.findOne({where: {id: uid}})
.then(record => {
let values = {
name: ime,
webhook: url
}
record.update(values).then( updatedRecord => {
console.log(`updated record ${JSON.stringify(updatedRecord,null,2)}`)
res.status(200).json({ message: "success"});
})
}
})
.catch((error) => {
// do seomthing with the error
throw new Error(error)
})
});
You can try the following code
await db.User.update({
name: ime,
webhook: url
}, { where: { id: uid } });
When defining your model I don't see the webhook field
I am just working on an analysis project. I have a create method in node but I don't know why suddenly this is happening. I have connected to the database successfully. and wrote created a mongoose model successfully.Now in below code promise.then() is not visited. document is not created. but if I removed any required field, it throws a error as expected.
my create method:
create: function (doc) {
return new Promise((resolve, reject) => {
bcrypt.hash(doc.password, saltRounds).then((hash) => {
doc.password = hash;
console.log(doc); //works
Account.create(doc).then((account) => {
console.log(account); //not working, no error, successful execution, code exit 0.
resolve(account);
}).catch((err) => {
console.log(account); //nothing printed
reject(err);
});
}).catch(err => {
console.log(account); //nothing printed
reject(err);
});
});
}
my database connection in database/db.js
const mongoose = require('mongoose');
require('dotenv').config();
const database = {
connect: function () {
mongoose.connect(
process.env.MONGO_URI + '?retryWrites=true',
{ useNewUrlParser: true, useUnifiedTopology: true }
).then(
function () {
console.log('database connected successfully');
}).catch(error => {
console.log(error);
});
}
};
module.exports = database;
My whole controller code controllers/account.js:
Account = require('../models/account');
const bcrypt = require('bcrypt');
const saltRounds = 10;
authController = {
create: function (doc) {
return new Promise((resolve, reject) => {
bcrypt.hash(doc.password, saltRounds).then((hash) => {
doc.password = hash;
let promise = Account.create(doc);
promise.then((account) => {
console.log(account);
resolve(account);
}).catch((err) => {
console.log(err);
reject(err);
});
}).catch(err => {
console.log(err);
reject(err);
});
});
},
show: function (email) {
return new Promise((resolve, reject) => {
let filter = {email: email};
Account.findOne(filter).then((account) => {
resolve(account);
}).catch((err) => {
reject(err);
});
});
},
update: function (id, update) {
return new Promise((resolve, reject) => {
let filter = {_id: id};
Account.findOneAndUpdate(filter, update).then((account) => {
resolve(account);
}).catch((err) => {
reject(err);
});
});
},
destroy: function (id) {
return new Promise((resolve, reject) => {
let filter = {_id: id};
Account.findOneAndDelete(filter).then(() => {
resolve({deleted: true});
}).catch((err) => {
reject(err);
});
});
},
compare: function (doc) {
return new Promise((resolve, reject) => {
let filter = {email: doc.email};
Account.findOne(filter).then(user => {
bcrypt.compareSync(doc.password, user.password).then((result) => {
resolve(result);
}).catch((err) => {
reject(err);
});
}).catch((err) => {
reject(err);
});
});
}
};
module.exports = authController;
My model for account in models/account.js:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const accountSchema = new Schema({
first: {
type: String,
required: true
},
last: {
type: String,
required: false
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
role: {
type: String,
required: true
},
boards: [{
type: mongoose.Schema.Types.ObjectId, ref: 'Board',
required: false
}]
},
{
timestamps: true
});
Account = mongoose.connection.model('User', accountSchema);
module.exports = Account;
my test.js code:
const accountCtrl = require('./controllers/account');
const account = {
first: "User 1",
last: "Last 1",
email: "email1#tasks.com",
password: "password",
role: "test",
};
accountCtrl.create(account).then((result) => {console.log(result)}).catch((err) => {console.log(err)});
I want to search with my query getFoodType to return results based on whether the foodType of particular restaurant/takeaway is a "Chicken","Pizza" etc
Like this foodType: "Chicken"
I've tried using arguments and mongoDB filters (it's a MongoDB server) but no luck.
Schema
const EaterySchema = new Schema({
name: {
type: String,
required: true
},
address: {
type: String,
required: true
},
foodType: {
type: String,
required: true
}
});
My Schema Types
type Eatery {
id: String!
name: String!
address: String!
foodType: String!
}
type Query {
eatery(id: String!): Eatery
eateries: [Eatery]
getFoodType(foodType: String): [Eatery]
}
My Resolver
getFoodType: () => {
return new Promise((resolve, reject) => {
Eatery.find({})
.populate()
.exec((err, res) => {
err ? reject(err) : resolve(res);
});
});
},
Current Query in Apollo Playground
{
getFoodType (foodType: "Chicken") {
id
name
address
foodType
}
}
I essentially want to return all the results with "Chicken" as a the foodType. Something like foodType: "Chicken".
First, you need to get the value of the foodType to be queried in Resolver
const resolvers = {
Query: {
getFoodType: (_, args) => {
const { foodType } = args
...
},
},
}
Then use foodType when querying
Eatery.find({ foodType })
Finally need to return the result
new Promise((resolve, reject) => {
return Eatery.find({ foodType })
.populate()
.exec((err, res) => {
err ? reject(err) : resolve(res)
})
})
Complete example
const resolvers = {
Query: {
getFoodType: (_, args) => {
const { foodType } = args
return new Promise((resolve, reject) => {
return Eatery.find({ foodType })
.populate()
.exec((err, res) => {
err ? reject(err) : resolve(res)
})
})
},
},
}
Use the async/await
const resolvers = {
Query: {
getFoodType: async (_, { foodType }) => {
try {
const eaterys = await Eatery.find({ foodType }).populate()
return eaterys
} catch (e) {
// Handling errors
}
},
},
}
I'm new to GraphQL so I'm doing my first query and I'm trying to query for a nested key, email
Here is my schema
const UserLogin = new GraphQLObjectType({
name: 'UserLoginType',
fields: () => ({
login: { type: GraphQLString },
email: { type: GraphQLString },
}),
});
exports.UserType = new GraphQLObjectType({
name: 'UserType',
fields: () => ({
id: NotNullStringType,
name: { type: GraphQLString },
state: { type: GraphQLString },
login: { type: UserLogin },
}),
});
And what I'm trying to do is build a query that takes an email and searches for the UserType.login.email value and return the first value that matches that email or null.
Thanks!
Edit: In my redux/actions.js file I added:
export const fetchLoginsByEmail = (userEmail) => {
return createGraphqlAction(
{
query: `
query fetchUserByEmail($userEmail: String!) {
fetchUserByEmail(email: $userEmail) {
login {
email
}
state
name
id
}
}
`,
variables: {
userEmail,
}
},
FETCH_LOGINS_BY_EMAIL,
);
};
And in my query.js file I have this in my model.exports object:
fetchUserByEmail: {
type: UserType,
resolve: async (source, args, { user, loginId }) => {
if (!user) {
return null;
}
return serailize({
...user,
login: await getLogin(loginId),
});
},
},
It looks like you're missing the arguments parameter for your query resolver.
Update your query.js into something like:
fetchUserByEmail: {
type: UserType,
args: {
email { type: GraphQLString }
},
resolve: async (source, args, { user, loginId }) => {
// You can now use args.email here and filter your results with it
console.log(args.email);
if (!user) {
return null;
}
return serailize({
...user,
login: await getLogin(loginId),
});
},
},
I'm using express-graphql and node-fetch in my application. I am trying to use graphql with my api call to grab data. Currently I am doing
const QueryType = new GraphQLObjectType({
name: "Query",
fields: () => ({
acctsFromRelation: {
type: new GraphQLList(AcctType),
args: {
id: {type: GraphQLString},
status: {type: GraphQLString}
},
resolve: (root, args) => getOpIds(args)
}
})
});
The AcctType is as follows
const AcctType = new GraphQLObjectType({
name: "acct",
fields: () => ({
id: {type: GraphQLString},
name: {type: GraphQLString},
clientType: {type: GraphQLString},
accountStatus: {type: GraphQLString,
args: {
status: {type: GraphQLString}
}},
primaryContact: {type: contactType},
billingAddress: {type: billingType}
})
});
I'm trying to do something like this:
{ acctsFromRelation (id: "2") {
id
name
accountStatus (status: "active")
primaryContact {
firstName
lastName
phone
email
}
billingAddress {
address1
address2
city
state
postalCode
country
}
}
}
where i obtain all accounts with id of 2 and accountStatus of active.
GetOpIds is as follows:
function getOpIds (ids) {
return fetch(API CALL THAT GIVES IDS)
.then(res => res.json())
.then(json => json.map((element) => getAccountByUrl(element.id)))
.catch(err => err)
}
and getAccountByUrl looks like this
function getAccountByUrl (ids) {
return fetch(URL THAT LOOKS UP 1 ID at a TIME)
.then(res => res.json())
.then(json => json)
.catch(err => err)
}
You can try this directly from the code
const query = `query AcctsFromRelation($id: ID, $status: String){acctsFromRelation(id: $id, status: $status)}`;
const variables = { id: id, status: status };
return new Promise((resolve, reject) => {
request("/graphql", query, variables).then(data => {
resolve(data.acctsFromRelation);
});
});