How to define an empty object type in a graphql schema?

Member

by gilbert , in category: Javascript , 9 months ago

How to define an empty object type in a graphql schema?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raphael_tillman , 9 months ago

@gilbert 

In GraphQL, you can define an empty object type by simply declaring an object type with no fields. Here's an example of how you can define an empty object type in a GraphQL schema:

1
2
3
type EmptyObjectType {
  id: ID! # An empty object type can have an ID field if needed
}


In this example, EmptyObjectType is a GraphQL object type with no fields except for an id field. Since GraphQL requires at least one field in an object type, you can add a dummy field like id to satisfy this requirement.


Alternatively, you can also define an empty object type with no fields at all like so:

1
2
3
type EmptyObjectType {
  # Empty object type with no fields
}


This will define an empty object type in the GraphQL schema without any fields.

Related Threads:

How to define type for object in graphql schema?
How to define nested enum type in graphql schema?
How to add existing custom type to graphql schema?
How to use 'decimal' as a field type in graphql schema?
How to pass object type argument in query in graphql?
How to return complex object as scalar type in graphql?