JavaScript correct usage of a thunk function (with variables) - javascript

I'm stuck in a thunk function usage and I need to better understand how it works.
Code that DOES NOT WORK:
const fields = {
id: {
type: new GraphQLNonNull(GraphQLID),
resolve: (obj) => dbIdToNodeId(obj._id, "History")
},
timestamp: {
type: GraphQLLong
},
objectId: {
type: GraphQLString
},
user: {
type: require('../User/connections').default,
args: connectionArgs,
resolve: (source, args) => {
return UserModel.findOne({ id: source.id }).exec();
}
},
action: {
type: new GraphQLNonNull(GraphQLString)
}
};
export const HistoryType = new GraphQLObjectType({
name: 'History',
description: 'History',
interfaces: () => [NodeInterface],
isTypeOf: (value) => value instanceof HistoryModel,
fields: () => (fields)
});
Code that DOES WORK:
export const HistoryType = new GraphQLObjectType({
name: 'History',
description: 'History',
interfaces: () => [NodeInterface],
isTypeOf: (value) => value instanceof HistoryModel,
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLID),
resolve: (obj) => dbIdToNodeId(obj._id, "History")
},
timestamp: {
type: GraphQLLong
},
objectId: {
type: GraphQLString
},
user: {
type: require('../User/connections').default,
args: connectionArgs,
resolve: (source, args) => {
return UserModel.findOne({ id: source.id }).exec();
}
},
action: {
type: new GraphQLNonNull(GraphQLString)
}
})
});
From my "not very skilled on JavaScript thunks" point of view, both codes are the same, but they are not as only the second one works, so:
a) Why the codes does not behave the same ? Why storing the thunk content in a function (fields) makes it behave differently ?
b) My fields variable is used in the sequence of the code, so is there a way to make the thunk work keeping its object in a separate variable, as code 1 (in fact a fix for code 1 that keeps the field variable) ?

Related

Mongoose: pre('validate') middleware is not working

This is my mongoose Setup. This happens only when i use class syntax. When i do the same thing with the use of functional programming it works fine. This is the first time i am using class syntax to do this. I think that's where the problem lies. I am doing something wrong with my class definition.
This is my mongoose Setup. This happens only when i use class syntax. When i do the same thing with the use of functional programming it works fine. This is the first time i am using class syntax to do this. I think that's where the problem lies. I am doing something wrong with my class definition.
const mongooseService = require('./services/mongoose.service')
const slugify = require('slugify')
const { marked } = require('marked')
const createDomPurifier = require('dompurify')
const { JSDOM } = require('jsdom')
const dompurify = createDomPurifier(new JSDOM().window)
class ArticleDao {
Schema = mongooseService.getMongoose().Schema
articleSchema = new this.Schema({
title: {
type: String,
required: true,
},
description: {
type: String,
},
markdown: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: new Date(),
},
slug: {
type: String,
required: true,
unique: String,
},
sanitizedHtml: {
type: String,
required: true,
},
})
Article = mongooseService.getMongoose().model('Article', this.articleSchema)
constructor() {
console.log(`created new instance of DAO`)
this.setPreValidation()
}
setPreValidation() {
console.log('h')
this.articleSchema.pre('save', (next) => {
if (this.title) {
this.slug = slugify(this.title, { lower: true, strict: true })
}
if (this.markdown) {
this.sanitizedHtml = dompurify.sanitize(marked(this.markdown))
}
next()
})
}
async addArticle(articleFields) {
const article = new this.Article(articleFields)
await article.save()
return article
}
async getArticleById(articleId) {
return this.Article.findOne({ _id: articleId }).exec()
}
async getArticleBySlug(articleSlug) {
return this.Article.findOne({ slug: articleSlug })
}
async getArticles() {
return this.Article.find().exec
}
async updateArticleById(articleId, articleFields) {
const existingArticle = await this.Article.findOneAndUpdate({
_id: articleId,
$set: articleFields,
new: true,
}).exec()
return existingArticle
}
async removeArticleById(articleId) {
await this.Article.findOneAndDelete({ _id: articleId }).exec()
}
}
module.exports = new ArticleDao()
This is the error i get:
Article validation failed: sanitizedHtml: Path `sanitizedHtml` is required., slug: Path `slug` is required.

