How to set a default value in GRAPHQL - node.js

I wanna set a default value in the role property but I don´t know how to do it.
The idea is that the role property is "BASIC" by default for all users.
I´m using express.
Sorry for my english, I´m not native, this is the code:
const UserType = new GraphQLObjectType({
name: "User",
description: "User type",
fields: () => ({
id: { type: GraphQLID },
username: { type: GraphQLString },
email: { type: GraphQLString },
displayName: { type: GraphQLString },
phone: { type: GraphQLString },
role: { type: GraphQLString}
}
),
});
thank you!

This is done with the defaultValue property. But this is not possible for the GraphQLObjectType as you show.
const UserType = new GraphQLObjectType({
name: 'User',
description: 'User type',
fields: () => ({
id: { type: GraphQLID },
username: { type: GraphQLString, defaultValue: 'default string' },
}),
});
Object literal may only specify known properties, and 'defaultValue' does not exist in type 'GraphQLFieldConfig<any, any, { [argName: string]: any; }>'
So GraphQLObjectType has no default Value property.
You need to solve this in a different place, not here. For example, when using data, if the value you want is empty, you can use default instead.
...
...
data.username ?? 'default string'
...
...
But where does this defaultValue property work? It works with GraphQLInputObjectType.
For example:
const filter = new GraphQLInputObjectType({
name: "Filter",
fields: () => ({
min: { type: new GraphQLNonNull(graphql.GraphQLInt) },
max: { type: graphql.GraphQLBoolean, defaultValue: 100 },
}),
});
and we can use it like this:
...
query: {
products: {
type: new GraphQLList(productTypes),
args: { filter: { type: new GraphQLNonNull(filter) } }, // <-----
resolve: allProducts,
},
},
...

This is already answered on https://stackoverflow.com/a/51567429/10310278
And official documentation https://graphql.org/graphql-js/type/#example-5
Please check these things out.

Related

mongoDB monggose partly update an object

I have this model.
User.js
methods: {
type: [String],
required: true,
},
local: {
email: {
type: String,
lowercase: true,
},
password: {
type: String,
},
id: Number,
title: {
type: String,
enum: ['Ms', 'Mrs', 'Mr', 'Dr'],
default: 'Mr',
},
firstName: String,
lastName: String,
role: {
type: String,
default: 'user',
},
permissions: [String],
},
status: { type: Boolean, default: true },
Please note that local field has many properties.
Say I only want to update few properties. namely title,lastName and role.
{
lastName:'virat',
role:'manager',
title:'Mr'
}
I tried to update it like this
const filter = { _id: req.params.id };
const update = {
local: {
lastName: "virat",
role: "manager",
title: "Mr",
},
};
await User.findOneAndUpdate(filter, update);
After the update, local has only 3 fields and other fields have been gone.
How do I update certain fields without losing the other fields?
Any help!
Thanks in advance! =)
Try this:
const filter = { _id: req.params.id };
const update = {
"local.lastName": "virat",
"local.role": "manager",
"local.title": "Mr"
};
await User.findOneAndUpdate(filter, update);

Why does the secondaryUser field affect wether findOne works?

