我试图在函数中调用一个简单的 grpc 请求,但我不知道如何等待响应并将其传递到 grpc 调用之外。
应该是这样的:
Client: (parent, args, context, info) =>{
client.Get({thing, objects, properties}, function(err, response) {
//how to return the response outside?
});
//await for the response function.
};
Run Code Online (Sandbox Code Playgroud)
是否可以在 client.Get 函数之外以某种方式返回响应?有人有一些建议吗?谢谢您的建议!
我知道我的数据是否会像
{
id: 123
address: xyz
name: hello
}
Run Code Online (Sandbox Code Playgroud)
我想在查询期间使用别名我可以这样做
query {
identification: id
address
full_name : name
}
Run Code Online (Sandbox Code Playgroud)
所以我的数据将如下所示:
{
identification: 123
address: xyz
full_name: hello
}
Run Code Online (Sandbox Code Playgroud)
但我希望即将发布的数据如下所示:
{
id : 123
info: {
address: xyz
name: hello
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 GraphQl Query 中实现这一点,我怎样才能给出别名info
我的组件实例中有一个查询:
@Component({
apollo: {
cartProducts: {
query: GET_CART_PRODUCTS,
loadingKey: "loading",
},
}
})
Run Code Online (Sandbox Code Playgroud)
它正在正确加载我的数据。然后我更改数据库中的数据并希望在单击时重新获取它们,这会调用该函数:
refresh() {
this.$apollo
.query({
query: GET_CART_PRODUCTS,
})
.then((res: any) => (this.cartProducts = res.data.cartProducts))
.catch((err: any) => console.log(err));
}
Run Code Online (Sandbox Code Playgroud)
但它不会更新cartProducts属性,也不会给我任何错误。虽然如果我刷新页面,就会有新数据。在我的 Apollo 配置中,我有这样的属性fetchPolicy: "network-only"。这种行为的原因可能是什么?提前致谢!
我有一个要求,需要根据输入使用两个运算符(_and 和 _or)。
那么是否可以在 hasura graphql 中将运算符作为变量/参数传递?
我正在传递“match”变量,要求是我应该能够根据一些点击传递“_or”或“_and”,如果可能的话,请写下运算符的“类型”。
query Search($match: String) {
restaurants(where: {_or: [{cuisine: {_ilike: $match}}, {name: {_ilike: $match}}]}) {
cuisine
id
name
reviews {
body
}
}
}
#variable
{
"match":"%woodland%"
}
Run Code Online (Sandbox Code Playgroud) 假设我有以下 GraphQL 类型:
type User {
id: String!
posts: [Post!]!
}
type Post {
id: String!
text: String,
}
Run Code Online (Sandbox Code Playgroud)
这是返回更新后的帖子的突变:
mutation addNewPost(
$userId: String!
$text: String!
) {
addNewPost(userId: $userId, text: $text) {
id
text
}
}
Run Code Online (Sandbox Code Playgroud)
运行此突变后,我的缓存包含帖子的新条目。如何将其添加到用户的帖子数组中?我尝试过cache.writeQuery和cache.modify但我无法弄清楚。
我想通过 GrahQL 和 React 保存一个大表单。我将所有表单值都放在一个变量名中:formValue 有没有办法将“formValue”传递给这样的查询?
const [toSaveUserProfile] = useMutation(SAVE_USER_PROFILE)
toSaveUserProfile({ variables: formValue })
const SAVE_USER_PROFILE = gql`
mutation saveUserProfile($profileId: String!) {
updateProfile(profile: { formValue }) {
id
}
}
`
Run Code Online (Sandbox Code Playgroud)
或者我应该一一传递所有字段并像这样定义它们的类型?
const [toSaveUserProfile] = useMutation(SAVE_USER_PROFILE)
toSaveUserProfile({ variables: formValue })
const SAVE_USER_PROFILE = gql`
mutation saveUserProfile($id: String!, $firstName: String, $lastName: String, $age: Int, ......) {
updateProfile(profile: { id: $id, firstName: $firstName, lastName: $lastName, age:$age, ......}) {
id
}
}
`
Run Code Online (Sandbox Code Playgroud)
模式看起来像这样
updateProfile(profile: ProfileDTOInput!): ProfileDTO
type ProfileDTO {
address: String …Run Code Online (Sandbox Code Playgroud) 我正在关注有关如何将 GraphQl 设置到我的项目中的 Lighthouse 官方文档。不幸的是,我遇到了以下错误:
No directive found for `bcrypt`
到目前为止我创建的架构如下:
type Query {
users: [User!]! @paginate(defaultCount: 10)
user(id: ID @eq): User @find
}
type User {
id: ID!
name: String!
email: String!
created_at: DateTime!
updated_at: DateTime!
}
type Mutation {
createUser(
name: String!,
email: String! @rules(apply: ["email", "unique:users"])
password: String! @bcrypt
): User @create
}
Run Code Online (Sandbox Code Playgroud)
我尝试执行的查询如下:
mutation {
createUser(
name:"John Doe"
email:"john@test.com"
password: "somesupersecret"
) {
id
email
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Strapi和graphQL,
通过这个查询我可以获得帖子
query {
posts{
id,
title
}
}
Run Code Online (Sandbox Code Playgroud)
我想将帖子和总计数放在 一起,理想的结果是这样的
{
"data": {
"totalCount" : "3",
"posts": [
{
"id": "1",
"title": "first post "
},
{
"id": "4",
"title": "post two"
},
{
"id": "5",
"title": "post 3"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我已阅读文档,我可以查询以获得总数,但这对我来说还不够!那么,我怎样才能将帖子和总计数放在一起呢?
我想向 NestJS 框架(v8)中的所有响应添加具有值的自定义标头。我认为正确的方法是使用全局拦截器,但我不知道该怎么做。
我正在添加我的拦截器:
app.useGlobalInterceptors(new HeadersInterceptor());
Run Code Online (Sandbox Code Playgroud)
我发现了多种方法,但没有一个有效。最常见的看起来像:
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable, tap } from 'rxjs';
@Injectable()
export class HeadersInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
tap(() => {
const res = context.switchToHttp().getResponse();
res.setHeader('my-global-header', 'important-value');
}),
);
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
res.setHeader is not a function
Run Code Online (Sandbox Code Playgroud)
编辑:从正确的答案来看,我应该提到我也在使用 GraphQL。
graphql ×10
apollo ×3
typescript ×2
async-await ×1
contentful ×1
gatsby ×1
graphql-js ×1
grpc ×1
hasura ×1
javascript ×1
laravel ×1
nestjs ×1
node.js ×1
nuxt-strapi ×1
reactjs ×1
rpc ×1
strapi ×1
vue-apollo ×1
vue.js ×1