How can I auto answer questions in inquirer.js?

I'm writing a small CLI in typescript and I have a command which basically allows me to generate a json file with default values in it (just like npm init -y), but I don't know how to auto answer the questions in inquirer.
This is what I've got so far:
export const initializeConfig = (project: string, ...args: boolean[]) => {
prompt([
{
type: "input",
name: "name",
message: "What is the name of the project?",
default: basename(cwd()),
when: () => args.every((arg) => arg === false),
},
{
type: "list",
name: "project",
message: "What is the type of the project?",
choices: ["Node", "Python"],
default: project,
when: () => args.every((arg) => arg === false),
},
])
.then((answers: Answers) => {
config = setConfig({ name: answers.name });
config = setConfig({ project: answers.project });
})
.then(() =>
prompt([
{
type: "input",
name: "path",
message: "Where is your project root located?",
default: ".",
when: () => args.every((arg) => arg === false),
},
{
type: "input",
name: "ignore",
message: "What do you want to ignore? (comma separated)",
default: defaultIgnores(config.project).ignore,
when: () => args.every((arg) => arg === false),
},
]).then((answers: Answers) => {
config = setConfig(ignoreFiles(config.project, answers.ignore));
createConfig(answers.path, config);
})
);
};
I thought that if I'd skip/hide the questions with when(), it would use the default values, but it doesn't. It's always undefined.
Didn't find this topic on the internet so far. Any ideas?
Kind of a life hack, but I managed to "auto answer" my questions in inquirer by creating a defaults() function that returns an object of the default values.
Then I can use those if my answer object is empty as you see below:
const defaults = (project: string) => {
return {
name: basename(cwd()),
project,
path: ".",
ignore: defaultIgnores(project).ignore,
};
};
export let config: any = {
version,
};
export const initializeConfig = (project: string, ...args: boolean[]) => {
prompt([
{
type: "input",
name: "name",
message: "What is the name of the project?",
default: defaults(project).name,
when: () => args.every((arg) => arg === false),
},
{
type: "list",
name: "project",
message: "What is the type of the project?",
choices: ["Node", "Python"],
default: defaults(project).project,
when: () => args.every((arg) => arg === false),
},
])
.then((answers: Answers) => {
const { name, project: projectName } = defaults(project);
config = setConfig({ name: answers.name || name });
config = setConfig({ project: answers.project || projectName });
})
.then(() =>
prompt([
{
type: "input",
name: "path",
message: "Where is your project root located?",
default: defaults(project).path,
when: () => args.every((arg) => arg === false),
},
{
type: "input",
name: "ignore",
message: "What do you want to ignore? (comma separated)",
default: defaults(project).ignore,
when: () => args.every((arg) => arg === false),
},
]).then((answers: Answers) => {
const { ignore, path } = defaults(project);
config = setConfig(
ignoreFiles(config.project, (answers.ignore || ignore)!)
);
createConfig(answers.path || path, config);
})
);
};

Angular jqxSchedular source localData Can't bind from remote