In this I am using nodejs with express and mongoose. My question is how does changing the secondaryUser field affect whether or not the findOne works? If I have it as friends.id it works and it finds the right profile, but I want to tie it to the user field in the profile. If I change it to friends.user.id the findOne fails and it sends the 404 error in the catch.
router.post(
"/:handle",
passport.authenticate("jwt", {
session: false
}),
(req, res) => {
Profile.findOne({ handle: req.params.handle }).then(friends => {
const newFriend = new Friend({
initialAccepted: true,
initialUser: req.user.id,
secondaryUser: friends.id
});
newFriend
.save()
.then(Friend => res.json(Friend))
.catch(err =>
res.status(404).json({
friendnotfound: "No people found with that handle"
})
);
});
}
);
The schema used for friend is
const FriendSchema = new Schema({
initialUser: {
type: Schema.Types.ObjectId,
ref: "profile"
},
secondaryUser: {
type: Schema.Types.ObjectId,
ref: "profile"
},
initialAccepted: {
type: Boolean,
default: false
},
initialSecondary: {
type: Boolean,
default: false
},
date: {
type: Date,
default: Date.now()
}
});
This is the schema for the profile
const ProfileSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: "users"
},
handle: {
type: String,
required: true,
max: 40
},
bio: {
type: String
},
platforms: {
type: [String]
},
website: {
type: String
},
social: {
youtube: {
type: String
},
twitter: {
type: String
},
facebook: {
type: String
},
linkedin: {
type: String
},
twitch: {
type: String
}
},
games: [
{
name: {
type: String
},
platform: {
type: String
},
handle: {
type: String
},
rank: {
type: String
}
}
],
date: {
type: Date,
default: Date.now
}
});
Follow proper naming convention for variables
Profile.findOne({ handle: req.params.handle }).then(profile => { // changed name from friends to profile
const newFriend = new Friend({
initialAccepted: true,
initialUser: req.user.id,
secondaryUser: profile.id // changed name from friends to profile
// profile.user.id (ref to user table not provided in schema)
});
if you give profile.user.id the object will not get created ( checking for id inside profile schema but user id provided)
Friend Schema:
secondaryUser: {
type: Schema.Types.ObjectId,
ref: "profile" // checking for id inside profile schema
},

How to use arrays Schema Types for GraphQL

Trying out GraphQL for the first time, and I'd like for one Schema Type to have access to an array of objects within another Schema Type. I'm using local data and lodash to test this before I wire it up to MongoDB. I'm getting a few errors regarding my attempts at it. I know I'm close. Any assistance would be appreciated.
The following was my latest attempt. I accessed the GraphQLList using express-graphql and used GraphQLID to access the second schema.
schema.js
var projects = [
{
name: "Title 1",
subtitle: "Subtitle 1",
summary: "Lorem ipsum....",
languageId: ["4", "2"],
id: "1"
},
...
var languages = [
{ name: "Javascript", id: "1" },
{ name: "HTML", id: "2" },
{ name: "CSS", id: "3" },
{ name: "Python", id: "4" },
]
...
const ProjectType = new GraphQLObjectType({
name: 'Project',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
subtitle: { type: GraphQLString },
summary: { type: GraphQLString },
languages: {
type: new GraphQLList(GraphQLID),
resolve(parent, args) {
console.log(parent)
return _.find(languages, { id: parent.languageId })
}
}
})
});
const LanguageType = new GraphQLObjectType({
name: 'Language',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
})
});
//Root Queries
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
project: {
type: ProjectType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return _.find(projects, { id: args.id });
}
},
language: {
type: LanguageType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return _.find(languages, { id: args.id })
}
}
}
});
The intention was to bring up a list of languages used in that project in Graphiql that would look something like this...
{
"data": {
"project": {
"name": "Project 1"
languages{
names{
"Python"
"HTML"
}
}
}
}
}
Instead I'm getting the following...
{
"errors": [
{
"message": "Syntax Error: Expected Name, found }",
"locations": [
{
"line": 7,
"column": 7
}
]
}
]
}
I reformatted the resolve to hard code an array of languages, and that works, but it's not functional. Any suggestions would be greatly appreciated. Thank you all for your time.
I'm not sure what query you're trying to make.
But in order to return Languages nested in Projects, you should change the return type of the languages field.
Something like:
const ProjectType = new GraphQLObjectType({
name: 'Project',
fields: () => ({
id: {type: GraphQLID},
name: {type: GraphQLString},
subtitle: {type: GraphQLString},
summary: {type: GraphQLString},
languages: {
// change this type to LanguageType
type: new GraphQLList(LanguageType),
resolve(parent, args) {
// No need to lodash here
return languages.filter((language) => {
return parent.languageId.includes(language.id)
})
}
}
})
});
I had a problem with an old project for my tags. in the end, I found it by try and catch u can use this
tags: {type: GraphQLList(GraphQLString)},

GraphQL.js Node/Express: How to pass object as GraphQL query argument

My goal is to be able to pass an object as an argument in a GraphQL query.
Goal:
{
accounts (filter:
{"fieldName": "id",
"fieldValues":["123"],
"filterType":"in"}){
id
}
}
Error:
"message": "filterType fields must be an object with field names as keys or a function which returns such an object."
I've tried a few different approaches but this seems to be the closest to the potential solution.
Schema:
const filterType = new GraphQLObjectType ({
name: 'filterType',
fields: {
fieldName: { type: GraphQLString },
fieldValues: { type: GraphQLString },
filterType: { type: GraphQLString },
}
})
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
accounts: {
type: new GraphQLList(accountType),
args: {
filter: { type: new GraphQLInputObjectType(filterType) },
},
resolve: (root, args, { loaders }) => loaders.account.load(args),
},
}),
});
I have found the solution here.
https://github.com/mugli/learning-graphql/blob/master/7.%20Deep%20Dive%20into%20GraphQL%20Type%20System.md#graphqlinputobjecttype
Schema:
const filterType = new GraphQLInputObjectType({
name: 'filterType',
fields: {
fieldName: { type: GraphQLString },
fieldValues: { type: GraphQLString },
filterType: { type: GraphQLString },
}
})
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
accounts: {
type: new GraphQLList(accountType),
args: {
filter: { type: filterType },
},
resolve: (root, args, { loaders }) => {
return loaders.account.load(args)},
},
}),
});
Problem was in the query, I had the both the keys and values as strings in the object argument.
Correct Query:
{
accounts(filter: {fieldName: "id", fieldValues: "123", filterType: "in"}) {
id
}
}
You don't define filterType as an object type then wrap it in an input type, you literally create it as an input type:
const filterType = new GraphQLInputObjectType({
name: 'filterType',
fields: {
fieldName: { type: GraphQLString },
fieldValues: { type: GraphQLString },
filterType: { type: GraphQLString },
}
})
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
accounts: {
type: new GraphQLList(accountType),
args: {
filter: { type: filterType },
},
resolve: (root, args, { loaders }) => loaders.account.load(args),
},
}),
});
You'll also want to declare its type at query time, as illustrated in #piotrbienias's answer.

