标签: express-graphql

使用graphql时出现意外的<EOF>

每次在同一行上都出现EOF错误,多次更改代码,甚至降级到以前的版本,graphql但没有任何积极结果。我的代码是:

const graphql = require('graphql')
const _ = require('lodash')
 const {
     GraphQLObjectType,
     GraphQLString,
     GraphQLInt,
     GraphQLSchema
 } = graphql

const users = [
    {id: '1', firstName: 'Ansh', age: 20},
    {id: '2', firstName: 'Ram', age: 21},
    {id: '3', firstName: 'Sham', age: 20}
]

 const UserType = new GraphQLObjectType({
     name: 'User',
     fields: {
        id: {type: GraphQLString},
        firstName: {type: GraphQLString},
        age: {type: GraphQLInt}
     }
 })

 const RootQuery = new GraphQLObjectType({
     name: 'RootQueryType',
          fields: {
             user: {
                 type: UserType,
                 args: {id: {type: GraphQLString}}, …
Run Code Online (Sandbox Code Playgroud)

graphql-js express-graphql

7
推荐指数
1
解决办法
4172
查看次数

如何缓存到express-graphql?

在我的 nodejs 应用程序中,我将 graphql 与express-graphql 一起使用

我看不到库提供的任何缓存选项。我应该如何为 graphql 请求做缓存?(我需要支持getpost

node.js express graphql express-graphql

7
推荐指数
0
解决办法
295
查看次数

拦截 Apollo/graphql 请求

我们在应用程序中使用 Apollo/Graphql,但遇到了一个问题。

该应用程序是在 Angular 5 中开发的,NodeJS 作为后端,graphql 服务器将响应前端。为了以 Angular 拦截 Http 请求,我们实现了“HttpInterceptor”,以便我们可以以自定义的方式处理错误。但是,Apollo 请求不会通过这个拦截器。

为了正确处理错误,我们使用了“apollo-link-error”。

但是我们想知道是否有可能以类似于 HttpInterceptor 的方式拦截 apollo 请求。

error-handling apollo apollo-client angular express-graphql

6
推荐指数
2
解决办法
8453
查看次数

如何使用 GraphQLError 自定义消息传递?

我正在尝试使用 GraphQLError 自定义消息传递。

我想用 GraphQL 错误处理的用例很少:

  • 当用户名和密码不匹配时,我想返回自定义用户名和密码不匹配的消息。

  • 当用户输入无效的电子邮件时,我想返回自定义输入的电子邮件无效的消息。

  • 很少有其他用例。

我创建了一个 ValidateError.js 文件来使用 GraphQLError 处理函数:

const { GraphQLError } = require('graphql');

module.exports  = class ValidationError extends GraphQLError {
  constructor(errors) {

    super('The request is invalid');

    var err = errors.reduce((result, error) => {

        if (Object.prototype.hasOwnProperty.call(result, error.key)) {
          result[error.key].push(error.message);
        } else {
          result[error.key] = [error.message];
        }

        return result;
      }, {});
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的应用程序索引文件 app.js 的代码:

app.use('/graphql', graphqlExpress(req => ({
  schema,
  context: {
    user: req.user
  },
  formatError(err) {
    return {
      message: err.message,
      code: err.originalError && …
Run Code Online (Sandbox Code Playgroud)

graphql express-graphql

6
推荐指数
1
解决办法
6574
查看次数

如何在没有_id参考的情况下在GraphqQL列表中获取模型的填充值?

我有一个带有某些字段的用户模型,并且引用了“技能”模型。

这些模型如下所示

1.Users.js(用户模型)

 const mongoose = require('mongoose');
 const Schema = mongoose.Schema;

 const UsersSchema = new Scheam({
     name: { type : String },
     email: { type : String },
     address: { type : String },

     Skills: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Skills' }],
 })

 module.exports = mongoose.model('Users', UsersSchema);
Run Code Online (Sandbox Code Playgroud)

2.技能模型

const mongoose = require('mongoose')
const Schema = mongoose.Schema;

const SkillsSchema = new Schema({
   name : { type: String },
});

module.exports = mongoose.model('Skills', SkillsSchema);
Run Code Online (Sandbox Code Playgroud)

我正在使用技能选择下拉菜单创建新用户,该下拉菜单存储用户模型中技能的ID。

通过在node js应用程序中填充Users模型来获取技能。

  export function getUser(req, res) {
     console.log('getUser'); …
Run Code Online (Sandbox Code Playgroud)

mongodb node.js graphql mern express-graphql

6
推荐指数
0
解决办法
67
查看次数

为 GraphQL 中的所有查询添加架构指令

  1. 有没有办法通过覆盖所有查询和突变SchemaDirectiveVisitor 方法之一来添加模式指令?例如,检查指令中的身份验证令牌。最好不要为每个定义的查询/突变添加一次。
  2. 如果是,应覆盖以下哪一种方法以及如何覆盖?我无法找到有关如何覆盖每个示例的示例。

    • visitSchema(schema: GraphQLSchema)
    • visitScalar(scalar: GraphQLScalarType)
    • visitObject(object: GraphQLObjectType)
    • visitFieldDefinition(field: GraphQLField<any, any>)
    • visitArgumentDefinition(argument: GraphQLArgument)
    • visitInterface(iface: GraphQLInterfaceType)
    • visitUnion(union: GraphQLUnionType)
    • visitEnum(type: GraphQLEnumType)
    • visitEnumValue(value: GraphQLEnumValue)
    • visitInputObject(object: GraphQLInputObjectType)
    • visitInputFieldDefinition(field: GraphQLInputField)

我的直觉会说,visitObject(object: GraphQLObjectType)因为type Query是一个GraphQLObjectType.

  1. DirectiveLocation最终会是什么?对象或查询/突变?

apollo graphql graphql-js apollo-server express-graphql

6
推荐指数
1
解决办法
1297
查看次数

在 graphqlHTTP.Middleware 之后挂载中间件

我有一个关于express-graphql.\nI\xe2\x80\x99m 的问题,试图在 GraphQL 解析器之后运行中间件。

\n\n

这是我的前两次尝试:

\n\n
app.use('/api', graphqlHTTP({\n    schema: graphqlSchema\n  })\n);\n\napp.use((req, res, next) => {\n  console.log('middleware called');\n  next();\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

\n\n
app.use('/api', graphqlHTTP({\n    schema: graphqlSchema,\n    graphiql: true,\n  }), () => {\n      console.log('middleware called');\n  }\n);\n
Run Code Online (Sandbox Code Playgroud)\n\n

两者都\xe2\x80\x99t 工作。\n我猜express-graphql不是next()在某个地方打电话。

\n\n

消息来源似乎证实了这一点:

\n\n
type Middleware = (request: Request, response: Response) => void;\n
Run Code Online (Sandbox Code Playgroud)\n\n

next不是参数。

\n\n

我尝试了这个解决方法:

\n\n
app.use( (req, res, next) => {\n  req.on('end', () => {\n    console.log('middleware called');\n  });\n  next();\n});\n\napp.use('/api', graphqlHTTP({\n    schema: graphqlSchema\n  })\n);\n …
Run Code Online (Sandbox Code Playgroud)

middleware express graphql graphql-js express-graphql

5
推荐指数
0
解决办法
438
查看次数

Safari 中“不允许请求资源”,Firefox 中“阻止加载混合活动内容”。Chrome 中的完美功能

我正在开发一个使用 React 前端和 Express 后端的应用程序,并通过 Apollo 设置 GraphQL (我正在关注并修改教程https://www.youtube.com/playlist?list=PLN3n1USn4xlkdRlq3VZ1sT6SGW0-yajjL

我目前正在尝试部署,并且正在使用 Heroku 进行部署。在部署之前,一切都在我的本地计算机上以及 Google Chrome 中的 Heroku 上完美运行。但是,我在 Safari 和 Firefox 中分别遇到了上述错误。想知道为什么这些浏览器会发生这种情况以及如何修复。

我花了大约10个小时对此进行研究。我尝试过的事情没有什么区别:

  • 我尝试将 CORS 添加到我的 Express 后端
  • 我尝试将 graphql 端点作为 HTTPS 提供服务
  • 在主 app.js 服务器文件中移动 app.use(express.static)

我找不到很多其他的东西可以尝试。我看到的所有地方似乎都说 CORS 解决了问题,但我的问题仍然存在。

Github 链接:https://github.com/LucaProvencal/thedrumroom Live Heroku 应用程序:https://powerful-shore-83650.herokuapp.com/

App.js(快速后端):

const cors = require('cors')
// const fs = require('fs')
// const https = require('https')
// const http = require('http')

app.use(express.static(path.join(__dirname, 'client/build')));
app.use(cors('*')); //NEXT TRY app.use(cors('/login')) etc...
app.use(cors('/*'));
app.use(cors('/'));
app.use(cors('/register'));
app.use(cors('/login'));
Run Code Online (Sandbox Code Playgroud)
app.get('/login', …
Run Code Online (Sandbox Code Playgroud)

heroku apollo apollo-server react-apollo express-graphql

5
推荐指数
1
解决办法
7361
查看次数

GraphQL 文件上传的拒绝和错误处理

我有一个带有接受单个文件的解析器的快速 GraphQL 端点。解析器包括对正在接收的文件的简单验证。

问题在于,当验证失败时,没有办法立即将错误返回给前端,因为抛出错误并不会强制中断上传请求。

一个过于简单的例子:

fileUpload: async (parent, { file, otherdata }) => {
    const isValid = (some logic here)
    if(!isValid)
        throw new ApolloError('upload parameters not valid')

    //No need to get this far,
    //we could have rejected the request already by examining otherdata,
    //or the stream could be already created for max time utilization

    const { createReadStream, filename, mimetype, encoding } = await file;
    const readStream = await createReadStream()
    ...
 }
Run Code Online (Sandbox Code Playgroud)

预期行为:解析器返回通常的 {errors:[], data:null} 对象 - 或错误本身 - 取决于错误策略选项。 …

error-handling upload file-upload graphql express-graphql

5
推荐指数
1
解决办法
855
查看次数

将对象作为参数传递给 GraphQL Mutation (graphql-request)

我在前端有一个非常基本的 graphql 突变,我将其发送到后端。我在 by 上使用此代码graphql-request作为指南。

对于原语它可以工作:

const mutation = gql`
    mutation EditArticle($id: ID!, $title: String) {
      editArticle(id: $id, title: $title) {
        id
      }
    }
  `
Run Code Online (Sandbox Code Playgroud)

现在我还希望能够更改有关文章的一些元数据,这些元数据存储在meta文章内的对象中:

...,
title: "Hello World",
meta: {
 author: "John",
 age: 32,
 ...
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:当使用graphql-request从前端发出请求时,如何将非原始对象类型作为参数传递给突变?

我已经尝试过这样的事情:

const Meta = new GraphQLObjectType({
    name: "Meta",
    fields: () => ({
      id: { type: GraphQLID },
      name: { type: GraphQLString },
      age ....
    }),
  })
   
const mutation = gql`
    mutation EditArticle($id: ID!, $title: …
Run Code Online (Sandbox Code Playgroud)

javascript graphql graphql-js express-graphql graphql-mutation

5
推荐指数
1
解决办法
6897
查看次数