2016-09-06 16:06:00 +05:30
|
|
|
// Import the required libraries
|
2017-04-11 21:30:06 +02:00
|
|
|
const graphql = require('graphql');
|
|
|
|
const graphqlHTTP = require('express-graphql');
|
|
|
|
const express = require('express');
|
|
|
|
const cors = require('cors');
|
2018-04-21 19:19:05 +03:00
|
|
|
const { logger } = require('@storybook/node-logger');
|
2016-09-06 16:06:00 +05:30
|
|
|
|
|
|
|
// Import the data you created above
|
2017-04-11 21:30:06 +02:00
|
|
|
const data = require('./data.json');
|
2016-09-06 16:06:00 +05:30
|
|
|
|
|
|
|
// Define the User type with two string fields: `id` and `name`.
|
|
|
|
// The type of User is GraphQLObjectType, which has child fields
|
|
|
|
// with their own types (in this case, GraphQLString).
|
2017-04-11 21:30:06 +02:00
|
|
|
const userType = new graphql.GraphQLObjectType({
|
2016-09-06 16:06:00 +05:30
|
|
|
name: 'User',
|
|
|
|
fields: {
|
|
|
|
id: { type: graphql.GraphQLString },
|
2017-04-14 09:26:27 +02:00
|
|
|
name: { type: graphql.GraphQLString },
|
|
|
|
},
|
2016-09-06 16:06:00 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
// Define the schema with one top-level field, `user`, that
|
|
|
|
// takes an `id` argument and returns the User with that ID.
|
|
|
|
// Note that the `query` is a GraphQLObjectType, just like User.
|
|
|
|
// The `user` field, however, is a userType, which we defined above.
|
2017-04-11 21:30:06 +02:00
|
|
|
const schema = new graphql.GraphQLSchema({
|
2016-09-06 16:06:00 +05:30
|
|
|
query: new graphql.GraphQLObjectType({
|
|
|
|
name: 'Query',
|
|
|
|
fields: {
|
|
|
|
user: {
|
|
|
|
type: userType,
|
|
|
|
// `args` describes the arguments that the `user` query accepts
|
|
|
|
args: {
|
2017-04-14 09:26:27 +02:00
|
|
|
id: { type: graphql.GraphQLString },
|
2016-09-06 16:06:00 +05:30
|
|
|
},
|
|
|
|
// The resolve function describes how to "resolve" or fulfill
|
|
|
|
// the incoming query.
|
|
|
|
// In this case we use the `id` argument from above as a key
|
|
|
|
// to get the User from `data`
|
2017-04-11 21:30:06 +02:00
|
|
|
resolve(_, args) {
|
2016-09-06 16:06:00 +05:30
|
|
|
return data[args.id];
|
2017-04-14 09:26:27 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2016-09-06 16:06:00 +05:30
|
|
|
});
|
|
|
|
|
2017-09-06 00:54:12 +02:00
|
|
|
express()
|
|
|
|
.use(cors())
|
|
|
|
.use('/graphql', graphqlHTTP({ schema, pretty: true }))
|
|
|
|
.listen(3000);
|
2016-09-06 16:06:00 +05:30
|
|
|
|
2018-04-21 19:19:05 +03:00
|
|
|
logger.info('GraphQL server running on http://localhost:3000/graphql');
|