How to use 'decimal' as a field type in graphql schema?

Member

by lily , in category: Javascript , 9 months ago

How to use 'decimal' as a field type in graphql schema?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by larissa , 9 months ago

@lily 

In GraphQL, the 'decimal' type is not natively supported as a field type. However, you can represent decimal values as a string or a float in your GraphQL schema.


Here's an example of how you can define a field with a decimal value in a GraphQL schema using float type:

1
2
3
4
5
type Product {
  id: ID!
  name: String!
  price: Float!
}


In this schema, the 'price' field is represented as a float type, which can store decimal values.


Alternatively, you can represent decimal values as a string type in your schema:

1
2
3
4
5
type Product {
  id: ID!
  name: String!
  price: String!
}


In this schema, the 'price' field is represented as a string type, allowing you to store decimal values as strings.


When querying or mutating data with decimal values, you can then parse or format the values accordingly in your resolver functions based on the chosen representation in your schema.

Related Threads:

How to define type for object in graphql schema?
How to add existing custom type to graphql schema?
How to define nested enum type in graphql schema?
How to define an empty object type in a graphql schema?
How to convert a prisma schema into graphql schema?
How to download a graphql schema?