36
loading...
This website collects cookies to deliver better user experience
const graphql = require("graphql");
const {
GraphQLObjectType,
GraphQLSchema,
GraphQLInt,
GraphQLString,
GraphQLList,
} = graphql;
//mock data
const data1 = require("../database/mockdata3.json");
// this is typedef we can see this after
const UserType = require("./TypeDefs/UserType");
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
getAllUsers: {
type: new GraphQLList(UserType),
// you can pass argument
args: { Name: { type: GraphQLString } },
resolve(parent, args) {
// write return what you need back
return data1;
},
},
},
});
// finally we are going to export this
module.exports = new GraphQLSchema({ query: RootQuery });
const UserType = new GraphQLObjectType({
name: "User",
fields: () => ({
//any fields you can define but you must enter the data-type
Name: { type: GraphQLString },
Age: { type: GraphQLInt },
}),
});
module.exports = UserType;
const express = require("express");
const app = express();
const PORT = 6969;
const { graphqlHTTP } = require("express-graphql");
const schema = require("./Schemas/Schema");
const cors = require("cors");
app.use(cors());
app.use(express.json());
app.use(
"/data",
graphqlHTTP({
schema,
// this is graphql playground field
graphiql: true,
})
);
app.listen(PORT, () => {
console.log("Server running");
});