2 minute read

Tags: , ,

ㄟ~~~~~~ 怎麼跳一天 XDDDDD

絕對不是因為228連假回鄉下吃大餐:meat_on_bone::pizza::hamburger: :satisfied::satisfied::satisfied:

我都不會 GraphQL

CoderBridge 網址 Day06


Day04Day05 建置好的繼續玩下去:)

Enumertaion types

  • schema.js

    • enum 名稱{都大寫}
     import { buildSchema } from 'graphql';
       
     const schema = buildSchema(`
           type Friend{
               id: ID
               firstName: String
               lastName: String
               gender: Gender
               age: Int
               language: String
               email: String
           }
       
           enum Gender{
               MALE
               FEMALE
               OTHER
           }
       
           type Query{
               getFriend(id: ID): Friend
           }
       
           input FriendInput{
             id: ID
             firstName: String!
             lastName: String
             gender: Gender
             age: Int
             language: String
             email: String
           }
       
           type Mutation{
               createFriend(input: FriendInput): Friend
           }
     `)
       
     export default schema;
    
  • resolvers.js

     class Friend {
         constructor(id, {firstName, lastName, gender, age, language, email}){
             this.id = id;
             this.firstName = firstName;
             this.lastName = lastName;
             this.age = age;
             this.language = language;
             this.gender= gender;
             this.email = email;
       
         }
     }
       
     const friendDatabase = {}
       
     const resolvers = {
       
         getFriend: ({ id }) => {
             return new Friend(id, friendDatabase[id]);
         },
       
         createFriend: ({input}) => {
             let id = require('crypto').randomBytes(10).toString('hex'); // random create id
             friendDatabase[id] = input;
             return new Friend(id, input);
         },
     };
       
     export default resolvers;
    

List types

  • schema.js
     import { buildSchema } from 'graphql';
       
     const schema = buildSchema(`
           type Friend{
               id: ID
               firstName: String
               lastName: String
               gender: Gender
               age: Int
               language: String
               email: String
               contacts: [Contact]
           }
       
           type Contact{
               firstName: String
               lastName: String
           }
       
           enum Gender{
               MALE
               FEMALE
               OTHER
           }
       
           type Query{
               getFriend(id: ID): Friend
           }
       
           input FriendInput{
             id: ID
             firstName: String!
             lastName: String
             gender: Gender
             age: Int
             language: String
             email: String
             contacts: [ContactInput]
           }
       
           input ContactInput{
             firstName: String
             lastName: String
           }
       
           type Mutation{
               createFriend(input: FriendInput): Friend
           }
     `)
       
     export default schema;
    
  • resolvers.js

       
     class Friend {
         constructor(id, {firstName, lastName, gender, age, language, email, contacts }){
             this.id = id;
             this.firstName = firstName;
             this.lastName = lastName;
             this.age = age;
             this.language = language;
             this.gender= gender;
             this.email = email;
             this.contacts = contacts
       
         }
     }
     const friendDatabase = {}
     const resolvers = {
         getFriend: ({ id }) => {
             return new Friend(id, friendDatabase[id]);
         },
       
         createFriend: ({input}) => {
             let id = require('crypto').randomBytes(10).toString('hex'); // random create id
             friendDatabase[id] = input;
             return new Friend(id, input);
         },
     };
     export default resolvers;
    
  • 塞資料的樣子 img

可以去官方看文件

GraphQL tool - npm install --save graphql-tools

  • 要安裝 npm install --save graphql-tools
  • 為了要實作 MongoDB 來微微修改一下 code,改了:
    • schema.js
    • resolvers.js
    • index.js
  • schema.js
     import { resolvers } from './resolvers';
     import { makeExecutableSchema, makeRemoteExecutableSchema } from 'graphql-tools'
       
     const typeDefs = `
           type Friend{
               id: ID
               firstName: String
               lastName: String
               gender: Gender
               age: Int
               language: String
               email: String
               contacts: [Contact]
           }
       
           type Contact{
               firstName: String
               lastName: String
           }
       
           enum Gender{
               MALE
               FEMALE
               OTHER
           }
       
           type Query{
               getFriend(id: ID): Friend
           }
       
           input FriendInput{
             id: ID
             firstName: String!
             lastName: String
             gender: Gender
             age: Int
             language: String
             email: String
             contacts: [ContactInput]
           }
       
           input ContactInput{
             firstName: String
             lastName: String
           }
       
           type Mutation{
               createFriend(input: FriendInput): Friend
           }
     `;
       
     const schema = makeRemoteExecutableSchema( {typeDefs, resolvers} )
       
     export {schema};
    
  • resolvers.js

     class Friend {
         constructor(id, {firstName, lastName, gender, age, language, email, contacts }){
             this.id = id;
             this.firstName = firstName;
             this.lastName = lastName;
             this.age = age;
             this.language = language;
             this.gender= gender;
             this.email = email;
             this.contacts = contacts
         }
     }
       
     const friendDatabase = {}
       
     // resolver map
     export const resolvers = {
         Query: {
             getFriend: ({ id }) => {
                 return new Friend(id, friendDatabase[id]);
             },
         },
         Mutation: {
       
             createFriend: ({input}) => {
                 let id = require('crypto').randomBytes(10).toString('hex'); // random create id
                 friendDatabase[id] = input;
                 return new Friend(id, input);
             },
         },
     };
    
  • index.js

     import express from 'express';
     import graphqlHTTP from 'express-graphql';
     import { schema } from './schema';
       
     const app = express();
       
     app.get('/', (req, res) => {
         res.send("GraphQL is amazing");
     });
       
     app.use("/graphql", graphqlHTTP({
         schema: schema,
         graphiql: true,
     }))
       
     app.listen(8080, () => console.log("Running server on port localhost:8080/graphql"));
    

其實我也是跟著影片學 XD 

Linkedin Learning

七天~ 逼一下自己