来自Facebook的官方消息是,Relay" 故意不了解身份验证机制".在中继存储库中的所有示例中,身份验证和访问控制是一个单独的问题.实际上,我还没有找到一种简单的方法来实现这种分离.
Relay存储库中提供的示例都具有根模式,其中的viewer字段假定有一个用户.该用户可以访问所有内容.
然而,实际上,应用程序具有许多用户,并且每个用户对每个节点具有不同的访问程度.
假设我在JavaScript中有这个架构:
export const Schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: () => ({
node: nodeField,
user: {
type: new GraphQLObjectType({
name: 'User',
args: {
// The `id` of the user being queried for
id: { type: new GraphQLNonNull(GraphQLID) },
// Identity the user who is querying
session: { type: new GraphQLInputObjectType({ ... }) },
},
resolve: (_, { id, session }) => {
// Given `session, get user with `id` …Run Code Online (Sandbox Code Playgroud) 我很难理解何时使用GraphQLInterfaceType和GraphQLUnionType.
我有RTFM:
任何人都可以提供一个真实世界的例子,当这些通过我的厚头有用时会有用吗?
我在看graphql.是否可以定义具有任意属性的对象?假设我有一些数据:
editOptions : { boxes : 3 , size : { width: 23,height:32} , color: #434343 }, etc...}
Run Code Online (Sandbox Code Playgroud)
这是在:
{ ... , box : { editOptions : {...} }, ... }
Run Code Online (Sandbox Code Playgroud)
假设editOptions永远不会具有相同的结构,有时对于获得颜色没有用,例如sakes.在mongoose中,可以将类型定义为:
editOptions:{}
这些editOptions通常对每个框都是唯一的.有些属性是共享的,但大多数都是唯一的.
所以我的问题是,有没有办法做到这一点?或者这是不好的做法,我应该改变我的模型.
谢谢.
我正在使用apollo-client库来查询来自我的Graphql服务器的数据.一些查询通过apollo轮询能力每隔5秒发送到服务器.
有一种通用的方法可以为从我的客户端轮询发送的所有请求添加自定义标头吗?
谢谢你的帮助 :)
我正在使用graphql-express创建一个端点,我可以在其中执行graphql查询.虽然我使用Sequelize和SQL数据库,但是直接在我的graphql resolve函数之外的服务器上使用它是错误的.如何从与其定义的同一服务器中查询我的graphql API?
这就是我设置graphql端点的方法:
const express = require('express');
const router = express.Router();
const graphqlHTTP = require('express-graphql');
const gqlOptions = {
schema: require('./schema')
};
router.use('/', graphqlHTTP(gqlOptions));
modules.exports = router;
Run Code Online (Sandbox Code Playgroud)
基本上我想要的是能够做这样的事情:
query(`
{
user(id: ${id}) {
name
}
}
`)
Run Code Online (Sandbox Code Playgroud)
我该如何创建这个query功能?
以下在graphiQL中正确执行
fragment BookGridFields on Book {
_id
title
}
{
allBooks {
...BookGridFields
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,可以在我的架构中指定片段,就在我的Book类型定义的下面,就像这样
type Book {
_id: String
title: String
pages: Int
weight: Float
authors: [Author]
}
fragment BookGridFields on Book {
_id
title
}
Run Code Online (Sandbox Code Playgroud)
所以我可以像这样运行查询
{
allBooks {
...BookGridFields
}
}
Run Code Online (Sandbox Code Playgroud)
无需将片段定义为我的查询的一部分.
目前上述错误有
未知片段\"BookGridFields \"
我试图获取当前用户,但在解析器中我未定义,在 jwt 策略中我使用令牌获取用户对象,但在解析器中用户未定义
授权卫士
import { ExecutionContext, Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { GqlExecutionContext } from '@nestjs/graphql';
import { AuthenticationError } from 'apollo-server-core';
import { ExecutionContextHost } from '@nestjs/core/helpers/execution-context-host';
@Injectable()
export class GqlAuthGuard extends AuthGuard('jwt') {
canActivate(context: ExecutionContext) {
const ctx = GqlExecutionContext.create(context);
const { req } = ctx.getContext();
return super.canActivate(
new ExecutionContextHost([req]),
);
}
handleRequest(err: any, user: any) {
if (err || !user) {
throw err || new AuthenticationError('GqlAuthGuard');
}
return user;
} …Run Code Online (Sandbox Code Playgroud) 我正在使用本教程为我的项目学习 GraphQL:https : //www.youtube.com/watch?v= ZQL7tL2S0oQ&ab_channel=WebDevSimplified
我得到错误:
TypeError: expressGraphQL is not a function
at Object.<anonymous>
Run Code Online (Sandbox Code Playgroud)
我已经尝试过:
现在的代码如下所示:
const express = require ('express')
const { expressGraphQL } = require('express-graphql')
const app = express();
app.use('/graphql', expressGraphQL({
graphiql: true,
})
)
app.listen(5000., () => console.log('Server Running'))
Run Code Online (Sandbox Code Playgroud)
如果我注释掉这一部分:
app.use('/graphql', expressGraphQL({
graphiql: true,
})
)
Run Code Online (Sandbox Code Playgroud)
无论使用 {} 括号还是不使用括号,代码都可以正常工作。
我有一些突变应该触发一些refetchQueries,但我需要这些查询有一个fetchPolicy而不是默认值.
有没有办法全局设置fetchPolicy而不是每个查询?因此,要避免在每个查询上设置fetchPolicy.
我是graphql的新手,我正在努力查询.我想通过他们的电子邮件地址返回用户
我有一个类型定义的调用V1User,它有以下字段id,email,password,role
在此查询中需要更改哪些内容才能根据电子邮件返回用户?
query GetAllV1User {
viewer {
allV1Users{
edges {
node {
id
email
role
createdAt
modifiedAt
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试过这个查询
query getV1UserQuery($email: String!) {
getV1User(email: $email) {
id
email
}
}
Run Code Online (Sandbox Code Playgroud)
有了这些参数
{"email": "test@test.com"}
Run Code Online (Sandbox Code Playgroud)
但是得到以下错误
{
"errors": [
{
"message": "Unknown argument \"email\" on field \"getV1User\" of type \"Query\".",
"locations": [
{
"line": 2,
"column": 13
}
],
"name": "GraphQLError"
},
{
"message": "Field \"getV1User\" argument \"id\" of type \"ID!\" is required but not provided.",
"locations": …Run Code Online (Sandbox Code Playgroud) graphql-js ×10
graphql ×9
javascript ×7
apollo ×1
jwt ×1
nestjs ×1
nodemon ×1
react-apollo ×1
relayjs ×1