我正在阅读Apollo 服务器端缓存的文档,但没有看到任何有关缓存通常如何设置键控的内容。
我需要的是一个以响应中包含的对象 ID 为关键的缓存,而不是以从查询中获取的内容为关键。
例如,假设下面的 Person 对象由 ID 字段唯一标识,并且 hasShortHair 字段的计算成本很高,但很少发生变化。
type Person {
id: String!
hasShortHair: Boolean!
}
Run Code Online (Sandbox Code Playgroud)
假设有 2 种不同的查询类型可以返回 Person:
getPerson(id: String!): Person!
getAllPeople: [Person!]!
Run Code Online (Sandbox Code Playgroud)
理想情况下,对于具有给定 ID 的人,如果最近通过 getPerson 或 getAllPeople 获取该人,那么我想缓存为该人计算的 hasShortHair 值,并将该缓存用于getPerson和 getAllPeople返回该人的查询。
像下面这样的设置可以达到这个目的吗?(基于文档中的书籍示例)
type Person @key(fields: "id") @cacheControl(maxAge: 30) {
id: String!
hasShortHair: Boolean!
}
Run Code Online (Sandbox Code Playgroud)
或者对此的缓存仍然是按请求键控的吗?
我正在使用 apollo-server-lambda、Prisma ORM 和 graphql-codegen。
我有getBookById一个返回Book. Book包含一个名为 的枚举BookStatus。我希望能够在操场上以 ENUM 形式返回,但出现错误:
无法为不可为 null 的字段 Book.bookStatus 返回 null。
图书状态 - TypeDef
enum BookStatus {
OPEN
DRAFT
CLOSED
}
Run Code Online (Sandbox Code Playgroud)
书籍 - TypeDef
type Book {
id: ID!
title: String!
bookStatus: BookStatus!
}
Run Code Online (Sandbox Code Playgroud)
getBookById - TypeDef
type Query {
getBookById(getBookByIdInput: GetBookByIdInput): Book
}
Run Code Online (Sandbox Code Playgroud)
使用带有订阅的乐观UI是否有意义?
所以基本上:
addChannelMutation({
variables: { name: eventValue },
optimisticResponse: {
__typename: "Mutation",
addChannel: {
__typename: "Channel",
id: data.channels.length,
name: eventValue
}
},
update: (store, { data: { addChannel } }) => {
// Read the data from our cache for this query.
const data = store.readQuery({ query: channelsListQuery });
// Add our comment from the mutation to the end.
data.channels.push(addChannel);
// Write our data back to the cache.
store.writeQuery({ query: channelsListQuery, data });
}
}).then(res => {});
Run Code Online (Sandbox Code Playgroud)
它将同一项添加两次,触发重复的密钥异常。因此,乐观的ui对于订阅是否有意义?
有人可以帮我使用scaphold.io进行第一个查询吗?
当我使用内部GraphiQL查询以下内容时:
query AllPrograms {
viewer {
allPrograms{
edges {
node {
id
name
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
回报看起来像这样:
{
"data": {
"viewer": {
"allPrograms": {
"edges": [
{
"node": {
"id": "UHJvZ3JhbTo2",
"name": "Blender"
}
},
{
"node": {
"id": "UHJvZ3JhbTo1",
"name": "Inkscape"
}
},
...
Run Code Online (Sandbox Code Playgroud)
我的组件看起来像这样:
<template>
<md-card>
<span class="md-headline">Programma's</span>
<span class="md-headline">{{ allPrograms }}</span>
</md-card>
</template>
<script>
import gql from 'graphql-tag'
import config from '../../config/client'
const log = console.log
const allPrograms = gql `
query AllPrograms { …Run Code Online (Sandbox Code Playgroud) 我想知道为什么人们似乎没有使用Redux的GraphQL jus.
我之前从未使用过GraphQL,但我想开始一个新项目,但Apollo和Relay都没有说服我.目前我正在创建一个使用react和redux以及"旧时尚"休息api的应用程序.我喜欢redux的想法,它在一个地方存储关于我的应用程序的整个信息.
而现在,据我所知,Apollo和relay都做了类似的事情,但他们使用单独的存储,在我们混合逻辑和视图甚至更多只是React,这些东西(另一个存储和混合代码)似乎是有点乱.优点是缓存,我是对的吗?
那么为什么我们不能像以前那样只使用普通的rest api发送查询并将数据放到redux存储中(可能会尝试存储有关同步的某些信息以进行优化).
对不起,如果有我错过的东西,我是新来的,我不是专业人士,这就是为什么我问一些人可能有更多的经验,我:)
示例:https://codesandbox.io/s/j4mo8qpmrw
文档:https://www.apollographql.com/docs/link/links/state.html#default
TLDR:这是一个待办事项列表,@ client查询参数不会过滤掉列表.
这是查询,将$ id作为参数
const GET_TODOS = gql`
query todos($id: Int!) {
todos(id: $id) @client {
id
text
}
}
`;
Run Code Online (Sandbox Code Playgroud)
查询在那里传递变量
<Query query={GET_TODOS} variables={{ id: 1 }}>
/* Code */
</Query>
Run Code Online (Sandbox Code Playgroud)
但默认解析器不使用该参数,您可以在上面的codesandbox.io示例中看到它.
文档说它应该有用,但我似乎无法弄清楚我错过了什么.提前致谢!
我们正在将graphql缓慢添加到我们的react项目中,并替换现有的redux集成。因此,我试图了解阿波罗中的缓存并看到了两件事。
我们有查询要在主页上调用应用程序列表,并且此应用程序列表将在其他页面上使用。所以我尝试的一个选项是在父容器中调用应用程序查询列表,并在子页面中使用client.readQuery,这样对graphql服务器的调用将仅在容器中发生,而在其他页面中它将发生从缓存中调用。但是我看到一些有关在类似情况下使用apollo-link-state的帖子。那么什么是最好的方法?什么时候使用apollo-cache-inmemory?什么时候使用apollo-link-state?
如何复制:
server.js
const { ApolloServer, makeExecutableSchema, gql } = require('apollo-server');
const typeDefs = gql`
type Mutation {
uploadAvatar(upload: Upload!): String!
}
`;
const resolvers = {
Mutation: {
uploadAvatar(root, args, context, info) {
return 'test';
}
}
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const server = new ApolloServer({
schema,
});
server.listen().then(({ url }) => {
console.log(` Server ready at ${url}`);
});
Run Code Online (Sandbox Code Playgroud)
package.json
"dependencies": {
"apollo-server": "^2.0.0-rc.6",
"graphql": "^0.13.2"
}
Run Code Online (Sandbox Code Playgroud)
在节点server.js上,我们收到以下错误:
键入在文档中找不到的“上传”。
给定最新版本的apollo服务器,我是否应该在查询中添加其他内容?根据本教程和我目前不记得的其他一些资料,除了编写Upload之外,不需要做任何其他事情,它应该可以正常工作。我有什么想念的吗?
我试图从一个突变中返回一个查询类型,在某些情况下我可以使它工作,但不能按我想要的方式工作。该问题与所使用的查询类型没有特别关系,因为我发现使用以外的其他类型具有相同的行为Query。
您可以在https://codesandbox.io/s/1z8kjy8m93上运行和修改此代码
服务器
const { ApolloServer, gql } = require("apollo-server");
const typeDefs = gql`
type Query {
hello(msg: String): String
}
type Mutation {
someMutation(someArg: String): MutationResponse
}
type MutationResponse {
query: Query
status: String
}
`;
const resolvers = {
Query: {
hello: (root, args, context) => {
console.log("hello: args = ", args);
return `${args.msg}, world !`;
}
},
Mutation: {
someMutation: (root, args, context) => {
console.log("someMutation: args = ", args);
return { status: …Run Code Online (Sandbox Code Playgroud) i have a problem with the apollo-server context. I wrote the following code:
const { ApolloServer } = require("apollo-server-azure-functions");
const { typeDefs, resolvers } = require('../graphql_schema/schema');
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => {
console.log(req);
return {
req: req
}
},
introspection: false,
playground: true,
});
module.exports = server.createHandler();
Run Code Online (Sandbox Code Playgroud)
But the req variable is always undefined. Does anybody have a idea?
I´m using following versions of apollo
"apollo-server": "^2.4.2",
"apollo-server-azure-functions": "^2.4.2",
Run Code Online (Sandbox Code Playgroud)
Thank you very much.
apollo ×10
graphql ×7
reactjs ×3
javascript ×2
react-apollo ×2
apollostack ×1
caching ×1
graphql-js ×1
prisma ×1
redux ×1
relayjs ×1
vue-apollo ×1
vue.js ×1