Skip to content

Commit

Permalink
28-01-first-query
Browse files Browse the repository at this point in the history
  • Loading branch information
leo41271 committed Jul 30, 2024
1 parent ec5a1a1 commit e48121c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
47 changes: 30 additions & 17 deletions 28 GraphQL/backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const multer = require('multer');

const feedRoutes = require('./routes/feed');
const authRoutes = require('./routes/auth');
/** UPDATED CONFIG
* express-graphql => graphql-http
* and express-graphiql-explorer (graphiql)
*/
const grapqlHttp = require('graphql-http/lib/use/express');
const graphiql = require('express-graphiql-explorer');
/** ================================== */
const graphqlSchema = require('./graphql/schema');
const graphqlResolver = require('./graphql/resolvers');

const app = express();
/** SOCKET-IO CONFIGURATION */
const http = require('http');
const server = http.createServer(app);
/** ======================= */

const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
Expand Down Expand Up @@ -56,8 +58,26 @@ app.use((req, res, next) => {
next();
});

This comment has been minimized.

Copy link
@leo41271

leo41271 Aug 30, 2024

Author Owner
  1. Adding Input Validation
    一般會在 middleware 來進行 validator 。 但現在有了 graphql 故 減少 API 。 後面會將 此部分寫謝進去。
    app.use('/feed', feedRoutes);
    app.use('/auth', authRoutes);

This comment has been minimized.

Copy link
@leo41271

leo41271 Aug 30, 2024

Author Owner

this is the only endpoint we have and we certainly don't want to validate all requests in exactly the same way.
So to change that and to adjust it to our needs, we want to move validation into our resolvers.

app.use('/feed', feedRoutes);
app.use('/auth', authRoutes);
/** EXPRESS-GRAPHIQL-EXPLORER PACKAGE */
/** note: /graphiql endpoint */
app.use(
'/graphiql',
graphiql({
graphQlEndpoint: '/graphql',
defaultQuery: `query MyQuery {}`,

This comment has been minimized.

Copy link
@leo41271

leo41271 Aug 30, 2024

Author Owner

425 Adding a Mutation Resolver & GraphiQL
透過在此處增加一行配置:
graphiql : true,
讓你可以用它專用的 測試 。此時 app.use( 在 localhost8080/graphql >> 會被當成是
get request。 比postman 還好

})
);
/** ================================= */

/** GRAPHQL-HTTP CONFIGURATION */
app.all('/graphql', (req, res) =>
grapqlHttp.createHandler({
schema: graphqlSchema,
rootValue: graphqlResolver,
context: { req, res },
})(req, res)
);
/** ========================== */

app.use((error, req, res, next) => {
console.log(error);
Expand All @@ -75,13 +95,6 @@ mongoose
'mongodb://127.0.0.1:27017/messages?retryWrites=true&authSource=admin'
)
.then(() => {
/** SEE LINES 12-15 -- UPDATED CONFIGURATION */
const io = require('./socket').init(server);
io.on('connection', (socket) => {
console.log('Client connected.');
});
/** ======================================== */
/** LISTEN TO CUSTOM SERVER INSTANCE */
server.listen(8080);
app.listen(8080);
})
.catch((err) => console.log(err));
8 changes: 8 additions & 0 deletions 28 GraphQL/backend/graphql/resolvers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
hello() {
return {
text: 'Hello World!',
views: 1,
};
},
};
16 changes: 16 additions & 0 deletions 28 GraphQL/backend/graphql/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { buildSchema } = require('graphql');

module.exports = buildSchema(`
type TestData {
text: String!
views: Int!
}
type RootQuery {
hello: TestData!

This comment has been minimized.

Copy link
@leo41271

leo41271 Aug 30, 2024

Author Owner

schema 有定義什麼 對應的 resolvvers 就也要有那個方法>> hello()

text: String!
String 透過對它加上 "!" 來表示它為必填, 如果返回的結果中沒有String 會得到錯誤

423 Understanding the Setup & Writing our First Query important 10:00

}
schema {
query: RootQuery
}
`);
6 changes: 4 additions & 2 deletions 28 GraphQL/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.2",
"express": "^4.19.2",
"express-graphiql-explorer": "^0.0.3",
"express-validator": "^7.0.1",
"graphql": "^15.8.0",
"graphql-http": "^1.22.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.2.4",
"multer": "^1.4.5-lts.1",
"npm": "^10.5.0",
"socket.io": "^4.7.5"
"npm": "^10.5.0"
},
"devDependencies": {
"nodemon": "^3.1.0"
Expand Down

1 comment on commit e48121c

@leo41271
Copy link
Owner Author

@leo41271 leo41271 commented on e48121c Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.