所以就在昨天,我开始学习 graphql,它真的很有趣,而且实际上很容易学习和理解。我开始阅读一些文章,发现了 N+1 问题。我在这里找到了这个例子
询问
# getting the top 100 reviews
{
top100Reviews {
body
author {
name
}
}
}
Run Code Online (Sandbox Code Playgroud)
架构
const typeDefs = gql`
type User {
id: ID!
name: String
}
type Review {
id: ID!
body: String
author: User
product: Product
}
type Query {
top100Reviews: [Review]
}
`;
Run Code Online (Sandbox Code Playgroud)
最后是解析器
const resolver = {
Query: {
top100Reviews: () => get100Reviews(),
},
Review: {
author: (review) => getUser(review.authorId),
},
};
Run Code Online (Sandbox Code Playgroud)
在这篇文章中他说
当我们执行以下查询以获取前 100 条评论和相应的作者姓名时,我们首先调用从数据库中检索 100 条评论记录,然后对于每条评论,我们再次调用数据库以获取用户详细信息给定作者 ID。 …
我目前正在使用单独的.graphql文件加载 GraphQL 模式,但它封装在字符串中:
schema.graphql
const schema = `
type CourseType {
_id: String!
name: String!
}
type Query {
courseType(_id: String): CourseType
courseTypes: [CourseType]!
}
`
module.exports = schema
Run Code Online (Sandbox Code Playgroud)
然后将其用于apollo-server:
index.js
const schema = `
type CourseType {
_id: String!
name: String!
}
type Query {
courseType(_id: String): CourseType
courseTypes: [CourseType]!
}
`
module.exports = schema
Run Code Online (Sandbox Code Playgroud)
有没有办法简单地加载一个看起来像这样的 .graphql ?
schema.graphql
type CourseType {
_id: String!
name: String!
}
type Query {
courseType(_id: String): CourseType
courseTypes: [CourseType]! …Run Code Online (Sandbox Code Playgroud) 我正在尝试用我的Apollo Server等待流的结果.我的解析器看起来像这样.
async currentSubs() {
try {
const stream = gateway.subscription.search(search => {
search.status().is(braintree.Subscription.Status.Active);
});
const data = await stream.pipe(new CollectObjects()).collect();
return data;
} catch (e) {
console.log(e);
throw new Meteor.Error('issue', e.message);
}
},
Run Code Online (Sandbox Code Playgroud)
当返回的数据流很小时,这个解析器工作正常,但是当进入的数据较大时,我得到了一个503 (Service Unavailable).我看起来超时发生在30秒左右.我已经尝试过增加我的Express服务器的超时graphQLServer.timeout = 240000;但这并没有什么不同.
我怎样才能解决这个问题?30秒超时来自何处?只有在结果需要更长时间时才会失败.
我正在使用https://github.com/mrdaniellewis/node-stream-collect从流中收集结果.
来自try catch的错误:
I20180128-13:09:26.872(-7)? { proxy:
I20180128-13:09:26.872(-7)? { error: 'Post http://127.0.0.1:26474/graphql: net/http: request canceled (Client.Timeout exceeded while awaiting headers)',
I20180128-13:09:26.872(-7)? level: 'error',
I20180128-13:09:26.873(-7)? msg: 'Error sending request to origin.',
I20180128-13:09:26.873(-7)? time: '2018-01-28T13:09:26-07:00',
I20180128-13:09:26.873(-7)? url: …Run Code Online (Sandbox Code Playgroud) 测试useSubscription钩子我发现有点困难,因为该方法在Apollo 文档中被省略/未记录(在撰写本文时)。据推测,应该使用<MockedProvider />from来模拟它@apollo/react-testing,就像该链接中给出的示例中的突变一样。
测试我正在工作的订阅的加载状态:
成分:
const GET_RUNNING_DATA_SUBSCRIPTION = gql`
subscription OnLastPowerUpdate {
onLastPowerUpdate {
result1,
result2,
}
}
`;
const Dashboard: React.FC<RouteComponentProps & Props> = props => {
const userHasProduct = !!props.user.serialNumber;
const [startGetRunningData] = useMutation(START_GET_RUNNING_DATA);
const [stopGetRunningData] = useMutation(STOP_GET_RUNNING_DATA);
useEffect(() => {
startGetRunningData({
variables: { serialNumber: props.user.serialNumber },
});
return () => {
stopGetRunningData();
};
}, [startGetRunningData, stopGetRunningData, props]);
const SubscriptionData = (): any => {
const { data, loading } …Run Code Online (Sandbox Code Playgroud) 我正在使用带有打字稿的apollo 服务器,但在我的解析器中获取上下文参数以获取name属性context是字符串时遇到问题。现在它被输入为any,我希望它被输入为string. 我还看到context参数的类型为 any,当我希望它是特定接口时。反正有没有告诉上下文和它的属性我想要它是什么类型而不是它们都被输入为any?
const server = new ApolloServer({
typeDefs: gql`
type Query {
test: String
}
`,
resolvers: {
Query: {
test(parent: any, args: any, context, info: any) {
context.name // name is typed as "any" when I'd like it to be typed as "string"
}
}
},
context() {
return {
name: 'John Doe'
}
}
})
Run Code Online (Sandbox Code Playgroud)
我试图做这样的事情,但这会引发错误。
context<{ name: string }>() {
return …Run Code Online (Sandbox Code Playgroud) 类型脚本显示错误,未提及每个参数的参数类型:
Mutation: {
createUser: (parent, args, context, info) =>{
}
Run Code Online (Sandbox Code Playgroud)
我可以使用任何类型来解决,但是正确的类型是什么?
Mutation: {
createUser: (parent: any, args: any, context: any, info: any) =>{
}
Run Code Online (Sandbox Code Playgroud)
我最近一直在做一个项目,它有node.js + express + typescript + Apollo服务器堆栈.在研究Apollo客户端时,我偶然发现了TypeScript部分.但对于服务器而言,这一切都没有,这让我在这种情况下自由选择.
所以问题是:有没有关于使用打字稿实现Apollo graphql server的最佳实践,或者我应该至少避免什么?
我\xe2\x80\x99m 使用较新版本的 apollo 服务器 V4,我需要有关如何将图像或视频从前端上传到 cloudinary 并将 URL 保存到 MongoDB 数据库\xe2\x80\xa6 的帮助,请帮助
\n我已阅读 Apollo 文档,但那里没有答案 \xe2\x80\xa6 我\xe2\x80\x99m 现在有点卡住了
\n我正在使用Apollo Stack graphql-server-express和apollo-client.
因为我的后端不完美,所以可能会出现错误,因此我必须对该路径的错误请求做出响应.
直到现在我的主要问题是身份验证,因此我回复了一个错误.
return new Error(`${data.status}: ${data.statusText} @ ${data.url}`)
Run Code Online (Sandbox Code Playgroud)
在前端,我使用apollo-client查询数据.
return apollo
.query({query: gql`
query {
${query}
}`,
forceFetch: forceFetch
})
.then(result => { debugger; return result.data })
.catch(error => { debugger; console.error(error); });
Run Code Online (Sandbox Code Playgroud)
但是,如果查询的一个属性响应错误,则只调用catch函数.即使是剩余属性的数据也会被转移,我在Chrome开发工具的网络标签中看到了这一点.In不是catch函数中的错误对象.
我的尝试与GraphiQL一起使用,我在同一个对象中获取错误和数据.
那么如何在不丢失整个请求的情况下为属性抛出错误呢?
有人可以帮我这个,我的设置如下,在Apollo 2.0之前,我有一个server.js,其中我使用了express和graphql-server-express我有一个只有http的cookie会话,当用户登录时我设置了jwt令牌作为cookie,它在浏览器中设置为仅限http.在后续请求中,我验证浏览器传回的cookie.一切正常,我可以从任何其他解析器中的req.session.token访问令牌,并验证保存在cookie会话中的jwt令牌.
server.js
import express from 'express';
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express';
import { ApolloEngine } from 'apollo-engine';
import bodyParser from 'body-parser';
import cors from 'cors';
import cookieSession from 'cookie-session';
import schema from './schema/';
?
const server = express();
?
server.use(
cookieSession({
name: 'session',
keys: 'k1,k2',
maxAge: 30 * 60 * 1000,
domain: '.mydomain.com',
path: '/',
}),
);
?
const corsOptions = {
origin: 'http://local.mydomain.com:3000',
credentials: true,
methods: ['GET', 'PUT', 'POST', 'OPTIONS'],
};
?
server.use(cors(corsOptions));
?
server.use(
'/graphql', …Run Code Online (Sandbox Code Playgroud) apollo-server ×10
graphql ×8
apollo ×4
node.js ×3
typescript ×3
express ×2
apollostack ×1
braintree ×1
cloudinary ×1
nodes ×1
react-hooks ×1
reactjs ×1
subscription ×1