Folks,
I need help. I've been at this for sometime now and cant seem to figure it out. It should be simple. I'm using the aldeed collection2 and I cant seem to get past validation error being thrown by the create user account method. My schema is pretty standard, attached to meteor.users collection:
Schema={}
//SimpleSchema.debug = true;
Schema.UserProfile = new SimpleSchema({
picture: {
type: String,
optional: true
},
updatedAt: {
type: Date,
autoValue: function() {
if (this.isUpdate) {
return new Date();
}
},
denyInsert: true,
optional: true
},
roles: {
type: String,
optional: true
}
});
Schema.User = new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id,
optional: true,
denyUpdate: true
},
emails: {
type: [Object],
optional: true
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email,
label: ""
},
"emails.$.verified": {
type: Boolean,
optional: true
},
createdAt: {
optional: true,
type: Date,
autoValue: function() {
if (this.isInsert) {
return new Date;
} else if (this.isUpsert) {
return {$setOnInsert: new Date};
} else {
this.unset();
}
}
},
profile: {
type: Schema.UserProfile,
optional: true
},
services: {
type: Object,
optional: true,
blackbox: true
},
// Option 2: [String] type
// If you are sure you will never need to use role groups, then
// you can specify [String] as the type
roles: {
type: [String],
optional: true,
autoValue: function() {
if (this.isInsert) {
return ['user'];
} else if (this.isUpsert) {
return {$setOnInsert: ['user']};
} else {
this.unset();
}
}
},
password: {
type: String,
label: "Password",
min: 6
}
});
/* Attach schema to Meteor.users collection */
Meteor.users.attachSchema(Schema.User);
The method on my server for creating the user is like below:
Accounts.config({
forbidClientAccountCreation : true
});
//creates user on server
Meteor.methods({
createNewUserAccount: function(userdata) {
var userId;
check(userdata, Schema.User);
//console.log(userdata);
userId = Accounts.createUser({
email: userdata.emails[0].address,
password: userdata.password,
profile: userdata.profile
});
//console.log(userId);
return userId;
}
});
Accounts.onCreateUser(function(options, userdata) {
//user.profile = {};
// we wait for Meteor to create the user before sending an email
//need to address the exception when existing email is tried for signing up
Meteor.setTimeout(function () {
Accounts.sendVerificationEmail(userdata._id);
}, 2 * 1000);
return userdata;
});
for my client, I have the following signup.js
Template.signup.events({
'submit form': function(e){
// Prevent form from submitting.
e.preventDefault();
//console.log(doc);
user = {
'email.$.address': $('[name="emails.0.address"]').val(),
password: $('[name="password"]').val()
};
Meteor.call('createNewUserAccount', user, function(error) {
if (error) {
return alert(error.reason);
} else {
Router.go('/');
}
});
}
Does anyone know what I'm doing wrong? The schema does not validate the email address.I get the error:
email.0.address is not allowed by the schema
You're creating an object:
user = {
'email.$.address': $('[name="emails.0.address"]').val(),
password: $('[name="password"]').val()
};
The key is the literal string 'email.$.address'
So when you do:
userId = Accounts.createUser({
email: userdata.emails[0].address,
password: userdata.password,
profile: userdata.profile
});
The email key can't find userdata.emails[0] because there is no emails key. Instead, the key is 'email.$.address'. Also, the schema does not have a key called email.$.address. It has one called emails.$.address
Try:
Template.signup.events({
'submit form': function(e){
// Prevent form from submitting.
e.preventDefault();
//console.log(doc);
user = {
'emails.$.address': $('[name="emails.0.address"]').val(),
password: $('[name="password"]').val()
};
Meteor.call('createNewUserAccount', user, function(error) {
if (error) {
return alert(error.reason);
} else {
Router.go('/');
}
});
}
Then
//creates user on server
Meteor.methods({
createNewUserAccount: function(userdata) {
var userId;
check(userdata, Schema.User);
//console.log(userdata);
userId = Accounts.createUser({
email: userdata['emails.$.address'],
password: userdata.password,
profile: userdata.profile
});
//console.log(userId);
return userId;
}
});
Related
thx for any help.
Im using at the frontend the apollo-client and at the backend graphql-nexus,prisma2 and graphql-yoga server.
I want to solve the n + 1 problem with #paljs/plugins.
At the frontend I have a query posts like:
query posts{
posts {
id
favoritedBy(where: { id: { equals: $currentUserId } }) {
id
}
author {
id
avatar {
id
}
}
link {
id
}
games {
id
}
tags {
id
}
likes(where: { user: { id: { equals: $currentUserId } } }) {
id
}
}
}
Posts resolver:
import { PrismaSelect } from '#paljs/plugins'
export const posts = queryField('posts', {
type: 'Post',
list: true,
args: {
...
},
resolve: async (_parent, args, { prisma, request }, info) => {
const select = new PrismaSelect(info).value
let opArgs: FindManyPostArgs = {
take: 10,
orderBy: {
[args.orderBy]: 'desc',
},
...select
}
const post = await prisma.post.findMany(opArgs)
//The result I want to return with the "sub-models" like likes, author tags...
console.log(JSON.stringify(post, undefined, 2))
return post
},
})
I logging the queries
const prisma = new PrismaClient({
log: ['query'],
})
My Problem: With PrismaSelect, I have 5 queries more than without and If I check the request-time at the frontend I need 300-400ms longer with PrismaSelect. So what I'm doing wrong?
I saw in the #paljs/plugins doc the select in the context. Maybe that is my mistake. How can I use the select in the context?
Here ist my Context:
import { PrismaClient, PrismaClientOptions } from '#prisma/client'
import { PubSub } from 'graphql-yoga'
import { PrismaDelete, onDeleteArgs } from '#paljs/plugins'
class Prisma extends PrismaClient {
constructor(options?: PrismaClientOptions) {
super(options)
}
async onDelete(args: onDeleteArgs) {
const prismaDelete = new PrismaDelete(this)
await prismaDelete.onDelete(args)
}
}
export const prisma = new PrismaClient({
log: ['query'],
})
export const pubsub = new PubSub()
export interface Context {
prisma: PrismaClient
request: any
pubsub: PubSub
}
export function createContext(request: any): Context {
return { prisma, request, pubsub }
}
You need to know that to use my PrismaSelect plugin you need to remove the nexus-prisma-plugin package and use my Pal.js CLI to create your CRUD and ObjectType for nexus and using #paljs/nexus plugin to add in mackSchema function
import { makeSchema } from '#nexus/schema';
import * as types from './graphql';
import { paljs } from '#paljs/nexus'; // import our plugin
export const schema = makeSchema({
types,
plugins: [paljs()],// here our plugin don't use nexus-prisma-plugin
outputs: {
schema: __dirname + '/generated/schema.graphql',
typegen: __dirname + '/generated/nexus.ts',
},
typegenAutoConfig: {
sources: [
{
source: require.resolve('./context'),
alias: 'Context',
},
],
contextType: 'Context.Context',
},
});
Now add this type to your Context
export interface Context {
prisma: PrismaClient
request: any
pubsub: PubSub
select: any // here our select type
}
export function createContext(request: any): Context {
// our paljs plugin will add select object before resolver
return { prisma, request, pubsub, select: {} }
}
after you add our plugin your query will log like this
extendType({
type: 'Query',
definition(t) {
t.field('findOneUser', {
type: 'User',
nullable: true,
args: {
where: arg({
type: 'UserWhereUniqueInput',
nullable: false,
}),
},
resolve(_, { where }, { prisma, select }) {
// our plugin add select object into context for you
return prisma.user.findOne({
where,
...select,
});
},
});
},
});
Can you please try to use my pal c command to start an example from my list and try your schema and make tests with it
It is working, thx Ahmed your plugin is AWESOME!!!!!
I changed my Post-Object from
const Post = objectType({
name: 'Post',
definition(t) {
t.model.id()
t.model.authorId()
t.model.tags()
t.model.games()
t.model.link()
t.model.report()
t.model.notifications()
t.model.author()
t.model.favoritedBy({
filtering: {
id: true,
},
})
t.model.likes({
filtering: {
user: true,
},
})
}
})
to
const Post = objectType({
name: 'Post',
definition(t) {
t.string('id')
t.field('tags', {
nullable: false,
list: [true],
type: 'Tag',
resolve(parent: any) {
return parent['tags']
},
})
t.field('games', {
list: [true],
type: 'Game',
resolve(parent: any) {
return parent['games']
},
})
t.field('link', {
type: 'Link',
nullable: true,
resolve(parent: any) {
return parent['link']
},
})
t.field('notifications', {
list: [true],
type: 'Notification',
resolve(parent: any) {
return parent['notifications']
},
})
t.field('author', {
nullable: false,
type: 'User',
resolve(parent: any) {
return parent['author']
},
})
t.field('favoritedBy', {
nullable: false,
list: [true],
type: 'User',
args: {
where: 'UserWhereInput',
},
resolve(parent: any) {
return parent['favoritedBy']
},
})
t.field('likes', {
list: [true],
type: 'Like',
args: {
where: 'LikeWhereInput',
},
resolve(parent: any) {
return parent['likes']
},
})
},
})
And I also used the nexus-prisma-plugin and paljs-plugin at the same time
this is my InputType in schema.graphql:
input RegisterInput {
birthday: String!
email: String!
firstName: String!
gender: String!
interests: [String!]!
lastName: String!
password: String!
}
and this is my mutation:
const RegisterInput = inputObjectType({
name: 'RegisterInput',
definition(t) {
t.string('birthday', { nullable: false });
t.string('email', { nullable: false });
t.string('firstName', { nullable: false });
t.string('lastName', { nullable: false });
t.string('gender', { nullable: false });
t.string('password', { nullable: false });
t.list.field('interests', {
type: 'String',
nullable: false,
});
},
});
const Mutation = objectType({
name: 'Mutation',
definition(t) {
t.field('register', {
type: User,
args: {
data: arg({ type: RegisterInput }),
},
resolve: async (
_root,
{ data: { password, interests, ...userData } },
{ prisma }
) => {
const hashedPassword = await bcrypt.hash(password, 10);
const user = await prisma.user.create({
data: {
...userData,
interests: [...interests],
password: hashedPassword,
},
});
return user;
},
});
my interests is just an array of strings, .e.g: ['abcd', 'def']
but i got this error:
Unknown arg `0` in data.interests.0 for type UserCreateInterestInput. Available args:
type UserCreateInterestsInput {
set?: List<String>
}
that error will repeat depending of how many items is in the array, e.g.: Unknown arg '1' and so on, same error message, how do i fix this?
You must provide a list of strings to set argument, such as:
type UserCreateInterestsInput {
set?: List<String>
}
Refer to this issue for more information.
const Mutation = objectType({
name: 'Mutation',
definition(t) {
t.field('register', {
type: User,
args: {
data: arg({ type: RegisterInput }),
},
resolve: async (
_root,
{ data: { password, interests, ...userData } },
{ prisma }
) => {
const hashedPassword = await bcrypt.hash(password, 10);
const user = await prisma.user.create({
data: {
...userData,
interests: {set: interests},
password: hashedPassword,
},
});
return user;
},
});
Hope this helps
Happened to me earlier, turns out it was a query mistake.
mutation {
createFruit(data:{
name: "Banana",
images: {
set: ["image_1.img", "image_2.img"]
}
}) {
name
images
}
}
Note it's not images: ["image_1.img", "image_2.img"]
fyi with prisma you can do t.model.interest() when defining objectType
I'm trying to get my head around graphql-subscriptions and withFilter. Subscriptions without variables work as intended, but if I try to use withFilter, I only get 'Subscription field must return Async Iterable. Received: undefined' error when I try to run the subscription.
Am I doing something wrong with setting up withFilter, are the some incompatibilities with packages I'm using or am I completely missing something obvious here? All queries and mutations work properly, so the basic set up should be fine.
My set up is similar to this (all code snippets are in https://gist.github.com/aqmattil/41e10e7c9f30b8ea964cecdc61c58f20
Package.json
// package.json
"dependencies": {
"apollo-server-express": "^2.0.0-beta.2",
"body-parser": "^1.18.3",
"express": "^4.16.3",
"graphql": "^0.13.2",
"graphql-subscriptions": "^0.5.8",
"subscriptions-transport-ws": "^0.9.11"
}
Mutations
// mutations.js
const mutation = new GraphQLObjectType({
name: 'mutation',
fields: {
addSite: {
type: SiteType,
description: "Create a new Site",
args: {
name: { type: new GraphQLNonNull(GraphQLString) },
location: { type: GraphQLString },
company: { type: GraphQLString }
},
async resolve(parentValue, { name, location, company }) {
const site = await new Site({ name, location, company }).save()
const siteid = site._id;
console.log("addSite resolve", siteid, name, location, company );
pubsub.publish('siteAdded', { 'siteAdded': site } );
return site;
}
}
}
});
module.exports = mutation;
Subscriptions
// subscriptions.js
const graphql = require('graphql');
const {
GraphQLObjectType,
GraphQLString
} = graphql;
const { withFilter } = require('graphql-subscriptions');
const SiteType = require('./site_type');
const pubsub = require('./pubsub_helper');
const Subscriptions = new GraphQLObjectType({
name: 'subscription',
fields: () => ({
/*
// this code works, commented out to test withfilter
siteAdded: {
type: SiteType,
resolve(payload) {
return payload.siteAdded;
},
subscribe() {
return pubsub.asyncIterator('siteAdded');
}
},
*/
// test withFilter
siteAdded: {
type: SiteType,
args: {
name: { type: GraphQLString }
},
resolve(payload) {
return payload.siteAdded;
},
subscribe() {
// this returns undefined
withFilter(
() => {
console.log("in subscribe withfilter");
return pubsub.asyncIterator('siteAdded');
}
),
(payload, variables) => {
console.log("payload, variables", payload, variables);
return true;
}
}
}
})
});
module.exports = Subscriptions;
I'm using graphiql to run the queries,
// this is used to add a site
mutation {
addSite(name:"test name", location: "somewhere") {
id
}
}
// simple subscription - this works as inteded, and new sites are shown
subscription {
siteAdded {
name
location
company {
id
}
}
}
// using query variables --> returns "Subscription
// field must return Async Iterable. Received: undefined"
subscription {
siteAdded(name: "test name") {
name
location
company {
id
}
}
}
I want to send graphql mutation request without sub section
mutation _ {
updateCurrentUser(fullName: "Syava", email: "fake#gmail.com")
}
and I am getting
{
"errors": [
{
"message": "Field \"updateCurrentUser\" of type \"User\" must have a sub selection.",
...
}
]
}
add { id } to request works fine but I don't want
Also Schema code
const userType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: { type: new GraphQLNonNull(GraphQLString) },
fullName: { type: GraphQLString },
email: { type: GraphQLString },
}),
});
type: userType,
args: {
fullName: { type: GraphQLString },
email: { type: new GraphQLNonNull(emailType) },
password: { type: GraphQLString },
},
resolve: async (root, { fullName, email, password }, { rootValue }) => {
const user = await User.findById(rootValue.req.user.id);
...
return user;
},
You define the type of the field to be UserType. Even though it's a mutation, it still follows the same rules and behavior as a query. Because UserType is an object type, it requires nested fields.
mutation _ {
updateCurrentUser(fullName: "Syava", email: "fake#gmail.com") {
fullName
email
}
}
// would respond with { fullName: 'Syava', email: 'fake#gmail.com' }
If you don't want the mutation to return a User, you can declare its type to GraphQLBoolean for example -- that's a scalar and doesn't have any nested fields.
{
type: GraphQLBoolean,
args: {
fullName: { type: GraphQLString },
email: { type: new GraphQLNonNull(emailType) },
password: { type: GraphQLString },
},
resolve: async (root, { fullName, email, password }, { rootValue }) => {
const user = await User.findById(rootValue.req.user.id);
user.fullName = fullName;
user.password = password; // or hashed to not store plain text passwords
return user.save(); // assuming save returns boolean; depends on the library you use
}
}
Note that the best practice for mutations in GraphQL APIs is to return a "result" object with multiple fields, such as the mutated object itself (e.g. user), clientMutationId (per Relay spec), and others as needed. This makes it flexible so you could add more data in the future.
updateCurrentUser(fullName: "Syava", email: "fake#gmail.com") {
clientMutationId
user {
...
}
}
I am developing a project in which admins can add chat rooms and those have five fields: (id, creator (admin who created it), name, slug and createdAt) and I am receiving this error Unhandled rejection SequelizeValidationError: notNull Violation: name cannot be null.
//models/user.js
var Sequelize = require('sequelize');
var passportLocalSequelize = require('passport-local-sequelize');
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.json')[env];
var mydb = new Sequelize(config.database, config.username, config.password, config);
// We do not need additional features for our users, so we just use the default user model
// provided by Passport
var User = passportLocalSequelize.defineUser(mydb,
{
email: { type: Sequelize.STRING, allowNull: false, unique: true }
},{}
);
//Function at a prototype level for performance optimization
User.Instance.prototype.$Model.findByEmail = function(email, done){
var parameters = {};
parameters.email = email;
var user = this.findOne({where: parameters});
user.then(function(usr) {
done(null, usr);
});
}
mydb.authenticate().then(function(done)
{
if(mydb.sync({force: false}))
done();
}).catch(function(error,user)
{
console.log(error);
});
module.exports = User;
//models/chat.js
var Sequelize = require('sequelize');
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.json')[env];
var User = require('./user');
var mydb = new Sequelize(config.database, config.username, config.password, config);
var Chat = mydb.define('Chat',
{
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
name: { type: Sequelize.STRING, unique: true, allowNull: false,
validate: {
notNull: { args: true, msg: "You must enter a name" }
},
set: function(val) {
val = val.toLowerCase().trim();
this.setDataValue('title', val );
this.setDataValue('slug', val.split(" ").join("_"));
}
},
slug: { type: Sequelize.STRING, unique: true, allowNull: false },
creator: { type: Sequelize.INTEGER, references: { model: User, key: 'id' }}
},
{
timestamps: true,
updatedAt: false // I only want the field createdAt
}
)
mydb.authenticate().then(function(done){
if(mydb.sync({force: false}))
done()
}).catch(function(err, chat){
console.log(err);
});
module.exports = Chat;
//routes/admin.js
router.post("/dashboard",isAuthenticated, function(req,res){
if (req.body.chatName) {
try{
console.log(req.body.chatName);
console.log(Chat.create({ name: req.body.chatName, creator: req.user.id }));
}
catch (e){
console.log(e.message);
}
}
});
I only copied and pasted the function which manages the chat rooms creation, console.log(req.body.chatName); prints correctly the entered name
From the sequelize docs:
// setting allowNull to false will add NOT NULL to the column, which means an error will be
// thrown from the DB when the query is executed if the column is null
In your Chat table you set
name: { type: Sequelize.STRING, unique: true, allowNull: false,
validate: {
notNull: { args: true, msg: "You must enter a name" }
},
set: function(val) {
val = val.toLowerCase().trim();
this.setDataValue('title', val );
this.setDataValue('slug', val.split(" ").join("_"));
}
},
for the user field, where allowNull is false.
So the error you're getting is directly from the db instead of your message in the validate property. Get rid of the allowNull property, keep the validation, and you should be good. A little late, but hope this clears things up!