How to fix the error "the trait `OutputType` is not implemented for `time::OffsetDateTime`"?

868 Views Asked by At

I'm using async-graphql and I'm trying to use this struct as SimpleObject:

use async_graphql::{CSimpleObject};

#[derive(SimpleObject)]
struct Player {
    id: String,
    created_at: time::OffsetDateTime,
    email: String,
}

but I'm getting this error:

error[E0277]: the trait bound `time::OffsetDateTime: OutputType` is not satisfied
   |
50 | #[derive(SimpleObject)]
   |          ^^^^^^^^^^^^ the trait `OutputType` is not implemented for `time::OffsetDateTime`
   |
   = help: the following other types implement trait `OutputType`:
             &'a [T]
             &'impl0 T
             Arc<T>
             Arc<[T]>
             BTreeMap<K, V>
             BTreeSet<T>
             Cow<'a, T>
             Edge<Cursor, Node, EdgeFields, Name>
           and 56 others
   = note: required because of the requirements on the impl of `OutputType` for `&time::OffsetDateTime`
   = note: this error originates in the derive macro `SimpleObject` (in Nightly builds, run with -Z macro-backtrace for more info)

What can I do to fix this?

I tried to search but I cannot understand what to do.

2

There are 2 best solutions below

3
On BEST ANSWER

First, the problem: derivation is generally recursive, so all the members of a structure have to implement the trait being derived before the structure itself can.

Generally (de)serialization libraries will implement the trait on standard library types, but they can't feasibly do so for every crate out there (not to mention they would have to depend on all of them in the first place).

So the possible solutions are:

  • Check the features of the library, sometimes the (de)serialization library will have an optional derivation for "major" crates of the ecosystem, which time probably qualifies as. This lets you just enable the corresponding feature and get the integration.
  • Barring that, you'll have to implement whatever trait or feature is necessary by hand, unless the field doesn't matter and you can just tell the library to skip it.
0
On

add time feature to Cargo.toml:

async-graphql = { version = "6.0.11", features = ["time"] }