GithubHelp home page GithubHelp logo

Comments (7)

linonetwo avatar linonetwo commented on April 30, 2024 1

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

from graphql-tools.

lawanyaselvamani avatar lawanyaselvamani commented on April 30, 2024 1

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...

from graphql-tools.

linonetwo avatar linonetwo commented on April 30, 2024 1

@lawanyaselvamani Please format your code.

from graphql-tools.

helfer avatar helfer commented on April 30, 2024

@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.

from graphql-tools.

maytis avatar maytis commented on April 30, 2024

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.

from graphql-tools.

helfer avatar helfer commented on April 30, 2024

The schema passed to apollo server should be an instance of GraphQLSchema from the graphql npm package.

from graphql-tools.

helfer avatar helfer commented on April 30, 2024

@maytis were you able to resolve the issue?

from graphql-tools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.