Day06: GraphQL - Enumeration and list types wiht node.js


Day06: GraphQL - Enumeration and list types wiht node.js

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
七天~ 逼一下自己

我都不會 GraphQL
模仿是最好的學習,耶! 一直很想學會&實作 GraphQL。就靠這七天拉!!! 七天內,把學到的嘎成文字和筆記:) PS.主要跟著: https://www.linkedin.com/learning/paths/stay-ahead-in-graphql?u=43607124






Related Posts

一起來看 Joshua B. Tenenbaum 教授有趣的認知科學研究 - Building Machines that Learn and Think Like People

一起來看 Joshua B. Tenenbaum 教授有趣的認知科學研究 - Building Machines that Learn and Think Like People

Day02_Origami學習筆記

Day02_Origami學習筆記

大數乘法

大數乘法

那些年不懂的JS - Event Loop

那些年不懂的JS - Event Loop

Day06:從經典案例看 bytecode

Day06:從經典案例看 bytecode

從製作 visfest 2019 badge 認識 ObservableHQ

從製作 visfest 2019 badge 認識 ObservableHQ



Comments