Nestjs Graphql

文档
工作示例

安装依赖:

npm i --save @nestjs/graphql apollo-server-express graphql-tools graphql

app.module.ts

import { Module } from '@nestjs/common';
// import { AppController } from './app.controller';
import { AppService } from './app.service';

import { GraphQLModule } from '@nestjs/graphql';
import { AppResolver } from './app.resolvers';

@Module({
  imports: [
    GraphQLModule.forRoot({
      typePaths: ['./**/*.graphql'],
    }),
  ],
  // controllers: [AppController],
  providers: [AppService, AppResolver],
})
export class AppModule {}

定义 typeDefs :

// app.graphql
type Query {
  hello: String
}

定义 resolvers :

// app.resolvers.ts
import { Query, Resolver } from '@nestjs/graphql';

@Resolver()
export class AppResolver {
  constructor() {}

  @Query()
  hello(): string {
    return 'hello nest.js';
  }
}

启动服务器,访问 http://localhost:5000/graphql

// 发送
query { hello }

// 返回
{
  "data": {
    "hello": "hello nest.js"
  }
}

原文地址:https://www.cnblogs.com/ajanuw/p/10343127.html

时间: 2024-10-10 10:23:13

Nestjs Graphql的相关文章

GraphQL介绍&使用nestjs构建GraphQL查询服务

GraphQL介绍&使用nestjs构建GraphQL查询服务(文章底部附demo地址) GraphQL一种用为你 API 而生的查询语言.出自于Facebook,GraphQL非常易懂,直接看查询语句就能知道查询出来的数据是什么样的.本质上属于API Layer层,负责前端请求的合并.数据整理等功能. 查询示例 使用几个简单的例子看下GraphQL的查询是什么样子的. 普通查询 { me { name } } 查询出来的数据格式如下: { "me": { "name

基于typescript 强大的 nestjs 框架试用

nestjs 一个nodejs 的graphql 框架 安装 npm i -g @nestjs/cli 初始化项目 nest new dalong 运行demo 使用yarn yarn start 添加graphql支持 参考官方demo sample/12-graphql-apollo 运行 yarn && yarn start 效果 操作 添加 查询 源码结构说明 代码比较简单,写过angular 的基本都可以看懂,主要是依赖注入,服务定义,以及graphql schema 定义,gr

[GraphQL] Create an Input Object Type for Complex Mutations

When we have certain mutations that require more complex input parameters, we can leverage the Input Object Type in GraphQL. In this video, we’ll learn how to create an Input Object Type and how to add it to a GraphQL Mutation Type. const express = r

GraphQL入门指南

Introduction GraphQL is Facebook's new query language for fetching application data in a uniform way. GraphQL并不是一个面向图数据库的查询语言,而是一个数据抽象层,包括数据格式.数据关联.查询方式定义与实现等等一揽子的东西.GraphQL也并不是一个具体的后端编程框架,如果将REST看做适合于简单逻辑的查询标准,那么GraphQL可以做一个独立的抽象层,通过对于多个REST风格的简单的接口

[GraphQL] Write a GraphQL Schema in JavaScript

Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but as our application grows, or when we start using more complex types like interfaces or unions, we find that we can’t use a GraphQL Language file in t

Graphql介绍(Introduction to GraphQL)

Introduction to GraphQL GraphQL介绍 Learn about GraphQL, how it works, and how to use it in this series of articles. Looking for documentation on how to build a GraphQL service? There are libraries to help you implement GraphQL in many different langua

[GraphQL] Deploy a GraphQL dev playground with graphql-up

In this lesson we'll use a simple GraphQL IDL schema to deploy and explore a fully functional GraphQL service in minutes with graphql-up. Install: npm i -g graphql-up -g Create schema: type Person { id: ID!, name: String!, tasks: [Task!]! @relation(n

[GraphQL] Use GraphQLList with GraphQLObject Types

When working with collections of things in GraphQL, we'll always reach out for the GraphQLListType. In this video, we'll learn how to use GraphQLList from the graphql package in combination with a GraphQLObject Type to create a field that returns a c

搭建graphQL

首先需要node,去官网下载即可,然后新建一个文件夹. 我新建的文件夹为MD,然后cd MD,npm init初始化项目,然后下载三个必要的插件 npm install express --save npm install graphql --save npm install express-graphql --save 之后下载babel-core, npm install babel-core --save npm install babel-preset-es2015 --save 因为b