Skip to content

Commit 52ab05f

Browse files
committed
init
1 parent e6d3441 commit 52ab05f

File tree

5 files changed

+1192
-1
lines changed

5 files changed

+1192
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+

README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,49 @@
1-
# graphql-basics
1+
# Gr
2+
3+
# Graphql
4+
5+
GraphQL is query language, a middle layer that defines the API between the front end and backend. It is a way of querying the data you need in a concise, clean manner.
6+
7+
# Motivation
8+
9+
10+
A web api is a protocol for interfacing between the client and the server. The current most-commonly used protocol today is REST, in which theyre a multiple types of requests such as get, put, post, and delete which contain headers and a body. This allows it to send and recieve the necessary information for the client and server to interface and send eachother the necessary information.
11+
12+
As the application scales though, the API needs to accomodate many requests that all do very specific things. Developers dealt with this with two ways:
13+
14+
1. Having many very specific routes, but makes the code difficult to maintain as the api evolves and the client and servr side both have to stay in sync.
15+
16+
2. Having general endpoints that can be used for multipl purposes, but send and recieve more data than necessary.
17+
18+
Graphql is a protocol that solves this my deisgning a query language that allows you to pprecisely describe what you wanted to do. It also provides a single endpoint in which the clinet can communicate easily what it needs to do.
19+
20+
21+
## Defining the Schema
22+
23+
The schema is a model of the data that describes the structure of the data and the relationships in between them.
24+
25+
For instance, if we wanted to define a student with a name, we would define it as a type.
26+
27+
```javascript
28+
const schema = gql`
29+
type Student {
30+
name: String!
31+
}
32+
`;
33+
34+
```
35+
36+
The exclamation mark after ```String``` above means that it is nonullable, and the ```gql``` tag in the beginning allows it to be parsed as graphql.
37+
38+
## Interacting with the schema
39+
40+
There are three types of interactions: querries, mutations, and subscirptions.
41+
42+
### Querries
43+
44+
45+
46+
47+
48+
49+

index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const express = require("express");
2+
const cors = require("cors");
3+
const { ApolloServer, gql } = require("apollo-server-express");
4+
5+
const app = express();
6+
7+
app.use(cors());
8+
9+
const schema = gql`
10+
type Query {
11+
me: User
12+
}
13+
14+
type User {
15+
username: String!
16+
}
17+
`;
18+
19+
const resolvers = {
20+
Query: {
21+
me: () => {
22+
return {
23+
username: "Robin Wieruch"
24+
};
25+
}
26+
}
27+
};
28+
29+
const server = new ApolloServer({
30+
typeDefs: schema,
31+
resolvers
32+
});
33+
34+
server.applyMiddleware({ app, path: "/graphql" });
35+
36+
app.listen({ port: 8000 }, () => {
37+
console.log("Apollo Server on http://localhost:8000/graphql");
38+
});

0 commit comments

Comments
 (0)