我试图将文件上传到我的 apollo 服务器,但我无法上传任何文件,也不会引发任何错误,但我在解析器中只收到空对象,当我使用像Altair 这样的GraphQL客户端时,它可以正常工作
使用 Altair 客户端时在服务器中输出
{
filename: 'Copy of Massive Orange Summer Sale Animated-300x250px-MediumRectangle.png',
mimetype: 'image/png',
encoding: '7bit',
createReadStream: [Function: createReadStream]
}
Run Code Online (Sandbox Code Playgroud)
使用 @apollo/client 包时服务器中的输出
{}
Run Code Online (Sandbox Code Playgroud)
客户代码

服务器代码

我知道我们也可以使用 apollo 执行休息请求,但我不知道如何执行发布请求,有人可以帮助我吗?
我的 REST post 请求端点是:<url>/transaction/getbyhash
有效负载:
{"hash":"4e23f9e1d1729996de46fc94d28475b4614f101d72a98f221493f900dc33e0c2"}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我使用 apollo 客户端和 graphql-tag 编写相同的请求吗?
我正在尝试使用 Apollo Server 开发后端,我在浏览器中启用了 cors 并尝试cors: {origin: true}在服务器中添加配置。我已经在下面包含了服务器代码。
希望有人能够发现我搞砸了什么。
// index.js
const {ApolloServer} = require('apollo-server')
const typeDefs = require('./schema')
const resolvers = require('./resolvers')
const CourseAPI = require('./sources')
const apollo = new ApolloServer({
typeDefs,
resolvers,
cors: {
origin: true
},
dataSources: () => {
return {
courseAPI: new CourseAPI({
client: 'pg',
connection: {
host: 'ec2-79-125-30-28.eu-west-1.compute.amazonaws.com',
user: 'hgsfclebwdvtrz',
password:
'975260d79ace6b3f78132e6d51f8f56612e6cbc24c8d6063b9c6be60f5160453',
database: 'd7jasm9tcmpc30'
}
})
}
}
})
apollo.listen().then(({url}) => {
console.log(` Server ready at ${url}`)
})
Run Code Online (Sandbox Code Playgroud) 所以对于初学者来说,这是我的代码:
export const GET_AUTHORIZED_USER = gql`
query GetAuthorizedUser(
$includeReviews: Boolean!
$after: String
$first: Int
) {
authorizedUser {
id
username
reviews @include(if: $includeReviews) {
pageInfo {
hasPreviousPage
hasNextPage
startCursor
endCursor
}
edges {
cursor
node {
id
repositoryId
rating
text
createdAt
user {
username
}
}
}
}
}
}
`;
Run Code Online (Sandbox Code Playgroud)
reviews接受$after和$first参数,但我的问题是我不知道如何添加它们。如果我删除@includes指令并添加参数,它就可以正常工作。就我而言,我想要同时拥有指令和参数。
我怎样才能实现这个目标?我知道我可以进行单独的查询来获取评论,但我想使用该指令。
我是 Next.js 和 GraphQL 世界的新手。到目前为止,我一直在使用 React 与 reduxjs 和 axios 进行 API 调用。我会学习更高级的东西,所以这是我的问题。如果我使用 apollo graphql,我有哪些本地状态管理选项?它可以由 apollo 本身处理,还是我应该使用像 redux store 这样的外部工具?
我有一个查询返回下面的示例数据
{
__typename: 'Typehere',
name: 'type',
address: 'address type'
}
Run Code Online (Sandbox Code Playgroud)
当我使用useQueryfetchPolicy: 'cache-first' 选项时,GQL API 返回正确的数据。但是当我使用 console.log(response) 时,数据现在将转换为
{
__typename: 'Typehere'
}
Run Code Online (Sandbox Code Playgroud)
fetchPolicy 的 API 调用和响应:“缓存优先”

控制台.log(响应)

为什么会发生这种情况?
我尝试使用 fetchPolicy: 'no-cache' 获取数据并且没有问题,但是,我需要缓存它以进行优化。我还尝试了阿波罗客户端文档中所述的其他获取策略。
我正面临着以我想要的格式将数据导入组件的困难.
我的数据来自:
对于我的组件,它不是一种非常有用的格式.我希望数据被id键为suc:
assets: {
"2a4a34sdf3dd": {
//asset data
}
}
Run Code Online (Sandbox Code Playgroud)
我的查询是:
const query = gql`{
assets {
body
id
}
}`
Run Code Online (Sandbox Code Playgroud)
我这样连接我的组件:
export default compose(
graphql(query),
connect(mapStateToProps, mapDispatchToProps)
)(AssetList);
Run Code Online (Sandbox Code Playgroud)
有关如何格式化我的graphql服务器返回的商店数据的任何见解?
我从波纹管样品中使用,但添加库时出现错误:
贝娄是我的build.gradle (project):
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
maven { url "https://jitpack.io" }
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.apollographql.android:gradle-plugin:0.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
贝娄是我的build.gradle (Module):
apply plugin: …Run Code Online (Sandbox Code Playgroud) 更新:apollo已更新代码和文档,因此问题现在无关紧要
预期结果
使用https://github.com/apollographql/apollo-tutorial-kit中的以下代码段启动apollo-server-express .
import express from 'express';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import bodyParser from 'body-parser';
import schema from './data/schema';
const GRAPHQL_PORT = 3000;
const graphQLServer = express();
graphQLServer.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
graphQLServer.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));
graphQLServer.listen(GRAPHQL_PORT, () => console.log(`GraphiQL is now running on http://localhost:${GRAPHQL_PORT}/graphiql`));
Run Code Online (Sandbox Code Playgroud)
实际结果:
在apollo-server-express的任何实现中,使用docs,使用https://github.com/apollographql/apollo-tutorial-kit等.我一遍又一遍地收到以下堆栈跟踪:
Error: Must provide document
at invariant (~/node_modules/graphql/jsutils/invariant.js:18:11)
at Object.validate (~/node_modules/graphql/validation/validate.js:58:34)
at doRunQuery (~/node_modules/apollo-server-core/dist/runQuery.js:80:38)
at ~/node_modules/apollo-server-core/dist/runQuery.js:20:54
at <anonymous>
Run Code Online (Sandbox Code Playgroud)
如何重现问题:
git clone https://github.com/apollographql/apollo-tutorial-kit.git
cd apollo-tutorial-kit
yarn/npm …Run Code Online (Sandbox Code Playgroud) 如何在graphQL阿波罗语法中添加字段解析器?使用graphql语法,如果我的讲座有问题,我可以这样做:
const LectureType = new GraphQLObjectType({
name: 'LectureType',
fields: () => ({
id: { type: GraphQLID },
questions: {
type: new GraphQLList(QuestionType),
resolve: ({ _id }, args, { models }) => models.Lecture.findQuestions(_id),
},
}),
});
Run Code Online (Sandbox Code Playgroud)
使用apollo graphQL的等效语法是什么?我认为我可以使用此解析器进行此类型定义:
type QuestionType {
id: String,
}
type LectureType {
id: String,
questions: [QuestionType],
}
const getOne = async ({ args, context }) => {
const { lecture, question } = constructIds(args.id);
const oneLecture = await model.get({ type: process.env.lecture, id: lecture });
oneLecture.questions = …Run Code Online (Sandbox Code Playgroud) apollo ×10
graphql ×8
react-apollo ×5
javascript ×2
reactjs ×2
android ×1
graphql-java ×1
graphql-js ×1
node.js ×1
react-redux ×1
rest ×1
server ×1