https://github.com/akshayjadhav4/graphql-learning
Learning GraphQL
https://github.com/akshayjadhav4/graphql-learning
graphql graphql-js
Last synced: about 1 month ago
JSON representation
Learning GraphQL
- Host: GitHub
- URL: https://github.com/akshayjadhav4/graphql-learning
- Owner: akshayjadhav4
- Created: 2020-12-15T10:21:44.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-15T10:22:30.000Z (over 5 years ago)
- Last Synced: 2025-03-27T13:51:34.299Z (about 1 year ago)
- Topics: graphql, graphql-js
- Language: JavaScript
- Homepage:
- Size: 127 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Learning GraphQL
## Create Schema
```
import { buildSchema } from "graphql";
const schema = buildSchema(`
type Course {
id: ID
courceName: String
category: String
price: Int
language: String
email: String
stack: Stack
teachingAssists: [TeachingAssists] //refernce to another type TeachingAssists
}
type TeachingAssists {
firstName: String
lastName : String
experience: Int
}
enum Stack {
REACT
MERN
GraphQL
JS
REACT NATIVE
FLUTTER
IOS
}
type Query {
getCourse(id: ID): Course //defined method to get Course data
}
input CourseInput {
id: ID
courceName: String! // ! symbol used to make field required while getting input
category: String!
price: Int!
language: String
email: String
stack: Stack
teachingAssists: TeachingAssists
}
type Mutation {
createCourse(input: CourseInput): Course
}
`);
export default schema;
```
## Create Resolver
_Not saving data in DB (saving in local object)_
```
import { nanoid } from "nanoid";
class Course {
constructor(
id,
{ courceName, category, price, language, email, stack, teachingAssists }
) {
this.id = id;
this.courceName = courceName;
this.category = category;
this.price = price;
this.language = language;
this.email = email;
this.stack = stack;
this.teachingAssists = teachingAssists;
}
}
const CourseHolder = {};
const resolvers = {
getCourse: ({ id }) => {
return new Course(id, CourseHolder[id]);
},
createCourse: ({ input }) => {
let id = nanoid();
CourseHolder[id] = input;
return new Course(id, input);
},
};
export default resolvers;
```
## GraphQL setup
```
import express from "express";
import resolvers from "./resolvers";
import schema from "./schema";
import { graphqlHTTP } from "express-graphql";
const app = express();
app.get("/", (req, res) => {
res.send("Set up Done.");
});
const root = resolvers;
//graphiql:true flag gives web ui to work with GraphQL
app.use("/graphql", graphqlHTTP({ schema, rootValue: root, graphiql: true }));
const PORT = ANY_PORT_NUMBER;
app.listen(PORT, () => console.log(`Server Running on port ${PORT}`));
```
## To run
go to **localhost:port/graphql** in browser and GraphQL window will open.
**Mutation operation (Creating Course)**
```
mutation{
createCourse(input:{
courceName: "GraphQL"
category: "WEB"
price: 299
language: "English"
email: "teach@one.com"
stack: GraphQL
teachingAssists: [{
firstName:"Teacher"
lastName:"One"
experience:5
},
{
firstName:"Teacher"
lastName:"Two"
experience:5
},
]
}){
//here we are seecting values which we want to see from response
id
courceName
}
}
```
**Result**

**Query operation (get data)**
```
query{
getCourse(id:"kmznLTLjB6evzPJ4wWNmp"){
id
courceName
teachingAssists{
firstName
experience
}
}
}
```
**Result**
