Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking graphql server

Tags:

graphql

I'm trying to mock my graphql server, so that when I query, it returns back mock data instead of going to the graphql server. At the moment when I run my query against the mock graphql, I'm getting back this error:

"errors":[{"message":"Syntax Error GraphQL request (1:13) Expected $, found Name \"input\"\n\n1: query login(input: {userName: \"james\" , passWord: \"password\"}

Here's my current setup:

Query

let query = `query login(input: {userName: "james" , passWord: "password"}){
   login(input: {userName: "james" , passWord: "password"})
}`;

Schema

const typeDefs = `
type loginCrendentialsType {
  data: String
}
input loginInputType{
  userName: String!
  passWord: String!
}
type RootQuery {
  login(input: loginInputType): [loginCrendentialsType]
}
schema {
  query: RootQuery
}
`;

Mock

const mocks = {
 login: (loginInputType) => ({
   data: mockDB.getUser(loginInputType.userName, loginInputType.passWord)
 })
};

setup

const schema = makeExecutableSchema({
 typeDefs
});

addMockFunctionsToSchema({
 schema,
 mocks,
});

graphql(schema,query).then((result) => {
  console.log("reachedhere  " + JSON.stringify(result));
});
like image 415
user3162979 Avatar asked Oct 29 '22 12:10

user3162979


1 Answers

I think when you provide input variables, it must be mutation not query??

use the variable to store the input object and assign it the $input

let query = `query login($input: {input: {userName: "james" , passWord: "password"} }){
   login(input: $input)
}`;

OR

let query = `query login{
   login(input: {userName: "james" , passWord: "password"})
}`;
like image 192
Parwat Kunwar Avatar answered Jan 02 '23 20:01

Parwat Kunwar