我需要帮助解释一段代码。
我一直在尝试学习如何使用 jwt 对用户进行身份验证并返回令牌和刷新令牌。
这是一次有趣的旅程,我遇到了这个 存储库,其中用户添加了一个扩展/链函数,据我所知,它结合了多个 graphql 解析器:
// I don't understand this function
const createResolver = (resolver) => {
const baseResolver = resolver;
baseResolver.createResolver = (childResolver) => {
const newResolver = async (parent, args, context, info) => {
await resolver(parent, args, context, info);
return childResolver(parent, args, context, info);
};
return createResolver(newResolver);
};
return baseResolver;
};
export const requiresAuth = createResolver((parent, args, context) => {
if (!context.user || !context.user.id) {
throw new Error('Not authenticated');
}
});
export const requiresAdmin …Run Code Online (Sandbox Code Playgroud) 所以我按照阿波罗文档教程https://www.apollographql.com/docs/tutorial/introduction/为我的 server.js 和 schema.js 提供了这段代码
服务器.js
import apollo from "apollo-server-express";
const { ApolloServer } = apollo;
import express from "express";
import typeDefs from "./schema.js";
const app = express();
const server = new ApolloServer({
typeDefs,
playground: true,
});
server.applyMiddleware({ app });
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`operating on port ${PORT}`);
})
Run Code Online (Sandbox Code Playgroud)
架构.js
import pkg from "apollo-server-express"
const { gql } = pkg
// defining schema
const typeDefs = gql`
type Launch {
id: ID!
site: …Run Code Online (Sandbox Code Playgroud) 这是我的问题,在我的反应应用程序中,每当创建订单时,我想获得该订单的订阅,称为 orderNotification,
在订单解析器中设置:
Subscription: {
orderNotification: {
subscribe: (_, __, { pubsub }) => pubsub.asyncIterator(ORDER_NOTIFICATION)
}
}
Run Code Online (Sandbox Code Playgroud)
突变:
Mutation: {
async createOrder(_, { MakeOrderInput: { state, message, products, total } }, context) {
try {
const userAuth = isAuth(context);
const pubsub = context.pubsub;
const newOrder = new Order({
state,
username: userAuth.username,
user: userAuth.id,
createdAt: new Date().toISOString(),
total,
message,
products,
userAddress: userAuth.address,
});
const index = products.findIndex(x => x.cost === 0);
if (index != -1) {
const u = await User.findById({ …Run Code Online (Sandbox Code Playgroud) 尝试使用 Prisma 检查 Postgres 表中是否存在记录,但似乎我只能查询 id 字段,而不能查询任何其他字段,如name和location,这会产生编译器错误
模型schema.prisma
model place {
id Int @id @default(dbgenerated("nextval('place_id_seq'::regclass)"))
name String
location String @unique
}
Run Code Online (Sandbox Code Playgroud)
生成类型
export type Place = {
__typename?: 'Place';
name?: Maybe<Scalars['String']>;
location?: Maybe<Scalars['String']>;
};
Run Code Online (Sandbox Code Playgroud)
查询解析器
let findPlace = await prisma.place.findUnique(
{
where: {
name: "abc"
}
}
)
Run Code Online (Sandbox Code Playgroud)
错误
Type '{ name: string; }' is not assignable to type 'placeWhereUniqueInput'.
Object literal may only specify known properties, and 'name' does not exist in type 'placeWhereUniqueInput'.ts(2322) …Run Code Online (Sandbox Code Playgroud) 我升级了 Nestjs 服务器包并尝试启动我的服务器。这次我得到了以下错误:
`TypeError: (0 , apollo_server_core_1.ApolloServerPluginLandingPageGraphQLPlayground) is not a function`
Run Code Online (Sandbox Code Playgroud)
我疑惑了...
所以,我正在尝试设计我的Apollo服务器.我想用对象作为参数创建一个变异.
这是我的架构的片段,它以某种方式导致了这个问题:
我认为它在语法上是正确的,但我遇到了这个错误:
{"errors":[{"message":"预期输入类型."}]}
我正在为GraphQL API模块化我的架构,并尝试在不使用任何第三方库的情况下合并解析器。
有没有简单的方法可以做到这一点Lodash.merge()?
Apollo文档说要使用Lodash之类的库来merge()模块化解析器。(http://dev.apollodata.com/tools/graphql-tools/generate-schema.html#modularizing)
问题似乎在于,从本质上讲,解析器包含作为属性的函数,因此当我通过Object.assign()或什至访问解析器时,它们似乎被省略了JSON.stringify()。
如果我console.log他们,我看到: {"Query":{},"Mutation":{}}
以下是解析器之一的外观:
const productResolvers = {
Query: {
myProducts: (root, { userId }, context) => {
return [
{ id: 1, amount: 100, expiry: '12625383984343', created: '12625383984343' },
{ id: 2, amount: 200, expiry: '12561351347311', created: '12625383984343' },
{ id: 3, amount: 200, expiry: '11346347378333', created: '12625383984343' },
{ id: 4, amount: 350, expiry: '23456234523453', created: '12625383984343' },
];
},
},
Mutation: {
addProduct: …Run Code Online (Sandbox Code Playgroud) 我想知道是否有可能使用Apollo Server在服务器端本地执行变异或查询。
例:
1-达到端点GET / createNew
2-我定义了一个突变,例如:
apolloserver.mutation(mutation_query).then((response)=>{res.status(200)})
Run Code Online (Sandbox Code Playgroud)
3-将新条目添加到数据库,并将JSON字符串返回给客户端
是否存在类似的东西?是一个apolloserver对象在运行时可用?
我正在使用NodeJS + Express
我不断收到“无法为不可为空的字段Airline.id返回null”。当FlightSchedule.operatingAirline为null(根据架构完全有效)并且客户端查询FlightSchedule.operatingAirline.id时发生错误。如何解决这个问题?将Airline.id,Airline.code和Airline.name设置为可为空可解决此问题,但这不是解决此问题的正确方法,因为如果存在Airline,这3个字段也将始终存在。以下是我的架构:
type Airline {
id: String!,
code: String!,
name: String!
}
type FlightSchedule {
airline: Airline!
operatingAirline: Airline
}
Run Code Online (Sandbox Code Playgroud)
以下是我的查询:
getFlightSchedules {
airline
{
id
code
name
}
operatingAirline
{
id
code
name
}
}
Run Code Online (Sandbox Code Playgroud) 使用纯graphql实现,我们可以从字符串运行查询,如下所示:
var { graphql, buildSchema } = require('graphql');
var schema = buildSchema(`
type Query {
hello: String
}
`);
var root = { hello: () => 'Hello world!' };
graphql(schema, '{ hello }', root).then((response) => {
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
但是在ApolloServer中找不到相同的方法:
const server = new ApolloServer({ typeDefs, resolvers });
// something like this
server.runQuery('{ hello }');
Run Code Online (Sandbox Code Playgroud) apollo-server ×10
graphql ×9
node.js ×3
express ×2
apollo ×1
apollostack ×1
javascript ×1
prisma ×1
reactjs ×1
resolver ×1
schema ×1