How to make a mutation query for inserting a list of (Array) fields in GraphQL

recently I started working on GraphQL, I am able to insert data in flat schema without any problem but when it comes to an Array of data I am getting an error like
{ "errors": [ { "message": "Must be input type" } ]}
I am testing my query using postman, my mutation query is
mutation M {
AddEvent
(
title: "Birthday event"
description:"Welcome to all"
media:[{url:"www.google.com", mediaType:"image" }]
location:[{address:{state:"***", city:"****"}}]
)
{title,description,media,location,created,_id}}
This is my Event Schema:
EventType = new GraphQLObjectType({
name: 'Event',
description: 'A Event',
fields: () => ({
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
id: {
type: GraphQLString,
description: 'The id of the event.',
},
title: {
type: GraphQLString,
description: 'The title of the event.',
},
description: {
type: GraphQLString,
description: 'The description of the event.',
},
media:{
type:new GraphQLList(mediaType),
description:'List of media',
},
location:{
type:new GraphQLList(locationType),
description:'List of location',
}
})
});
// Media Type
export var mediaType = new GraphQLObjectType({
name: 'Media',
description: 'A Media',
fields: () => ({
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
url:{
type: GraphQLString,
description: 'The url of the event.',
},
mediaType:{
type: GraphQLString,
description: 'The mediaTypa of the event.',
}
})
});
// Location Type
export var locationType = new GraphQLObjectType({
name: 'Location',
description: 'A location',
fields: () => ({
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
address:{
type: GraphQLString,
description: 'The address.',
},
state:{
type: GraphQLString,
description: 'The state.',
},
city:{
type: GraphQLString,
description: 'The city.',
},
zip:{
type: GraphQLString,
description: 'The zip code.',
},
country:{
type: GraphQLString,
description: 'The country.',
}
})
});
Mongoose Schema:
var EventSchema = new mongoose.Schema({
title: {
required: true,
type: String,
trim: true,
match: /^([\w ,.!?]{1,100})$/
},
description: {
required: false,
type: String,
trim: true,
match: /^([\w ,.!?]{1,100})$/
},
media: [{
url: {
type: String,
trim: true
},
mediaType: {
type: String,
trim: true
}
}],
location: [{
address: {
type: String
},
city: {
type: String
},
state: {
type: String
},
zip: {
type: String
},
country: {
type: String
}
}]
})
Mutation Type:
addEvent: {
type: EventType,
args: {
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
title: {
type: GraphQLString,
description: 'The title of the event.',
},
description: {
type: GraphQLString,
description: 'The description of the event.',
},
media:{
type:new GraphQLList(mediaType),
description:'List of media',
},
location:{
type:new GraphQLList(locationType),
description:'List of media',
},
created: {
type: GraphQLInt,
description: 'The created of the user.',
}
},
resolve: (obj, {title,description,media,location,created,_id}) => {
let toCreateEvent = {
title,
description,
created:new Date(),
start: new Date(),
media,
location,
_id,
};
return mongo()
.then(db => {
return new Promise(
function(resolve,reject){
let collection = db.collection('events');
collection.insert(toCreateEvent, (err, result) => {
db.close();
if (err) {
reject(err);
return;
}
resolve(result);
});
})
});
}
}
Your issue is that when you define mutations, all types must be input types, hence the error you get "Must be input type". So in here (from your mutation):
media:{
type:new GraphQLList(mediaType),
description:'List of media',
},
location:{
type:new GraphQLList(locationType),
description:'List of media',
},
GraphQLList, mediaType and locationType must be input types.
GraphQLList is already an input type (see here https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82 to see the list of GraphQL types considered as input types).
However your types mediaType and locationType are of GraphQLObjectType type, which is not an input type but if you look at the list of input types again: https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82, you'll find GraphQLInputObjectType which is an object input type, so, what you need to do is to replace mediaType and locationType by their "input" version.
What I suggest to do is to create mediaInputType and locationInputType which would have the same field structure as mediaType and locationType but created with new GraphQLInputObjectType({... instead of new GraphQLObjectType({... and use them in your mutation.
I ran into the same issue and I solved it like that, feel free to comment if you have any question.
I ran into the same problem - I did not know how to specify array of objects in the input definition. So for those who wants to see a "text" schema solution:
type Book {
title: String!
}
to have an array of Books in your input type
input AuthorInput {
name: String!
age: Int!
}
you can not just add books: [Book!] inside the input statement, you will need deliberately create input type containing needed fields (duplicate if you like):
input BookInput {
title: String!
}
and then you can:
input AuthorInput {
name: String!
age: Int!
books: [BookInput!]
}

Resources