1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| const express = require('express'); const graphql = require('graphql'); const graphqlHTTP = require('express-graphql');
var AccountType = new graphql.GraphQLObjectType({ name: 'Account', fields: { name: { type: graphql.GraphQLString }, age: { type: graphql.GraphQLInt }, sex: { type: graphql.GraphQLString }, department: { type: graphql.GraphQLString } } });
var queryType = new graphql.GraphQLObjectType({ name: 'Query', fields: { account: { type: AccountType, args: { username: { type: graphql.GraphQLString } }, resolve: function (_, { username }) { const name = username; const sex = 'male'; const age = 18; const department = 'school'; return { name, sex, age, department } } } } });
var schema = new graphql.GraphQLSchema({ query: queryType });
const app = express();
app.use('/graphql', graphqlHTTP({ schema: schema, graphql: true }));
app.listen(3000);
|