I am trying to use jqxSchedular for my web app.
Schedular couldn't bind from remote data.
Here is my Angular component:
export class CourseScheduleComponent implements OnInit {
appointmentDataFields: any =
{
from: "start",
to: "end",
description: "description",
subject: "subject",
resourceId: "calendar"
};
source = {
dataType: "array",
dataFields: [
{ name: 'id', type: 'string' },
{ name: 'description', type: 'string' },
{ name: 'subject', type: 'string' },
{ name: 'calendar', type: 'string' },
{ name: 'start', type: 'date' },
{ name: 'end', type: 'date' }
],
localData: []
}
resources: any =
{
colorScheme: "scheme04",
dataField: "calendar",
source: new jqx.dataAdapter(this.source)
};
dataAdapter: any;
date: any = new jqx.date();
views: string[] | any[] =
[
'dayView',
'weekView',
'monthView',
'agendaView'
];
constructor(private repository: RepositoryService,private router: Router,
private activeRoute: ActivatedRoute ) { }
ngOnInit() {
this.getCourseSchedules().subscribe(res=>{
this.source.localData = res as CourseSchedule[];
},err=>{
console.log(err);
});
this.dataAdapter = new jqx.dataAdapter(this.source)
}
getCourseSchedules()
{
var courseId : string = this.activeRoute.snapshot.params['id'];
var apiUrl = `/api/course/schedule?courseId=${courseId}`;
return this.repository.getData(apiUrl).pipe(
map(data => {
let schedules = data as CourseSchedule[];
let newSchedules:CourseSchedule[] = [];
schedules.forEach((schedule) => {
const {start,end,...other} = schedule;
newSchedules.push(<CourseSchedule>{
start: new Date(start),
end: new Date(end),
...other
})
});
return newSchedules;
})
);
}
}
When I debug the ngOnInit there is no problem with setting localData. But when I consolled log source,it shows localdata is null.
I couldnt find for remote databinding example for Angular jqxSchedular.
So ,basicly it works with local data but at remote it doesnt work.
Please help about this.
You have to add them from the jqx component using addAppointment method as below:
getCourseSchedules()
{
let self = this;
var courseId : string = this.activeRoute.snapshot.params['id'];
var apiUrl = `/api/course/schedule?courseId=${courseId}`;
return this.repository.getData(apiUrl).pipe(
map(data => {
let schedules = data as CourseSchedule[];
let newSchedules:CourseSchedule[] = [];
schedules.forEach((schedule) => {
const {start,end,...other} = schedule;
var appointment = {
start: new Date(start),
end: new Date(end),
..other
};
self.myScheduler.addAppointment(appointment);
});
})
);
}
Please refer to the API for more details.

Deep query to nested types in GraphQL return NULL

Have a strange problem...
Query to nested types return null.
But, if I return anything in parent type - resolve return right result
My code:
import { GraphQLList, GraphQLString, GraphQLID, GraphQLObjectType, GraphQLSchema } from 'graphql';
import AdminModel from '../models/Admin.model';
const AdminType = new GraphQLObjectType({
name: 'AdminType',
fields: {
_id: { type: GraphQLID },
login: { type: GraphQLString },
password: { type: GraphQLString }
}
});
const AdminRooteType = new GraphQLObjectType({
name: 'AdminRooteType',
fields: {
getAdmins: {
type: new GraphQLList(AdminType),
resolve() {
return AdminModel.find({})
}
}
}
})
export default new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQuery',
fields: {
admin: {
type: AdminRooteType,
resolve() {
// EMPTY RESOLVE - EMPTY RESULT
}
}
}
})
});
Query:
{
admin {
getAdmins {
login
}
}
}
Result:
{
"data": {
"admin": null
}
}
If I changed returned value in fields admin in RootQuery:
import { GraphQLList, GraphQLString, GraphQLID, GraphQLObjectType, GraphQLSchema } from 'graphql';
import AdminModel from '../models/Admin.model';
const AdminType = new GraphQLObjectType({
name: 'AdminType',
fields: {
_id: { type: GraphQLID },
login: { type: GraphQLString },
password: { type: GraphQLString }
}
});
const AdminRooteType = new GraphQLObjectType({
name: 'AdminRooteType',
fields: {
getAdmins: {
type: new GraphQLList(AdminType),
resolve() {
return AdminModel.find({})
}
}
}
})
export default new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQuery',
fields: {
admin: {
type: AdminRooteType,
#resolve() {#
#// RETURN ANYTHING HERE:#
# return 'foobar'#
}
}
}
})
});
I've got expected result:
{
"data": {
"admin": {
"getAdmins": [
{
"login": "123"
},
{
"login": "12asdf3"
}
]
}
}
}
What is right solution for this issue? (without using dummy values in return)
Thank's a lot!
What you are seeing is the expected behavior. Imagine we have a User type with some fields:
const UserType = new GraphQLObjectType({
name: 'User',
fields: {
id: { type: GraphQLID },
name: { type: GraphQLString },
}
})
And a way to fetch a single user:
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
user: {
type: AdminRooteType,
resolve: () => getUser(),
},
},
})
If getUser returns an object representing a User, the resolvers for all the fields (i.e. id and name on the User type will be called.
When those fields (and whatever child fields they might have) resolve, you end up with a User object for the entire user field to return. A response might look something like:
"data": {
"user": {
"id": 1,
"name": "Maria",
"comments": [
{
"id": 1,
// and so on...
}
]
}
}
Now, consider what happens when a user is not found and we return null instead. Our response looks like this:
"data": {
"user": null
}
It doesn't make sense to call any of the resolvers for the User fields. Would you expect the API to still return an id or name in this case? If it did, what values would those fields have? If we just returned a null id and name, how would the client distinguish that object from a User that existed but really did have id and name null values?
The point is, if a field returns a GraphQLObjectType and it resolves to null, none of the resolvers on the GraphQLObjectType will be called.
By unnecessarily nesting your getAdmins field inside another GraphQLObjectType, you're forced to return some kind of object inside the resolver for admin. So you will need to either live with that, or avoid creating an AdminRootType altogether and just put the getAdmins field on your Query type directly, as per convention:
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
getAdmins: {
type: new GraphQLList(AdminType),
resolve: () => AdminModel.find({}),
},
},
})

