我是相当新的graphQL。我的要求是在发送查询时,graphQL应该返回如下的JSON响应
{
user: '$',
name: 'Josh',
age: '30'
}
我尝试了以下方式,但没有达到结果。
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');
有人可以指导我要去哪里哪里
谢谢。
答案 0 :(得分:0)
我通过以下方法实现:
var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');
const { GraphQLJSON, GraphQLJSONObject } = require('graphql-type-json');
var schema = buildSchema(`
scalar JSON
type Query {
getObject: JSON
}
`);
var root = {
JSON: GraphQLJSON,
getObject: () => {
return {
name: "jshua",
age: "30",
place: "china",
gender: "Male"
}
}
};
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');