apollo/server express,mongoose,resolver chains

2024-04-30 18:44

本文主要是介绍apollo/server express,mongoose,resolver chains,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

apollo/server Get started with Apollo Server - Apollo GraphQL Docs

mongoose Mongoose 5.0 中文文档

index2.ts

import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import express from 'express';
import http from 'http';
import cors from 'cors';
import { typeDefs, resolvers } from './index2-schema';
import { users } from "./index2-mongoose";const app = express();const httpServer = http.createServer(app);const server = new ApolloServer<{ token?: String }>({typeDefs,resolvers,plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});server.start().then(() => {app.use('/graphql',cors<cors.CorsRequest>(),express.json(),expressMiddleware(server, {context: async ({ req }) => ({token: req.headers.token,msg: "会传递到所有的resolver context参数中",dataSources: { users }}),}),);httpServer.listen(4000, () => {console.log(`🚀 Server ready at http://localhost:4000/graphql`);});
});

index2-schema.ts

import { users } from "./index2-mongoose";export let typeDefs = `#graphql#1type Event {name: String!date: String!location: Location}type Location {name: String!weather: WeatherInfo}type WeatherInfo {temperature: Floatdescription: String}#2type Library {branch: String!books: [Book]}type Book {title: Stringauthor: Author}type Author {name: String!}#3type User{_id: ID!name: String!age: Int!sex: String!}type Query {books: [Book]users: [User]user(id: ID!): UserupcomingEvents: [Event!]!libraries: [Library]Library: Library}
`;export const resolvers = {Query: {books() {return [{ id: '1', title: "t1", },{ id: '2', title: "t2", },]},async users(parent: any, args: any, context: { dataSources: { users: { find: () => any; }; }; }) {let value = await users.find();console.log(await context.dataSources.users.find());return value;},async user(parent: any, { id }: any, context: { dataSources: { users: { findById: (arg0: any) => any; }; }; }) {console.log(await context.dataSources.users.findById(id));const user = await users.findById(id)return user;},upcomingEvents() {return [{name: "01",date: "2024-1-1",location: {name: "sunday",weather: {temperature: 18.5,description: 'cold'}}}];},libraries() {return [{ id: "1", branch: 'downtown' },{ id: "2", branch: 'riverside' },];}},Library: {books(parent: any) {// parent上一步libraries解析的结果console.log('books parent =', parent);let books = [{ id: '1', belongto: "1", title: "t1", },{ id: '2', belongto: "1", title: "t2", },{ id: '3', belongto: "1", title: "t2", },{ id: '4', belongto: "2", title: "t2", },{ id: '5', belongto: "2", title: "t2", },]return books.filter(item => item.belongto === parent.id);}},Book: {author(parent: any, args: any, context: any, info: any) {// 上一步books的解析结果console.log(parent);console.log(context);let authors = [{ id: '1', bid: '1', name: "Tom" },{ id: '2', bid: '2', name: "Sam" },{ id: '3', bid: '3', name: "Jack" },{ id: '4', bid: '4', name: "Lucy" },{ id: '5', bid: '5', name: "David" },]return authors.find(item => item.bid === parent.id);}}
};/*
// 查询案例
query ExampleQuery {books {titleauthor{name}}user(id: "63ca47df3dd42947e4cc021b") {_idnameage}upcomingEvents{name}libraries {branchbooks{titleauthor {name}}}
}
*/

index2-mongoose.ts 

import mongoose, { Mongoose } from 'mongoose';mongoose.set("strictQuery", true);mongoose.connect('mongodb://localhost:27017/test');export var db = mongoose.connection;db.on('error', console.error.bind(console, 'connection error:'));db.once('open', function () {console.log('mongodb connection success!');
});function UserModel(mongoose: Mongoose) {let user = new mongoose.Schema({name: { type: String, required: true },password: { type: String, required: true },age: { type: Number, require: true },sex: { type: String, require: true },phone: { type: String, require: true },email: { type: String, required: true },image: { type: String, default: null },myChannel: { type: String, default: null },describle: { type: String, default: null },subscribeCount: { type: Number, default: 0 },});return mongoose.model("User", user);
}export const users = UserModel(mongoose);

这篇关于apollo/server express,mongoose,resolver chains的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/949636

相关文章

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Linux中修改Apache HTTP Server(httpd)默认端口的完整指南

《Linux中修改ApacheHTTPServer(httpd)默认端口的完整指南》ApacheHTTPServer(简称httpd)是Linux系统中最常用的Web服务器之一,本文将详细介绍如何... 目录一、修改 httpd 默认端口的步骤1. 查找 httpd 配置文件路径2. 编辑配置文件3. 保存

Windows Server 2025 搭建NPS-Radius服务器的步骤

《WindowsServer2025搭建NPS-Radius服务器的步骤》本文主要介绍了通过微软的NPS角色实现一个Radius服务器,身份验证和证书使用微软ADCS、ADDS,具有一定的参考价... 目录简介示意图什么是 802.1X?核心作用802.1X的组成角色工作流程简述802.1X常见应用802.