How to pass video node from resolve/database to node definitions in graphql relay?

My node definitions looks like this:
class Store {}
let store = new Store()
let nodeDefs = nodeDefinitions(
(globalId) => {
let type = fromGlobalId(globalId).type
let id = fromGlobalId(globalId).id
if (type === 'Store') {
return store
}
if (type === 'Video') {
return docClient.query(
Object.assign(
{},
{TableName: videosTable},
{KeyConditionExpression: 'id = :id'},
{ExpressionAttributeValues: { ':id': id }}
)
).promise().then(dataToConnection)
}
return null
},
(obj) => {
if (obj instanceof Store) {
return storeType
}
if (obj instanceof Video) {
return videoType
}
return null
}
)
The problem is that video node is always null, even when actual video is being returned from the database, because for it to not be null I need to look it up based on id or somehow fetch it from database.
This is the video node I am referring to:
video: {
type: videoType,
args: Object.assign(
{},
connectionArgs,
{id: {type: GraphQLString}}
),
resolve: (_, args) => {
return docClient.query(
Object.assign(
{},
{TableName: pokemonTable},
{KeyConditionExpression: 'id = :id'},
{ExpressionAttributeValues: { ':id': args.id }},
paginationToParams(args)
)
).promise().then(dataToConnection)
}
},
and
const videoType = new GraphQLObjectType({
name: 'Video',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLID),
resolve: (obj) => obj.id
},
name: { type: GraphQLString },
url: { type: GraphQLString }
}),
interfaces: [nodeDefs.nodeInterface]
})
const allVideosConnection = connectionDefinitions({
name: 'Video',
nodeType: videoType
})
I tried doing database query directly inside node definitions, but that didn't work.
dataToConnection just converts the output of dynamoDB:
video DATA!! { Items:
[ { id: 'f4623d92-3b48-4e1a-bfcc-01ff3c8cf754',
url: 'http://www.pokkentournament.com/assets/img/characters/char-detail/detail-pikachuLibre.png',
name: 'YAHOO' } ],
Count: 1,
ScannedCount: 1 }
into something that graphql relay understands:
video dataToConnection!! { edges:
[ { cursor: 'ZHluYW1vZGJjb25uZWN0aW9uOmY0NjIzZDkyLTNiNDgtNGUxYS1iZmNjLTAxZmYzYzhjZjc1NA==',
node: [Object] } ],
pageInfo:
{ startCursor: 'ZHluYW1vZGJjb25uZWN0aW9uOmY0NjIzZDkyLTNiNDgtNGUxYS1iZmNjLTAxZmYzYzhjZjc1NA==',
endCursor: 'ZHluYW1vZGJjb25uZWN0aW9uOmY0NjIzZDkyLTNiNDgtNGUxYS1iZmNjLTAxZmYzYzhjZjc1NA==',
hasPreviousPage: false,
hasNextPage: false } }
and the function itself can be found here: https://github.com/dowjones/graphql-dynamodb-connections/pull/3/files
It could be the problem.
Also, asking/querying for id makes the whole video object null:
But omitting id from the query returns something, whether querying with relay id:
or database id
and querying for all of the videos works:
The interesting part is that I get exactly same problem even if I delete the video part from node definitions:
let nodeDefs = nodeDefinitions(
(globalId) => {
let type = fromGlobalId(globalId).type
let id = fromGlobalId(globalId).id
if (type === 'Store') {
return store
}
return null
},
(obj) => {
if (obj instanceof Store) {
return storeType
}
return null
}
)
Any ideas?
UPDATE:
I did some digging and found that interfaces in fact is undefined
const storeType = new GraphQLObjectType({
name: 'Store',
fields: () => ({
id: globalIdField('Store'),
allVideosConnection: {
type: allVideosConnection.connectionType,
args: Object.assign(
{},
connectionArgs
),
resolve: (_, args) => {
return docClient.scan(
Object.assign(
{},
{TableName: pokemonTable},
paginationToParams(args)
)
).promise().then(dataToConnection)
}
},
video: {
type: videoType,
args: Object.assign(
{},
connectionArgs,
{id: {type: GraphQLString}}
),
resolve: (_, args) => {
return docClient.query(
Object.assign(
{},
{TableName: pokemonTable},
{KeyConditionExpression: 'id = :id'},
{ExpressionAttributeValues: { ':id': args.id }},
paginationToParams(args)
)
).promise().then(dataToConnection)
}
}
}),
interfaces: [nodeDefs.nodeInterface]
})
console.dir(storeType.interfaces, { depth: null })
prints undefined
Why? I clearly define them at the top!
Also, I can do that:
But this doesn't work:
This is what is being returned in video: {} resolve:
{ edges:
[ { cursor: 'ZHluYW1vZGJjb25uZWN0aW9uOmY0NjIzZDkyLTNiNDgtNGUxYS1iZmNjLTAxZmYzYzhjZjc1NA==',
node:
{ id: 'f4623d92-3b48-4e1a-bfcc-01ff3c8cf754',
url: 'http://www.pokkentournament.com/assets/img/characters/char-detail/detail-pikachuLibre.png',
name: 'YAHOO' } } ],
pageInfo:
{ startCursor: 'ZHluYW1vZGJjb25uZWN0aW9uOmY0NjIzZDkyLTNiNDgtNGUxYS1iZmNjLTAxZmYzYzhjZjc1NA==',
endCursor: 'ZHluYW1vZGJjb25uZWN0aW9uOmY0NjIzZDkyLTNiNDgtNGUxYS1iZmNjLTAxZmYzYzhjZjc1NA==',
hasPreviousPage: false,
hasNextPage: false } }
Somehow that's okay for allVideosConnection, but not okay (ends up null) for video
Do I need to convert ids of nodes to global IDs? using toGlobalId ? Just for video ?
Because another thing I noticed is that if I
console.log('fromGlobalId', fromGlobalId(globalId))
inside my node definitions, this query:
{
node(id: "f4623d92-3b48-4e1a-bfcc-01ff3c8cf754") {
id
...F1
}
}
fragment F1 on Video {
url
name
}
becomes this:
fromGlobalId { type: '', id: '\u000e6]_v{vxsn\u001eU/\u001b}G>SW_]O\u001c>x' }
However, if I do
I get
globalId U3RvcmU6
fromGlobalId { type: 'Store', id: '' }
So to make node definitions work, all I had to do was this:
class Video {}
let video = new Video()
return Object.assign(video, data.Items[0])
i.e. create class with the same name as type name
and then Object.assign to it
Just doing this, doesn't work:
return {Video: data.Items[0]}
I also need to create IDs in the database like that: Video:f4623d92-3b48-4e1a-bfcc-01ff3c8cf754, where I am essentially putting type and randomly generated unique id together separated by a colon (:) and then encode it with toGlobalId function of graphql-relay-js library (so I end up with VmlkZW86ZjQ2MjNkOTItM2I0OC00ZTFhLWJmY2MtMDFmZjNjOGNmNzU0Og==), so then I can decode it with fromGlobalId so that node definitions can retrieve both type and id({ type: 'Video', id: 'f4623d92-3b48-4e1a-bfcc-01ff3c8cf754:' }), after which I still need to add fromGlobalId(globalId).id.replace(/\:$/, '')) to remove the trailing colon (:).
`
Also, interfaces are not meant to be accessible, they are just for configuration.

Categories