GraphQL-处理嵌套数据

时间:2019-08-22 03:21:43

标签: graphql

我的数据架构为:

{
    instrumentName: String,
    lastPrice {
        bid: Float,
        ask: Float
    }
}

数据以Json格式存储在我的数据库中,例如:

{
    "instrumentName": "GOOGLE.N",
    "lastPrice": {
        "bid": 90.00,
        "ask": 105.00
    }
}

在GraphQL中,我必须将graphQL模式定义为:

type Instrument {
    instrumentName: String
    lastPrice: Price
}
type Price {
    bid: Float
    ask: Float
}

构建数据提取器时,可以使用Instrument提取器。我只是从数据库中选择记录作为先前的示例数据。 但是,当它以递归方式获取价格时,由于已经从工具获取器中的数据库中加载了买入/卖出价格,我想知道是否有一种方法可以“告诉”价格获取器以获取先前的样本数据,例如,将数据导入价格获取程序的数据获取环境? 还是可以定义一个不同的(嵌套的)graphQL模式来处理这种情况?

1 个答案:

答案 0 :(得分:0)

我目前正在使用apollo-server,并且至少与此(不了解其他GraphQL实现)有关,default resolver behaviour应该为您解决这个问题,因此只有一个数据库如果您的instrument提取程序返回了完整的数据结构,则需要提取:

const resolvers = {
    Query: {
        instrument: (parent, args, context, info) => {
            instrumentName: "GOOGLE.N",
            lastPrice: {
                bid: 90.00,
                ask: 105.00
            }                    
        }
    },

    // Following resolver should not be needed as will be handled by default resolver
    //  Instrument: {
    //      lastPrice: (parent, args, context, info) => {
    //          ...
    //      }
    //  }
}

如果您使用的不是apollo服务器,则无法确定以上情况是否成立。