使用Sangria创建资源时,保持id不受GraphQL迁移的影响

时间:2017-02-11 19:58:11

标签: scala graphql sangria

我一直试图定义我认为是非常基本的突变。 我是所有Scala,GraphQL,Akka HTTP的新手,而不是母语人士,所以请原谅我,如果下面的任何内容都是废话!对于冗长的帖子感到抱歉,我确实尝试用最少的例子来保持简短,但我觉得完整的背景很重要。

上下文

我有以下代码定义Transaction案例类,所有事务的假数据库,以及查询和创建事务的模式:

case class Transaction(id: Int, description: String, amount: BigDecimal)

object TransactionDB {
  var transactions: List[Transaction] = Nil
}

class TransactionDB {
  def transaction(id: Int): Option[Transaction] =
    TransactionDB.transactions.find(_.id == id)
  def createTransaction(transaction: Transaction) = {
    TransactionDB.transactions = transaction :: TransactionDB.transactions
    transaction
  }
}

import sangria.macros.derive._
import sangria.marshalling.sprayJson._
import sangria.schema._
import spray.json._
import DefaultJsonProtocol._

object TransactionSchema {
  val Id = Argument("id", IntType)
  val TransactionType = deriveObjectType[Unit, Transaction]()
  val QueryType = ObjectType("Query", fields[TransactionDB, Unit](
    Field("transaction", OptionType(TransactionType),
      arguments = Id :: Nil,
      resolve = c ⇒ c.ctx.transaction(c.arg(Id))
    )
  ))

  implicit val transactionFormat = jsonFormat3(Transaction)
  val TransactionInputType = deriveInputObjectType[Transaction](
    InputObjectTypeName("TransactionInput")
  )

  val TransactionArg = Argument("transaction", TransactionInputType)
  val MutationType = ObjectType("Mutation", fields[TransactionDB, Unit](
    Field("createTransaction", TransactionType,
      arguments = TransactionArg :: Nil,
      resolve = c ⇒ c.ctx.createTransaction(c.arg(TransactionArg))
    )
  ))

  val schema = Schema(QueryType, Some(MutationType))
}

生成的GraphQL架构如下:

type Mutation {
  createTransaction(transaction: TransactionInput!): Transaction!
}

type Query {
  transaction(id: Int!): Transaction
}

type Transaction {
  id: Int!
  description: String!
  amount: BigDecimal!
}

input TransactionInput {
  id: Int!
  description: String!
  amount: BigDecimal!
}

然后我可以创建一个事务并使用以下查询进行查询:

mutation {
  createTransaction(transaction: {
    id: 1
    description: "Electricity bill"
    amount: 100
  }) {
    id
  }
}

query {
  transaction(id: 1) {
    id
    description
    amount
  }
}

问题

但是,id不应该是突变的一部分。理想情况下,我得到:

input TransactionInput {
  description: String!
  amount: BigDecimal!
}

type Mutation {
  createTransaction(input: TransactionInput): Transaction
  updateTransaction(id: Int!, input: TransactionInput): Transaction # For later
}

itertools.product中,这是使用以下内容完成的:

createTransaction({description, amount}) {
  const id = generateId();
  transactionRepo[id] = new Transaction(id, description, amount);
  return transaction[id];
},
updateTransaction(id, {description, amount}) { // Assuming id exists
  transactionRepo[id] = new Transaction(id, description, amount);
  return transactionRepo[id];
},

在Scala / Sangria中寻找解决方案是我陷入困境的地方。

尝试解决方案

尝试1:我在考虑这样的事情:

def createTransaction(description: String, amount: BigDecimal) = {
  val r = scala.util.Random // Or leave this to the DB layer
  val transaction = Transaction(r.nextInt(10000), description, amount)
  TransactionDB.transactions = transaction :: TransactionDB.transactions
  transaction
}

但是这不起作用,因为createTransactionTransactionArg映射到Transaction

尝试2 :由于删除jsonFormat2,我还尝试切换到id,但这会在编译时触发type mismatch因为{{1}有3个属性。

尝试3 :我尝试使用以下内容更新派生的Transaction以排除InputObjectType

id

这会编译,但在尝试运行上面给出的突变结果时:

    val TransactionInputType = deriveInputObjectType[Transaction]( InputObjectTypeName("TransactionInput"), ExcludeInputFields("id") ) 字段中省略Argument 'transaction' has invalid value: Object is missing required member 'id'
  • id
  • 添加回来时
  • createTransaction(transaction: {...})

我错过了什么?使用Argument 'transaction' expected type 'TransactionInput!' but got: {id: 42, description: \"Electricity bill\", amount: 100}. Reason: Unknown field 'id' is not defined in the input type 'TransactionInput'.对我来说似乎很简单,但我无法使用Sangria在Scala中找到如何做到这一点。我多次阅读文档并广泛关注Google,因为这似乎是一个简单的问题,但没有找到类似的东西。谢谢!

1 个答案:

答案 0 :(得分:4)

似乎createTransactionupdateTransaction都返回了Transaction类型定义为:

type Transaction {
  id: Int!
  description: String!
  amount: BigDecimal!
}

id: Int!表示需要id(这是!的含义)所以我首先将其重新定义为:id: Int(无感叹号)。