Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Representing calculations/script functions in GraphQL queries and schema

We are using GraphQL as a query language for a data aggregation engine.

I am looking for ideas to represent simple (or complex) arithmetic calculation functions in GraphQL referring to existing types/attributes defined in a schema, and can be used over existing attributes.

I am looking into Custom scalars and Directives

Example -

{
    item{
        units
        price_per_unit
        market_price: function:multiply(units, price_per_unit)
        market_price_usd: function:usdPrice(units, price_per_unit, currency)
    }
}

where function:multiply is already defined in the GraphQL schema as a type

functions {
    multiply(operand 1, operand2) {
        result
    }
    usdPrice(operand1, operand2, currency) {
        result: {
                if(currency == GBP) {
                    operand1 * operand2 * .76
                }
            {
    }

internally resolver will multiply operand 1 and operand 2 to create result.

like image 250
am00100 Avatar asked Sep 24 '18 20:09

am00100


People also ask

How do you access data in GraphQL schema?

Like many other type systems, GraphQL schemas include the ability to define interfaces and union types. Learn about them in the schema guide. If you are querying a field that returns an interface or a union type, you will need to use inline fragments to access data on the underlying concrete type.

What is the use of GraphQL?

GraphQL has been gaining wide adoption as a way of building and consuming Web APIs. GraphQL is a specification that defines a type system, query language, and schema language for your Web API, and an execution algorithm for how a GraphQL service (or engine) should validate and execute queries against the GraphQL schema.

What are the query types for GraphQL services?

Every GraphQL service has a query type and may or may not have a mutation type. These types are the same as a regular object type, but they are special because they define the entry point of every GraphQL query. So if you see a query that looks like: That means that the GraphQL service needs to have a Query type with hero and droid fields:

What is a graphqlschema object?

As you can see, the GraphQLSchema object is a useful intermediate representation. One thing you might learn from this post is that GraphQL.js, the reference implementation of GraphQL in JavaScript, is an incredibly useful set of tools for working with GraphQL schemas and queries.


1 Answers

This isn't something GraphQL is especially good at. By far the easiest thing to do will be to retrieve the individual fields and then do the computation on the client, something like

data.item.forEach((i) => { i.total_price = i.units * i.price_per_unit });

In particular, there's no way to run any sort of "subquery" in GraphQL. Given a "multiply" function like you've shown there's no GraphQL syntax that will let you "call" it with any particular inputs.

If you think the specific computed values are common enough, you can also add them to the GraphQL schema, and compute them server-side if requested using a custom resolver function.

type Item {
  units: Int!
  pricePerUnit: CurrencyValue!
  # computed, always units * pricePerUnit
  marketPrice: CurrencyValue!
}
type CurrencyValue {
  amount: Float!
  currency: Currency!
  # computed, always amount * currency { usd }
  usd: Float!
}
type Currency {
  code: String!
  "1 currency = this many US$"
  usd: Float!
}

allowing queries like

{
  item {
    marketPrice { usd }
  }
}
like image 56
David Maze Avatar answered Nov 08 '22 16:11

David Maze