Git Product home page Git Product logo

nest-graphql's Introduction

udemy: https://www.udemy.com/share/108fp43@wD4vjdVtuFA9qRKBsl6ZkXrHBA6L4egbulCUvsHHOIlrrKkWfr6q_1OAP0I7rB6Hqg==/

GraphQL

スカラー型

  1. Int: 整数型
  2. Float: 浮動小数点数型
  3. String: 文字列型
  4. Boolean: 論理型
  5. ID: ユニークな文字列型

ListとNullable

  • !がついていたらNot Null
type User {
  tasks: [Task]!
}
定義 意味
[String]! リスト自体が必ず存在するがnull要素を持つ可能性がある
[String!] 中身の要素は必ず1つ以上存在するが、リスト自体がnullになる可能性がある
[String!]! リストが必ず存在し、中身も必ず1つ以上存在する
[String] リスト自体も中身もnullになる可能性がある

EnumとUnion

enum Color {
  RED
  BLUE
  GREEN
}
union Vehicle = Car | Bike

Query型

type Query = {
  getTasks(): [Task]!
  getTaskById(id: Int!): Task
}

Mutation型

type Mutation = {
  createTask(id: Int!, name: String!): Task!
  deleteTask(id: Int!): Task!
}

コードファーストとスキーマファースト

  • コードファースト
    • Nestでは、TypeScriptのコードに特定のでコレーたを付与しGraphQLスキーマを自動生成
    • フロントではバックエンドのコードを基に生成されたスキーマを参照
  • スキーマファースト

NestJS

https://docs.nestjs.com/

  • Angularにインスパイアされている
  • ExpressをベースとしているのでExpressで出来ることはNestでも出来る
    • Expressの機能やライブラリを利用することができる
    • optionでfastifyをベースにすることも可能
  • 拡張性が高い
  • nest cliが便利
  • Express, FastifyでのネイティブAPIをそのまま使用できる

Why need Nest ?

Nodeにおける便利なライブラリが日々誕生しているものの、アーキテクチャを標準化するという 問題を解決するものはなかった、そこをNestが担う。

アーキテクチャ

  • ルートはmain.tsのapp.module.ts
    • これをルートモジュールと呼ぶ
  • その下にさまざまなxxx.module.tsがある
    • それごとに、xxx.service.ts, xxx.resolver.tsが出来上がる
  • Dependency Injectionを簡単に出来る仕組みがある → DIコンテナ
  1. ModuleのProviderに依存される側のクラスを登録する
  2. 依存する側のConstructorで注入される側のクラスを引数として受け取る

Module

  • Resolver, Serviceをまとめ、アプリケーションとして利用できるようNestに登録
  • 必ず一つのルートモジュールが必要
  • classに@Moduleでコレーたをつけて定義する
@Module({
  imports: [PrismaModule],
  providers: [TaskResolver, TaskService]
})
export class TaskModule {}

Controller (Resolver)

  • GraphQLスキーマに定義されたデータを返却する
  • REST API(MVC)におけるControllerと似た立ち位置
  • @Resolverデコレータを付与、さらに中のメソッドにも@Queryと@Mutationデコレータを付与
@Resolver()
export class TaskResolver {
  @Query(() => [Task])
  getTasks() {
    // ...
  }

  @Mutation(() => Task)
  createTask() {
    //..
  }
}

Service

  • アプリケーション固有のビジネスロジックを定義する
  • リゾルバーに書いても動作はするが、責務を分けることで保守性が上がる
// @Service ではない
@Injectable()
export class UserService {
  // ビジネスロジックを実現するメソッドの作成
  find(userName: number) {
    // ...
  }
}

instllation

nest new backend
cd backend
yarn add @nestjs/graphql @nestjs/apollo @apollo/server graphql

nest-graphql's People

Contributors

s-kawabe avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.