【问题标题】:I can't setup a react apollo mongodb app => resolvers with mongo / mongoose / NO METEOR我无法使用 mongo / mongoose / NO METEOR 设置 react apollo mongodb app => 解析器
【发布时间】:2018-01-08 03:03:07
【问题描述】:

我目前正在学习Apollo,我是一名前端开发人员(后台半年多角1.5) 我对 mongodb 以及 apollo(通过流星应用程序)的经验很少

我已经阅读了好几遍 Apollo 文档,用关键字 apollo、react、mongodb、resolvers 解析了 Google,各种组合......(没有结果)

我需要构建一个小的 React 应用程序 (POC),它处理 查询、突变和订阅,它将使用 mongodb(或其他数据库,我指出这一点)持久保存数据一个,因为这只是我迄今为止合作过的一个)。

不能使用Meteor

我的问题是,我找不到 graphql resolver 在我需要的所有情况下(查询、子、变异)使用 mongo 的合适示例。

您能否提供一些样板,以帮助我了解机制?

谢谢。

【问题讨论】:

    标签: mongodb reactjs mongoose apollo


    【解决方案1】:

    如果您不想阅读一些教程。你可以看看 githunt 项目。它是一个带有查询、突变和订阅的 apollo-graphql 实现。它不使用 mongoDB,但这应该是一个简单的更改。

    服务器:https://github.com/apollographql/GitHunt-API/tree/master/api

    客户:https://github.com/apollographql/GitHunt-React

    干杯

    【讨论】:

    • 我确实尝试过,但有些需要重写的部分是我目前的技能。我发布了一个答案,该答案指向一个样板文件(不是来自我),供可能需要相同代码才能正常启动的人使用。还是谢谢!
    【解决方案2】:

    我认为graphqly 是你的答案。

    import graphly from "graphqly";
    
    const gBuilder = graphly.createBuilder();
    
    // define types, inputs ... (in any order)
    gBuilder.type("Products").implements("List").def(`
      products: [Product]!
    `);
    
    gBuilder.type("Product").def(`
      id: ID!
      name: String!
      link: String
      price: Int
    `);
    
    // we're too lazy to define a separate input, so we can `extend` other structure
    gBuilder.input("ProductInput").ext("Product");
    
    gBuilder.enum("ProductOrder").def(`
      PRICE_DESCENDING
      PRICE_ASCENDING
      NEWEST
    `);
    
    // define interface `List`
    gBuilder.iface("List").def(`
      total: Int!,
      offset: Int!,
      limit: Int!,
      # number of items actually in this window
      size: Int!
    `);
    
    
    gBuilder.query(`
      products(limit: Int = 20, offset: Int = 0, filter: ProductFilter): Products
    `)
    .resolve((root, args, context) => {
      // your resolver here
      // `this` is binded to the current Query instance
    });
    

    【讨论】:

    • 感谢您的回答,尽管我正在寻找更完整的东西(请参阅我的回答)。不过还是谢谢!
    【解决方案3】:

    所以,对于只有 mongo / mongoose 的服务器部分来说,这是一个好的开始:

    https://github.com/khadorkin/apollo-server-mongoose

    我已对此进行了增强,我将在几天后提供指向 github 存储库的链接。

    这里是回购。 仍有工作要做,特别是关于订阅,但这可能是一个好的开始

    Link to Github repo

    【讨论】:

      猜你喜欢
      • 2021-03-28
      • 1970-01-01
      • 2017-12-21
      • 2018-09-22
      • 2021-10-26
      • 2017-03-22
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      相关资源
      最近更新 更多