gqty icon indicating copy to clipboard operation
gqty copied to clipboard

Why each scalar `MakeNullable`?

Open sergeysova opened this issue 4 years ago • 5 comments

I have a graphQL schema:

Details
# Directs the executor to query only when the field exists.
directive @ifdef on FIELD

type AccessToken {
  token: String!
  scopes: [String!]!
  expiresAt: DateTime!
  registrationId: UUID!
  registration: UserRegistration
}

type Application {
  id: UUID!
  isDev: Boolean!
  redirectUri: [String!]!
  title: String!
  allowedRegistrations: Boolean!
  registrations: [UserRegistration!]!
}

input ApplicationCreate {
  title: String!
  redirectUri: [String!]!
  isDev: Boolean
  allowedRegistrations: Boolean
}

type ApplicationSecret {
  id: UUID!
  isDev: Boolean!
  redirectUri: [String!]!
  title: String!
  allowedRegistrations: Boolean!

  # Allowed to read only after application is created
  secretKey: String!
}

# Implement the DateTime<Utc> scalar
#
# The input/output is a string in RFC3339 format.
scalar DateTime

type Mutation {
  applicationCreate(form: ApplicationCreate!): ApplicationSecret!
  applicationRegenerateSecret(applicationId: UUID!): ApplicationSecret
  registerRequestCreate(email: String!): RegisterRequest!
  registerRequestDeleteAllForEmail(email: String!): Int!
}

type Query {
  version: String!
  accessTokens: [AccessToken!]!
  application(id: UUID!): Application
  applications: [Application!]!
  registerRequests: [RegisterRequest!]!
  users: [User!]!
  userByEmail(email: String!): User
  userById(userId: UUID!): User
}

type RegisterRequest {
  email: String!
  code: String!
  expiresAt: DateTime!
}

# A UUID is a unique 128-bit number, stored as 16 octets. UUIDs are parsed as Strings
# within GraphQL. UUIDs are used to assign unique identifiers to entities without requiring a central
# allocating authority.
#
# # References
#
# * [Wikipedia: Universally Unique Identifier](http://en.wikipedia.org/wiki/Universally_unique_identifier)
# * [RFC4122: A Universally Unique IDentifier (UUID) URN Namespace](http://tools.ietf.org/html/rfc4122)
scalar UUID

type User {
  id: UUID!
  email: String!
  canonicalEmail: String!
  firstName: String!
  lastName: String!
  registrations: [UserRegistration!]!
}

type UserRegistration {
  id: UUID!

  # Field renamed from `client_id`
  applicationId: UUID!
  createdAt: DateTime!
  userId: UUID!
  user: User
  application: Application
  accessTokens: [AccessToken!]!
}

As I see Application.title has a strong String! type.

But if I want to fetch it via resolved, I see string | undefined:

image

The same via useQuery():

image

Why there string | undefined on String! type?

sergeysova avatar Oct 27 '21 12:10 sergeysova

https://gqty.dev/docs/intro/how-it-works#skeleton-render--values

PabloSzx avatar Oct 27 '21 14:10 PabloSzx

@PabloSzx Can I disable this behaviour? I don't use react client at all. I'm using refetch and resolved

sergeysova avatar Oct 27 '21 15:10 sergeysova

@PabloSzx Can I disable this behaviour? I don't use react client at all. I'm using refetch and resolved

https://gqty.dev/docs/client/helper-functions#type-casters

You can use type casters, but read the warnings mentioned in the docs

PabloSzx avatar Oct 27 '21 15:10 PabloSzx

@PabloSzx , I wonder, why is that not the default? I mean, the docs do mention: This next example is a perfectly safe place to use it,.

smeijer avatar Feb 03 '22 10:02 smeijer

@PabloSzx , I wonder, why is that not the default? I mean, the docs do mention: This next example is a perfectly safe place to use it,.

because in that example is safe to do, but that example doesn't cover all the possibilities of gqty usage, and the priority is giving type-safety, and in much other scenarios doing type-casting is not safe

PabloSzx avatar Feb 05 '22 21:02 PabloSzx