简体   繁体   中英

Using the same object for ObjectType and InputObjectType

I have a model object defined like this:

case class OrganizationId(value: Long) extends AnyVal with TypedId

case class OrganizationFields(name: String, iban: Option[String], bic: Option[String])

case class Organization(
  id: OrganizationId, addressId: AddressId, updatedAt: LocalDateTime, insertedAt: LocalDateTime, fields: OrganizationFields
)

And here is what I'm trying to do in my sangria schema definition:

implicit val OrganizationFields = deriveObjectType[GraphqlContext, OrganizationFields]()
  implicit val OrganizationType: ObjectType[GraphqlContext, Organization] = deriveObjectType[GraphqlContext, Organization]()
implicit val OrganizationInputType = deriveInputObjectType[OrganizationFields]()

I need to define OrganizationFields both as an ObjectType to be able to use Organization in my graphql queries and also as an ObjectInputType to be able to use it in my graphql mutation as input. The issue is that I get the following exception at run time:
Type name 'OrganizationFields' is used for several conflicting GraphQL type kinds: ObjectType, InputObjectType. Conflict found in an argument 'organization' defined in field 'createOrganization' of 'Mutation' type
Is there a way to make it work?

In your example, both GraphQL types would have the same name. This is not allowed since all types within a GraphQL schema share the same global namespace.

You can rename one of these types in order to solve this issue. For example:

implicit val OrganizationInputType = deriveInputObjectType[OrganizationFields](
  InputObjectTypeName("OrganizationFieldsInput"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM