I am trying to setup a relationship type in Grandstack. I am having issues getting things to run correctly. Even when I copy the guide into my project and try to run it ... things do not work. Here is what they have at https://grandstack.io/docs/guide-graphql-schema-design
type Actor {
actorId: ID!
name: String
movies: [Movie] #relation(name: "ACTED_IN", direction: OUT)
}
type Movie {
movieId: ID!
title: String
description: String
year: Int
actors(limit: Int = 10): [Actor] #relation(name: "ACTED_IN", direction: IN)
similarMovies(limit: Int = 10): [Movie] #cypher(statement: """
MATCH (this)<-[:ACTED_IN]-(:Actor)-[:ACTED_IN]->(rec:Movie)
WITH rec, COUNT(*) AS score ORDER BY score DESC
RETURN rec LIMIT $limit
""")
}
type User {
userId: ID!
name: String
rated: [Rated]
}
type Rated #relation(name: "RATED") {
from: User
to: Movie
rating: Float
review: String
}
The problem is when I run this I get the following error:
ApolloError: The 'from' argument of the #relation directive on the _UserRated type is "User", but a "User" field is not defined.
at validateRelationTypeNodeField (/Users/kennyv/git/skoutal-server/node_modules/neo4j-graphql-js/dist/augment/types/relationship/relationship.js:221:13)
at validateRelationTypeDirective (/Users/kennyv/git/skoutal-server/node_modules/neo4j-graphql-js/dist/augment/types/relationship/relationship.js:183:5)
Not sure why it thinks the User field is not defined.
Here is my server.js file
require("dotenv").config();
const { ApolloServer, PubSub } = require("apollo-server");
const jwt = require("jsonwebtoken");
const neo4j = require("neo4j-driver");
const { augmentSchema, makeAugmentedSchema } = require("neo4j-graphql-js");
const typeDefs = require("./graphql/typeDefs.js");
const resolvers = require("./graphql/resolvers");
require("console-stamp")(console, { pattern: "mm/dd/yyyy HH:MM:ss.l" });
// Create an instance of Express
const pubsub = new PubSub();
const PORT = process.env.PORT || 5000;
const schema = makeAugmentedSchema({
typeDefs,
resolvers,
// config: {
// query: true,
// mutation: true,
// },
});
const augmentedSchema = augmentSchema(schema);
const driver = neo4j.driver(
process.env.NEO4J_URI || "bolt://localhost:7687",
neo4j.auth.basic(
process.env.NEO4J_USER || "neo4j",
process.env.NEO4J_PASSWORD || "neo4j"
)
);
const server = new ApolloServer({
schema: schema,
context: ({ req }) => {
const token = req?.headers?.authorization?.slice(7);
let userId;
if (token) {
const decoded = jwt.verify(token, process.env.APP_SECRET);
userId = decoded.id;
}
return {
cypherParams: { userId },
driver,
neo4jDatabase: process.env.NEO4J_DATABASE,
req,
pubsub,
};
},
});
"apollo-server": "^2.20.0",
"graphql": "^15.5.0",
"neo4j-driver": "^4.2.2",
"neo4j-graphql-js": "^2.19.2",
I was accidentally running
const augmentedSchema = augmentSchema(schema);
on a schema that I had already ran
makeAugmentedSchema({
typeDefs,
resolvers,
// config: {
// query: true,
// mutation: true,
// },
});
This was an artifact of moving away from makeExecutableSchema which I had to remove because it was causing issues.
namely:
Error: Unknown directive "#relation".
Unknown directive "#relation".
Unknown directive "#cypher".
Unknown directive "#relation".
Unknown directive "#relation".
Unknown directive "#relation".
So when i commented out the augmentSchema things started working fine.
Related
I am new to using Graphql and MongoDB. I am trying to insert data from an existing javascript file where the data has been defined. I was trying to use a mutation in order to achieve this but I have no clue what I'm really doing. Any help would be nice.
const dotenv = require('dotenv');
dotenv.config();
const { ApolloServer, gql } = require('apollo-server');
const { MongoClient } = require('mongodb');
const items = require('./itemsListData');
const typeDefs = gql`
type Query {
items:[Item!]!
}
type Item{
id:ID!,
name:String!,
aisle:String!,
bay:String!,
price:Float!,
xVal:Int!,
yVal:Int!
}
type Mutation {
createItem(name: String!, aisle: String!): Item!
}
`;
console.log(items)
const resolvers = {
Query: {
items:() => items,
},
Item:{
id: ( { _id, id }) => _id || id,
},
Mutation: {
createItem: async(_, { name }, { db }) => {
// name:String!, bays:[Bay!]!, xStartVal:Int!, xEndVal:Int!, yStartVal:Int!, yEndVal:Int!
const newItem = {
items
}
// insert Item object into database
const result = await db.collection('Items').insert(newItem);
console.log("This is the result " + result);
return result.ops[0]; // first item in array is the item we just added
}
}
};
const start = async () => {
const client = new MongoClient("mongodb+srv://admin:admin#quickkartcluster.o0bsfej.mongodb.net/test", { useNewUrlParser: true, useUnifiedTopology: true });
await client.connect();
const db = client.db("QuickKart");
const context = {
db,
}
const server = new ApolloServer({
typeDefs,
resolvers,
context,
introspection: true
});
// The `listen` method launches a web server.
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
}
start();
here is my javascript data file
https://pastebin.com/wvGANBgR
My graphql queries work with the local DB file but while trying to connect to MongoDB it doesn't work as expected.
mongo.js
require("dotenv").config();
const mongoose = require("mongoose");
const MONGODB_URI = process.env.MONGODB_URI;
if (!MONGODB_URI) {
throw new Error(
"Please define the MONGODB_URI environment variable inside .env.local"
);
}
/**
* Global is used here to maintain a cached connection across hot reloads
* in development. This prevents connections from growing exponentially
* during API Route usage.
*/
let cached = global.mongoose;
if (!cached) {
cached = global.mongoose = { conn: null, promise: null };
}
async function dbConnect() {
if (cached.conn) {
return cached.conn;
}
if (!cached.promise) {
const opts = {
bufferCommands: false,
};
cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
return mongoose;
});
}
cached.conn = await cached.promise;
return cached.conn;
}
module.exports = dbConnect;
MONGODB_URI=mongodb+srv://xxxx:xxxx#cluster0.yxbw7.mongodb.net/?retryWrites=true&w=majority
graphql query to get all the products:
exports.typeDefs = gql`
type Query {
products: [Product!]!
}
type Product {
id: ID!
name: String!
description: String!
price: Float!
image: String!
}
`;
Resolvers:
const { Product } = require("../models/Product");
exports.Query = {
products: async (parent, args, context) => {
let products = await Product.find({}).exec();
return products;
},
};
The DB has data in it but still making the query,
query{
products {
description
id
name
price
image
}
}
It returns an empty product array,
{
"data": {
"products": []
}
}
Something seems wrong with MongoDB to connect with graphql, the queries work with the local DB file which has a dummy object product data.
I am using Apollo-server-express and Graphql-tools. I have been all over the Graphql-tools documentation and I can't get this to work. I'm trying to get my schema.graphql file to import as my typeDefs. It seems like Graphql-tools should be making this easy, but something isn't falling into place.
index.js
const { ApolloServer } = require("apollo-server-express");
const { makeExecutableSchema } = require('#graphql-tools/schema');
const express = require("express");
const { join } = require("path");
const { loadSchema } = require("#graphql-tools/load");
const { GraphQLFileLoader } = require("#graphql-tools/graphql-file-loader");
const { addResolversToSchema } = require("#graphql-tools/schema");
const app = express();
const resolvers = {
Query: {
items: (parent, args, ctx, info) => {
return ctx.prisma.item.findMany();
},
},
Mutation: {
makeItem: (parent, args, context, info) => {
const newItem = context.prisma.item.create({
data: {
...args,
price: parseInt(Math.ceil(args.price * 100)),
},
});
return newItem;
},
deleteItem: (parent, args, context, info) => {
return context.prisma.item.delete({
where: {
id: args.id,
},
});
},
},
};
const schemaSource = loadSchemaSync(join(__dirname, "schema.graphql"), {
loaders: [new GraphQLFileLoader()],
});
const schema = makeExecutableSchema({
typeDefs: schemaSource,
resolvers,
});
const server = new ApolloServer({
schema,
resolvers,
});
server.applyMiddleware({ app });
app.listen(
{ port: 4000 },
() =>
console.log(
`🌎 => Backend server is now running on port http://localhost:4000`
)
);
schema.graphql
type Query {
items: [Item!]!
}
type Mutation {
makeItem(
piece: String!
image: String!
identifier: String!
price: Float!
itemNumber: Int!
): Item!
deleteItem(id: ID!): Item!
}
type Item {
id: ID!
piece: String!
image: String!
identifier: String!
price: Int!
itemNumber: Int!
}
In its current state I am getting an error that says: "Error: typeDefs must be a string, array or schema AST, got object"
As I understand it makeExecutableSchema should be doing all the necessary steps, like changing the schema into a string. I can't seem to figure out what is going on here and any help would be greatly appreciated.
loadSchemaSync will load a GraphQLSchema object using the provided pointer. You should use loadTypedefsSync instead.
const sources = loadTypedefsSync(join(__dirname, "schema.graphql"), {
loaders: [new GraphQLFileLoader()],
});
const typeDefs = sources.map(source => source.document)
const server = new ApolloServer({ typeDefs, resolvers })
If you want to use loadSchema, you don't need to use makeExecutableSchema because your schema has already been created. So you would do this instead as shown in the docs:
const schema = loadSchemaSync(join(__dirname, "schema.graphql"), {
loaders: [new GraphQLFileLoader()],
});
const resolvers = {...};
const schemaWithResolvers = addResolversToSchema({
schema,
resolvers,
});
const server = new ApolloServer({ schema: schemaWithResolvers })
I'm using this tutorial to make a node/mongo application. When I run addContact, it seems like the contact saves to the DB but, I get an error saying that the disconnect function is not a function. Can anyone tell me why this is happening and, how to fix it? I'm assume that there's some issue with the scope of the constant, db?
code:
const mongoose = require('mongoose');
const assert = require('assert');
mongoose.Promise = global.Promise;
const db = mongoose.connect('mongodb://localhost:27017/contact-manager');
function toLower(v) {
return v.toLowerCase();
}
const contactSchema = mongoose.Schema({
firstname: { type: String, set: toLower },
lastname: { type: String, set: toLower },
phone: { type: String, set: toLower },
email: { type: String, set: toLower }
});
const Contact = mongoose.model('Contact', contactSchema);
const addContact = (contact) => {
Contact.create(contact, (err) => {
assert.equal(null, err);
console.info('New contact added');
db.disconnect();
});
};
const getContact = (name) => {
const search = new RegExp(name, 'i');
Contact.find({$or: [{firstname: search }, {lastname: search }]})
.exec((err, contact) => {
assert.equal(null, err);
console.info(contact);
console.info(`${contact.length} matches`);
db.disconnect();
});
};
module.exports = { addContact, getContact };
code part2:
const program = require('commander');
const {addContact, getContact} = require('./logic');
program
.version('0.0.1')
.description('Contact management system');
program
.command('addContact <firstame> <lastname> <phone> <email>')
.alias('a')
.description('Add a contact')
.action((firstname, lastname, phone, email) => {
addContact({firstname, lastname, phone, email});
});
program
.command('getContact <name>')
.alias('r')
.description('Get contact')
.action(name => getContact(name));
program.parse(process.argv);
error:
New contact added
/Users/user/contact-manager/node_modules/mongodb/lib/utils.js:132
throw err;
^
TypeError: db.disconnect is not a function
at Contact.create (/Users/user/contact-manager/logic.js:33:8)
at Function.<anonymous> (/Users/user/contact-manager/node_modules/mongoose/lib/model.js:3913:16)
at parallel (/Users/user/contact-manager/node_modules/mongoose/lib/model.js:2077:12)
at /Users/user/contact-manager/node_modules/async/internal/parallel.js:35:9
at /Users/user/contact-manager/node_modules/async/internal/once.js:12:16
at iteratorCallback (/Users/user/contact-manager/node_modules/async/eachOf.js:52:13)
at /Users/user/contact-manager/node_modules/async/internal/onlyOnce.js:12:16
at /Users/user/contact-manager/node_modules/async/internal/parallel.js:32:13
at apply (/Users/user/contact-manager/node_modules/lodash/_apply.js:15:25)
at /Users/user/contact-manager/node_modules/lodash/_overRest.js:32:12
at callbackWrapper (/Users/user/contact-manager/node_modules/mongoose/lib/model.js:2046:11)
at /Users/user/contact-manager/node_modules/mongoose/lib/model.js:3913:16
at model.$__save.error (/Users/user/contact-manager/node_modules/mongoose/lib/model.js:342:7)
at /Users/user/contact-manager/node_modules/kareem/index.js:297:21
at next (/Users/user/contact-manager/node_modules/kareem/index.js:209:27)
at Kareem.execPost (/Users/user/contact-manager/node_modules/kareem/index.js:217:3)
mongoose.connect does not return a value, as far as I can tell from looking at the code given in the getting started guide. If you want to close the database connection, you need to call the close method on mongoose.connection which is written to when you open a connection with mongoose.connect. The simplest way to do this is to move your mongoose.connect to its own line:
mongoose.connect('mongodb://localhost:27017/contact-manager');
const db = mongoose.connection;
Then just replace your calls to db.disconnect() with calls to db.close().
Try letting mongoose handle the connection closure using mongoose.connection.close()
Update: Been some time. But back then decided not to use Mongoose. Main reason being that we couldn't really come up with a great reason for using an ORM when using mongo and javascript.
I've been trying to create a database/model with Mongoose which is basically just a user database where the username is unique. Sounds simple enough, but for some reason I've been unable to do so.
What I've got so far is this:
var mongoose = require('mongoose').Mongoose,
db = mongoose.connect('mongodb://localhost/db');
mongoose.model('User', {
properties: [
'name',
'age'
],
cast: {
name: String,
age: Number
},
//indexes: [[{name:1}, {unique:true}]],
indexes: [
'name'
]
/*,
setters: {},
getters: {},
methods: {}
*/
});
var User = db.model('User');
var u = new User();
u.name = 'Foo';
u.save(function() {
User.find().all(function(arr) {
console.log(arr);
console.log('length='+arr.length);
});
});
/*User.remove({}, function() {});*/
It just doesn't work. The database is created alright, but the username is not unique. Any help or knowledge of what I'm doing wrong?
You need to define the schema. Try this: (
var mongoose = require('mongoose').Mongoose,
db = mongoose.connect('mongodb://localhost/db'),
Schema = mongoose.Schema;
mongoose.model('User', new Schema({
properties: [
'name',
'age'
],
[...]
}));
For Mongoose 2.7 (tested in Node v. 0.8):
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var db = mongoose.connect('mongodb://localhost/db');
var User = new Schema({
first_name: String,
last_name: String
});
var UserModel = mongoose.model('User', User);
var record = new UserModel();
record.first_name = 'hello';
record.last_name = 'world';
record.save(function (err) {
UserModel.find({}, function(err, users) {
for (var i=0, counter=users.length; i < counter; i++) {
var user = users[i];
console.log( "User => _id: " + user._id + ", first_name: " + user.first_name + ", last_name: " + user.last_name );
}
});
});
Try giving right path in var mongoose = require('mongoose').Mongoose,
. It worked for me..
#
my code
require.paths.unshift("/home/LearnBoost-mongoose-45a591d/mongoose");
var mongoose = require('mongoose').Mongoose;
var db = mongoose.connect('mongodb://localhost/db');
mongoose.model('User', {
properties: ['first name', 'last name', 'age', 'marriage_status', 'details', 'remark'],
});
var User = db.model('User');
var record = new User();
record.first name = 'xxx';
record.last name = 'xxx';
record.age = 'xxx';
record.marriage_status = 'xxx';
record.details = 'xxx';
record.remarks = 'xxx';
record.save(function() {
User.find().all(function(arr) {
console.log(arr);
console.log('length='+arr.length);
});
});
//User.remove({}, function() {});
Compile it with node filename.js
good luck..
You should define your unique indexes before running your app for the first time. Otherwise, you need to drop your collection and start over. Also, mongoose will not throw an error when you attempt to save { name: 'user1' } when 'user1' already exists.
Learnboost recently uploaded a set of examples https://github.com/LearnBoost/mongoose/tree/master/examples
I am aware this question is 10 years old and the original poster abandoned Mongoose, but since it pops up near the top of Google searches I felt I would provide a fresh answer.
Providing a complete basic example, using Typescript. I have added comments in the
code, where appropriate.
async function mongooseHelloWorld () {
const url = 'mongodb://localhost/helloworld';
// provide options to avoid a number of deprecation warnings
// details at: https://mongoosejs.com/docs/connections.html
const options = {
'useNewUrlParser': true,
'useCreateIndex': true,
'useFindAndModify': false,
'useUnifiedTopology': true
};
// connect to the database
console.log(`Connecting to the database at ${url}`);
await mongoose.connect(url, options);
// create a schema, specifying the fields and also
// indicating createdAt/updatedAt fields should be managed
const userSchema = new mongoose.Schema({
name:{
type: String,
required:true
},
email: {
type: String,
required: true
}
}, {
timestamps: true
});
// this will use the main connection. If you need to use custom
// connections see: https://mongoosejs.com/docs/models.html
const User = mongoose.model('User', userSchema);
// create two users (will not be unique on multiple runs)
console.log('Creating some users');
await User.create({ name: 'Jane Doe', email: 'jane.done#example.abcdef' });
await User.create({ name: 'Joe Bloggs', email: 'jane.done#example.abcdef' });
// Find all users in the database, without any query conditions
const entries = await User.find();
for (let i = 0; i < entries.length; i++) {
const entry = entries[i] as any;
console.log(`user: { name: ${entry.name}, email: ${entry.email} }`);
}
}
// running the code, and making sure we output any fatal errors
mongooseHelloWorld()
.then(() => process.exit(0))
.catch(error => {
console.log(error)
});
Note, this was validated with Mongoose 5.9.26, running against Mongo 4.0.13.