标签: apollo-server

GraphQL:对来自不同数据源的嵌套实体进行过滤,排序和分页?

我正在尝试使用graphql将许多休息端点绑定在一起,而我仍然坚持如何过滤,排序和分页结果数据.具体来说,我需要通过嵌套值进行过滤和/或排序.

在所有情况下,我都无法对其余端点进行过滤,因为它们是具有单独数据库的独立微服务.(即我可以title在其余端点中过滤文章,但不能在author.name上过滤).同样排序.如果没有过滤和排序,也无法在其余端点上进行分页.

为了说明问题,并尝试解决方案,我formatResponseapollo-server中使用了以下内容,但我想知道是否有更好的方法.

我已经将解决​​方案归结为我能想到的最小的文件集:

data.js表示2个虚构的休息端点返回的内容:

export const Authors = [{ id: 1, name: 'Sam' }, { id: 2, name: 'Pat' }];

export const Articles = [
  { id: 1, title: 'Aardvarks', author: 1 },
  { id: 2, title: 'Emus', author: 2 },
  { id: 3, title: 'Tapir', author: 1 },
]
Run Code Online (Sandbox Code Playgroud)

模式定义为:

import _ from 'lodash';
import {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLList,
  GraphQLString,
  GraphQLInt,
} from 'graphql';

import {
  Articles,
  Authors,
} from './data';

const …
Run Code Online (Sandbox Code Playgroud)

microservices graphql graphql-js apollo-server

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

子解析器中的Graphql-Access参数

我正在使用apollo-server和apollo-graphql-tools,我有以下架构

type TotalVehicleResponse {
  totalCars: Int
  totalTrucks: Int
}

type RootQuery {
  getTotalVehicals(color: String): TotalVehicleResponse
}

schema {
  query: RootQuery
}
Run Code Online (Sandbox Code Playgroud)

和解析器功能是这样的

