Is there a documentation for this. I'm unable to figure out where to pass this arguments. I have tried passing it to apolloServer.
graphQLServer.use('/graphql', apolloServer({
graphiql: true,
pretty: true,
schema: Schema,
resolvers: Resolvers,
resolverValidationOptions: { requireResolversForNonScalar: false }
//mocks: Mocks,
}));
@maytis You can find the documentation here: http://docs.apollostack.com/graphql-tools/generate-schema.html
There's a second option in resolverValidationOptions, which is called requireResolversForArgs, presumably you want to set that to false as well.
Thanks helfer, From the example, I should pass jsSchema to Schema attribute in ApolloServer or as object ? Since, resolvers are already being passed on to the jsSchema.
The schema passed to apollo server should be an instance of GraphQLSchema from the graphql npm package.
@maytis were you able to resolve the issue?
I used to face the same problem, and found it's my misunderstood in writing of resolver function.
{"errors":[{"message":"Resolve function missing for \"RootQuery.Config\""}]}
export const resolvers = {
RootMutation: {
getToken(root, { username, password }, context) {
return context.User.login(username, password);
},
},
RootQuery: {
Config: {
alarmTypes(_, __, context) {
return context.Config.getAlarmTypes();
},
},
},
ConfigType: {
alarmTypes(_, __, context) {
return context.Config.getAlarmTypes();
},
},
AlarmCodeType: {
code(a, b, c) {
console.log(a, b, c);
},
label(a, b, c) {
console.log(a, b, c);
},
},
};
export const typeDefinitions = `schema {
query: RootQuery
mutation: RootMutation
}
type RootMutation {
getToken(username: String!, password: String!): TokenType
}
type TokenType {
token: String!
error: String!
}
type RootQuery {
Config: ConfigType
#User: UserType
#Company: PowerEntityType
}
type ConfigType {
alarmTypes: [AlarmCodeType!]
}
type AlarmCodeType {
code: Int!
label: String!
}
Now I write:
export const resolvers = {
RootMutation: {
getToken(root, { username, password }, context) {
return context.User.login(username, password);
},
},
RootQuery: {
Config(root, args, context) {
return context.Config.getAlarmTypes();
},
},
ConfigType: {
alarmTypes(_, __, context) {
return context.Config.getAlarmTypes();
},
},
AlarmCodeType: {
code(a, b, c) {
console.log(a, b, c);
},
label(a, b, c) {
console.log(a, b, c);
},
},
};
it's ok now
Hi, I can't understood it. Can anyone help me to get what the last informated defining.
And i am facing the same issue.. when i try to merge the schemas and resolvers of 2 models called product and users.. and i am new graphql.
If anyone redirect me to the right path for merging two models then it is very favourable to me.
I am creating API using graphql-tools and apollo-server..
`module.exports =
type product {
id: ID
userId: Int
description: String
images: [String]
categoryId: Int
viewers: [Int]
city: String
state: String
country: String
latitude: String
longitude: String
isFree: Boolean
createdAt: String
updatedAt: String
likedUsers: [Int]
status: String
make: String
model: String
year: Int
serviceCategory: String
service: String
rate: Int
currencyCode: String
bodyType: String
transmission: String
fuelType: String
driveTrain: String
seats: String
mileage: Int
}
type Query {
getProduct: product
getAllProducts: [product]
}
input productData {
description: String
images: [String]
categoryId: Int
city: String
state: String
country: String
latitude: String
longitude: String
isFree: Boolean
make: String
model: String
year: Int
serviceCategory: String
service: String
rate: Int
currencyCode: String
bodyType: String
transmission: String
fuelType: String
driveTrain: String
seats: String
mileage: Int
}
type Mutation {
createProduct(data: productData): product
editProduct( id: Int!, userId: Int!, data: productData): product
};`
`const resolvers = {
Query: {
getProduct: async (root, {id}, {currentUser, product}) => {
console.log(args, user, currentUser);
if (!currentUser) {
throw new Error('User Not Authenticated');
} else {
const foundProduct = await product.findOne({id});
return foundProduct;
}
},
getAllProducts: async(root, args, {currentUser, product}) => {
console.log(args, product, currentUser);
const products = await product.find();
return products;
}
},
Mutation: {
createProduct: async (root, args, {currentUser, product}) => {
if(!currentUser) {
throw new Error('User Not Authenticated');
} else {
const newProduct = await new product(args).save();
return newProduct;
}
},
editProduct: async (root, {id, userId, data}, {currentUser, product}) => {
if (!currentUser) {
throw new Error('User Not Authenticated');
} else if (currentUser.id !== userId) {
throw new Error('This user have no permissions to edit this product');
} else {
const foundProduct = await product.findOneAndUpdate({id}, {$set: data}, {new: true});
if(!foundProduct){
throw new Error('Product Not Found');
}
return foundProduct;
}
}
}
};
module.exports = resolvers;`
`module.exports =
type User {
id: ID
firstName: String!
lastName: String!
password: String!
bio: String
profileImage: String
email: String! @unique
currencyCode: String
TimeZone: Int
status: String
conversationId: Int
faceBookId: String
googleId: String
deviceId: String
blocked: [Int]
favourites: [Int]
faceBookVerified: Boolean
googleVerified: Boolean
phoneVerified: Boolean
emailVerified: Boolean
rememberToken: String
phone: Int
createdAt: String
updatedAt: String
city: String
state: String
country: String
latitude: String
longitude: String
unit: String
radius: Float
}
type Token {
token: String!
}
type Query {
getCurrentUser: User
}
input userInput {
firstName: String,
lastName: String,
email: String,
bio: String,
profileImage: String,
password: String,
city: String
state: String
country: String
latitude: String
longitude: String
unit: String
radius: Float
}
type Mutation {
signup(deviceId: String, googleId: String, facebookId: String, firstName: String!, lastName: String!, email: String!, password: String, profileImage: String): Token
signin(deviceId: String, googleId: String, facebookId: String, email: String!, password: String!, profileImage: String): Token
editProfile(email: String!, data: userInput): User
};`
`const jwt = require('jsonwebtoken');
var bcrypt = require('bcrypt');
const createToken = (user, secret, expiresIn) => {
const { firstName, email, id} = user;
return jwt.sign({firstName, email, id}, secret, {expiresIn})
};
const resolvers = {
Query: {
getCurrentUser: async (root, args, {user, currentUser}) => {
if (!currentUser) {
throw new Error('User Not Authenticated');
} else {
const foundUser = await user.findOne({email: currentUser.email});
return foundUser;
}
}
},
Mutation: {
signup: async (root, args, {user}) => {
console.log(args);
const foundUser = await user.findOne({email: args.email}).exec();
if(foundUser) {
throw new Error('User already exits');
}
const newUser = await new user(args).save();
return {token: createToken(newUser, process.env.JWT_SECRET, "1yr")};
},
signin: async (root, {email, password}, {user}) => {
const foundUser = await user.findOne({email});
if(!foundUser){
throw new Error('User Not Found');
}
const isValidPassword = await bcrypt.compare(password, foundUser.password);
if(!isValidPassword){
throw new Error('inValid password');
}
return {token: createToken(foundUser, process.env.JWT_SECRET, "1yr")};
},
editProfile: async (root, {email, data}, {currentUser, user}) => {
if(!currentUser) {
throw new Error('User Not Authenticated');
} else {
if (!!data.password) {
const saltRounds = 10;
data.password = await bcrypt.hash(data.password, saltRounds);
}
const foundUser = await user.findOneAndUpdate({email}, {$set: data}, {new: true});
if(!foundUser){
throw new Error('User Not Found');
}
return foundUser;
}
}
}
};
module.exports = resolvers;
`
I want to combine those 2 schemas and resolvers to pass it as a single thing in a server file using makexecuable schema.. But I can't merge those both things, I am getting errors...
@lawanyaselvamani Please format your code.