Fauna is stating that the type User
don’t comply with my projection:
Fauna says:
Type `User` is not a subtype of `.{ { id: A, ttl: B, ts: C, firstName: D, lastName: E, primaryEmail: F, emails: G, emailVerification: H, avatar: I, activeOrganization: .{ { id: J, coll: K, ts: L, name: M, slug: N, members: O, plan: P, ... } => { id: J, coll: K, ts: L, name: M, slug: N, members: O, plan: P } } => Q, organizations: .{ { id: R, coll: S, ts: T, name: U, slug: V, members: W, plan: X, ... } => { id: R, coll: S, ts: T, name: U, slug: V, members: W, plan: X } } => Y, accounts: Z, ... } => { id: A, ttl: B, ts: C, firstName: D, lastName: E, primaryEmail: F, emails: G, emailVerification: H, avatar: I, activeOrganization: Q, organizations: Y, accounts: Z } } => Any`
I have the following Schema:
collection User {
firstName: String
lastName: String
primaryEmail: String
emails: Array<String>
compute emailVerification: String? = doc => { getVerificationEmail(doc.id)}
avatar: String?
activeOrganization: Ref<Organization>?
organizations: Array<Ref<Organization>>?
accounts: Array<Ref<Account>>
}
collection Organization {
name: String
logo: String?
slug: String
members: Array<{
user: Ref<User>
role: "role_organization_member" | "role_organization_admin" | "role_organization_owner"
}>
plan: "Free" | "Pro" | "Enterprise"
}
and the following function (simplified)
@role(server)
function createOrganization ( data: { name: String, logo: String?, slug: String } ): User {
let user: Any = Query.identity()
let user: User = user
[...]
user {
id,
ttl,
ts,
firstName,
lastName,
primaryEmail,
emails,
emailVerification,
avatar,
activeOrganization {
id,
coll,
ts,
name,
logo,
slug,
members,
plan
},
organizations {
id,
coll,
ts,
name,
logo,
slug,
members,
plan
},
accounts
}
}
As soon as I add the projection of activeOrganization
and organization
to the function, the Fauna CLI starts complaining. At this moment it is also telling me that I need to handle null cases like this as part of the projection:
cause: Type `Null` does not have field `ts`
at src/lib/db/schema/fsl/functions/organization/createOrganization.fsl:44:7
|
44 | ts,
| ^^
|
hint: Use the ! or ?. operator to handle the null case
at src/lib/db/schema/fsl/functions/organization/createOrganization.fsl:44:7
|
44 | !ts,
| +
|
but following this suggestion is only leading to an invalid function definition.