{
  RootQuery: {
    getTotalVehicals: async (root, args, context) => {
      // args = {color: 'something'}
      return {};
    },
    TotalVehicleResponse: {
      totalCars: async (root, args, conext) => {
        // args is empty({}) here
        .........
        .........
      },
      totalTrucks: async (root, args, conext) => {
        // args is empty({}) here
        .........
        .........
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何在任何子args解析器中访问root解析器(getTotalVehicals)中可用的?

apollo graphql graphql-js apollo-server

7
推荐指数
3
解决办法
3814
查看次数

有没有办法摆脱 GraphQL 中的 [Object: null prototype]

我试图与一个一对多的关系数据库MongooseGraphQL

每当我将数据插入 GraphQL 突变参数时,我都会收到[Object: null prototype]错误消息。

我注意到[Object: null prototype]当我尝试console.log进行调试时,对象会在它前面。

我尝试了很多方法,尝试使用map()args 甚至使用replace()但没有运气。我得到的只是"args.ingredient.map/replace is not a function"

我通过更改参数来测试硬编码方法,例如:

args.category = '5c28c79af62fad2514ccc788'
args.ingredient = '5c28c8deb99a9d263462a086'
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是,它适用于这种方法。我假设输入不能是一个对象而只是一个 ID。

实际结果见下文。

解析器

Query: {
    recipes: async (root, args, { req }, info) => {
        return Recipe.find({}).populate('ingredient category', 'name createdAt').exec().then(docs => docs.map(x => x))
    },
},
Mutation: {
    addRecipe: async (root, args, { req }, info) => {
      // args.category = '5c28c79af62fad2514ccc788' …
Run Code Online (Sandbox Code Playgroud)

javascript mongoose node.js graphql apollo-server

7
推荐指数
2
解决办法
9006
查看次数

缺少 GET 查询:在 Express 服务器上使用 Apollo 实现 GraphQL

我正在遵循此处的教程:在 Express 服务器上使用 Apollo 实现 GraphQL ,并且我在浏览器中收到错误GET 查询丢失http://localhost:7700/graphql

首先我自己输入了所有代码。当我遇到错误时,我从GitHub下载了代码:kimobrian/GraphQL-Express:使用GraphQL实现的Express Server,以消除我犯错误的可能性。但是,我仍然遇到同样的错误。

我认为最好提供存储库的链接,而不是将代码粘贴到此处,因为我使用存储库中的相同代码。另外,我不确定哪个文件可能包含问题。

$ npm start

> tutorial-server@1.0.0 start kimobrian/GraphQL-Express.git
> nodemon ./server.js --exec babel-node -e js

[nodemon] 1.18.9
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `babel-node ./server.js`
GraphQL Server is now running on http://localhost:7700
Run Code Online (Sandbox Code Playgroud)

错误是浏览器中http://localhost:7700/graphql缺少 GET 查询。Firefox 和 Chromium 中也是如此。

更新:我发现的相关信息的唯一问题在这里:nodejs with Graphql。建议的解决方案是

server.use('/graphiql', graphiqlExpress({
  endpointURL: '/graphql',
}));
Run Code Online (Sandbox Code Playgroud)

然而,这正是我已经拥有的代码(来自教程)。这是我的全部server.js

import express from …
Run Code Online (Sandbox Code Playgroud)

node.js express apollo graphql apollo-server

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

如何知道 GraphQL 查询中请求了哪些字段?

我编写了一个 GraphQL 查询,如下所示:

{
  posts {
    author {
      comments
    }
    comments
  }
}
Run Code Online (Sandbox Code Playgroud)

我想知道如何获取有关posts解析器中请求的子字段的详细信息。

我想这样做是为了避免解析器的嵌套调用。我正在使用 ApolloServer 的DataSourceAPI。

我可以更改 API 服务器以一次获取所有数据。

我正在使用 ApolloServer 2.0,也欢迎任何其他避免嵌套调用的方法。

resolver apollo graphql apollo-server

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

基于嵌套字段的过滤器从 apollo-server 返回数据

是否可以为 apollo 中基于嵌套字段过滤器的查询返回数据?例如:

询问:

Users($filter: String!) {
  user(filter: $filter) { 
    id, 
    name, 
    address(filter: $filter) { 
       street, 
       city, 
       country
    } 
  } 
}
Run Code Online (Sandbox Code Playgroud)

类型定义:

Query: {
   users(filter: String): [User]!
}

User: {
    id: ID!,
    name: String!,
    address: Address
}

Address: {
    street: String!,
    city: String!,
    country: String!
}
Run Code Online (Sandbox Code Playgroud)

阿波罗解析器:

const resolverMap = {
  Query: {
    User(obj, args, context, info) {
      // query api for User info (not including address)
      // the final results of the query should only return users with the …
Run Code Online (Sandbox Code Playgroud)

apollo reactjs graphql apollo-server

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

使用 Dataloader 处理 GraphQL 字段参数?

我想知道在使用 Dataloader 时如何最好地处理 GraphQL 字段参数是否有任何共识。batchFnDataloader 需要的批处理函数期望接收Array<key>并返回一个Array<Promise>,通常只调用load( parent.id )whereparent是给定字段的解析器的第一个参数。在大多数情况下,这很好,但是如果您需要为嵌套字段提供参数怎么办?

例如,假设我有一个 SQL 数据库,其中包含用于Users, 的表Books,以及一个称为BooksRead表示用户:书籍之间 1:many 关系的关系表。

我可能会运行以下查询来查看所有用户阅读的书籍:

query {
  users {
    id
    first_name
    books_read {
      title
      author {
        name
      }
      year_published
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

假设在 中有一个BooksReadLoaderavailable context,这样解析器 forbooks_read可能如下所示:

const UserResolvers = {
  books_read: async function getBooksRead( user, args, context ) {
    return await context.loaders.booksRead.load( user.id );
  }
};
Run Code Online (Sandbox Code Playgroud)

的批处理加载函数BooksReadLoader将 …

javascript sql graphql apollo-server

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

在 Apollo Server 中调用另一个解析器的最佳方法是什么?

假设您想createdAtupdatedAt解析器调用解析器。例如,这不起作用:

{
  Review: {
    createdAt: review => review._id.getTimestamp(),
    updatedAt: review => review.updatedAt || this.createdAt(review)
  },
}
Run Code Online (Sandbox Code Playgroud)

我意识到我可以创建一个reviewCreatedAt()从两者调用的函数,但我正在寻找一种调用createdAt解析器的方法。

apollo graphql apollo-server

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

“ApolloServer”类型的参数不可分配给“ApolloServerBase”类型的参数

我使用apollo-server-testingpackage编写集成测试遵循这个官方文档。但是当我将ApolloServer类的实例传递给createTestClient方法时,tsc抛出一个类型不兼容的错误。

“ApolloServer”类型的参数不能分配给“ApolloServerBase”类型的参数。属性“requestOptions”的类型不兼容。类型 'Partial>' 不能分配给类型 'Partial>'。属性“documentStore”的类型不兼容。输入'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache | undefined' 不可分配到类型 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-caching/dist/ InMemoryLRUCache").InMemoryLRUCache ...'。输入 'import("/Users/ldu020/workspace/github.

这是最小的复制代码:

topic.integration.test.ts

import { constructTestServer } from '../../../__utils';
import { createTestClient } from 'apollo-server-testing';

describe('topic integration test suites', () => {
  it('should ', () => {
    const { server, cnodeAPI } = constructTestServer();
    const { query } = createTestClient(server); // tsc throw an error here
  });
});
Run Code Online (Sandbox Code Playgroud)

__util.ts

import { ApolloServer } from 'apollo-server';
import { …
Run Code Online (Sandbox Code Playgroud)

typescript apollo-server apollo-server-testing

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

Apollo 服务器:将参数传递给嵌套解析器

我的 GraphQL 查询如下所示:

{
    p1: property(someArgs: "some_value") {
        id
        nestedField {
            id
            moreNestedField {
                id
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在服务器端,我使用 Apollo Server。我有一个用于 的解析器,以及用于和property的其他解析器。我需要检索嵌套解析器上的值。我尝试使用解析器上的可用资源来执行此操作:nestedFieldmoreNestedFieldsomeArgscontext

property: (_, {someArgs}, ctx) => {
    ctx.someArgs = someArgs;

    // Do something
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为上下文在所有解析器之间共享,因此如果我property的查询有多个,则上下文值不会很好。

我还尝试在嵌套解析器上使用path可用的。info我可以去现场property,但我在这里没有争论......

我还尝试添加一些数据info,但它没有在嵌套解析器上共享。

在所有解析器上添加参数不是一个选项,因为它会使查询变得非常臃肿并且编写起来很麻烦,我不希望这样。

有什么想法吗?

谢谢!

javascript node.js graphql apollo-server

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