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查询数据库中有多少个表 sql server 数表:select count(1) from sysobjects where xtype='U'数视图:select count(1) from sysobjects where xtype='V'数存储过程select count(1) from sysobjects where xtype='P' SE

SQL Server中,always on服务器的相关操作

在SQL Server中,建立了always on服务,可用于数据库的同步备份,当数据库出现问题后,always on服务会自动切换主从服务器。 例如192.168.1.10为主服务器,12为从服务器,当主服务器出现问题后,always on自动将主服务器切换为12,保证数据库正常访问。 对于always on服务器有如下操作: 1、切换主从服务器:假如需要手动切换主从服务器时(如果两个服务

SQL Server中,isnull()函数以及null的用法

SQL Serve中的isnull()函数:          isnull(value1,value2)         1、value1与value2的数据类型必须一致。         2、如果value1的值不为null,结果返回value1。         3、如果value1为null,结果返回vaule2的值。vaule2是你设定的值。        如

SQL Server中,添加数据库到AlwaysOn高可用性组条件

1、将数据添加到AlwaysOn高可用性组,需要满足以下条件: 2、更多具体AlwaysOn设置,参考:https://msdn.microsoft.com/zh-cn/library/windows/apps/ff878487(v=sql.120).aspx 注:上述资源来自MSDN。

SQL Server中,用Restore DataBase把数据库还原到指定的路径

restore database 数据库名 from disk='备份文件路径' with move '数据库文件名' to '数据库文件放置路径', move '日志文件名' to '日志文件存放置路径' Go 如: restore database EaseWe from disk='H:\EaseWe.bak' with move 'Ease

BD错误集锦6——【IDEA报错】tomcat server功能无效,报错Java EE: EJB, JPA, Servlets

在网上查找原因,发现是非法关闭IDEA导致的。 Open Settings | Plugns and enable it. 在设置中enable JAVA EE和tomcat server即可。 参考: https://stackoverflow.com/questions/43607642/intellij-idea-plugin-errorproblems-found-loadin

Server:sffe是什么?

发现的一个很好玩的问题。刚刚看cookie相关的东西,顺便想 看下自己项目里面的  cookie请求的头,然后突然发下图的内容。 正常的话,我本地自己配置的为Nginx服务器,他的server应该是   nginx的。 可是却显示。sffe、很奇怪。。。刚刚开始以为,自己是不是哪里配错了。。。 结果很简单………………因为请求的链接是 Request URL: http://f

博通5720 windows server 2003 32位网卡驱动和系统

driver for DELL R320 Broadcom 5720 Windows 2003 32bit 本人安装windows server 2003 网卡驱动成功!! 提供方便网盘下载地址博通5720网卡驱动 : http://pan.baidu.com/s/1GQWpw  windows server 2003 ghost 系统: http://pan

配置SpringCloud Config Client连上Config Server

SpringCloud Config Client实际上就是连接到Config Server的普通应用,前面一篇文章 SpringCloud Config Server搭建 已经介绍了如何搭建一个Config Server,本文介绍如何让一个Spring应用连上Config Server并使用Config Server上的配置信息。 创建一个Spring应用 不知道为什么,在https://s

SpringCloud Config Server搭建

文章目录 1 创建Git仓库2 创建应用配置文件3 将应用配置文件commit到Git仓库中4 创建Config Server应用5 配置运行Config Server6 访问Config Server SpringCloud Config Server的作用实际上是充当Spring应用和存放在Git中的配置文件之间的媒介,为其他应用提供一个统一的配置管理中心,架构图如下: