Im pretty new graphQL. My requirement is when sending a query, graphQL should return a JSON response as below
{
user: '$',
name: 'Josh',
age: '30'
}
I tried in following way but not achieved the result.
var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');
var schema = buildSchema(`
type Query {
user: String!
}
`);
// JSON
var json = {
user: '$',
name: 'Josh',
age: '30'
};
var root = {
user: () => {
return json;
}
};
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');
Can someone guide where I'm going wrong.
Thanks in advance.
Stringfor user but probably an object of typeUserand then define the user type:type User { user: String! name: String! age: String! }. Consider converting age toInt.