小编Har*_*uri的帖子

如何在GraphQL中返回对象数组,可能使用与返回单个对象相同的端点?

我正在制作GraphQL API,在其中我可以通过其ID检索汽车对象或在不提供任何参数的情况下检索所有汽车。

使用下面的代码,我可以通过提供id作为参数来成功检索单个汽车对象。

但是,在我希望得到对象数组的情况下,即当我完全不提供任何参数时,在GraphiQL上没有任何结果。

schema.js

let cars = [
  { name: "Honda", id: "1" },
  { name: "Toyota", id: "2" },
  { name: "BMW", id: "3" }
];

const CarType = new GraphQLObjectType({
  name: "Car",
  fields: () => ({
    id: { type: GraphQLString },
    name: { type: GraphQLString }
  })
});

const RootQuery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    cars: {
      type: CarType,
      args: {
        id: { type: GraphQLString }
      },
      resolve(parent, args) {
        if (args.id) {
          console.log(cars.find(car => …
Run Code Online (Sandbox Code Playgroud)

javascript node.js graphql graphql-js express-graphql

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

我的程序更改了函数内部数组的元素,而没有显式更改它.怎么会发生?

所以我正在编写一个函数,它应该交换数组的第一个和最后一个元素并返回修改后的数组.我的代码如下:

public static int[] swapEnds(int[] nums) {

    int newArray[] = new int[nums.length];
    newArray = nums; // copies all the elements to the new array
    newArray[0] = nums[nums.length -1 ]; // changes the first element of the newArray
    newArray[newArray.length-1] = nums[0]; // changes the last element of the newArray

    return newArray;
}
Run Code Online (Sandbox Code Playgroud)

通过做一些调试,我发现nums [0]已经以某种方式改变了,但我没有在我的代码中的任何地方进行更改.任何帮助将非常感激.谢谢.

java arrays swap function

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

在Observer Design模式中,Subject和Observable是同一个东西吗?

在观察者设计模式中,观察者观察主体和/或可观察者.观察者通过他们的更新得到通知.

我很困惑这两件事(主题和可观察的)是否基本相同?或者这两者之间有细微差别吗?

java design-patterns class subject observable

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