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

Member

by lily , in category: Javascript , a month ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by larissa , a month 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.