Model fetchable entities
Define an entity such as User, Product, or Order with fields clients can select.
GraphQL · Class
GraphQLObjectType defines a schema object with named, selectable fields. Each field has an output type and can accept input arguments and resolve its value.
GraphQLObjectType is the JavaScript constructor used to define a concrete GraphQL object in a schema. Its configuration names the type and supplies fields; each field declares an output type and may provide a resolver that produces the field value.
new GraphQLObjectType({ name, fields })| Name | Type | Description |
|---|---|---|
config | GraphQLObjectTypeConfig | Configuration object containing the type name and field definitions; individual fields specify output types and may define arguments and resolvers. |
A GraphQL object type representing a fetchable kind of object and its fields.
import { graphql, GraphQLObjectType, GraphQLSchema, GraphQLString } from "graphql";
const Query = new GraphQLObjectType({
name: "Query",
fields: {
greeting: {
type: GraphQLString,
resolve: () => "Hello, GraphQL!",
},
},
});
const schema = new GraphQLSchema({ query: Query });
const result = await graphql({
schema,
source: "{ greeting }",
});
console.log(result.data?.greeting);Defines an object type with one output field, uses it as the query root, and executes a matching query.
Define an entity such as User, Product, or Order with fields clients can select.
Expose the entry points for read operations through a Query object.
Connect schema fields to application data sources with resolver functions.
GraphQLObjectType defines an object type in the executable schema. The type groups named fields, and each field exposes an output type; fields can also accept arguments and resolve values.
Follow-up: How would you model a field that accepts a filter? Define the field's return type as an output type and define its arguments with input types, such as scalars or input objects.
An object type represents a kind of object that clients can fetch and the fields available on it. Its fields describe the selectable data and their relationships to other types.
Follow-up: Why can objects form nested query trees? Because an object field can return another object, allowing clients to select fields at successive levels.
The query root must be an object type. It is the starting point for query operations, and its fields expose the operations clients may select.
Follow-up: Can the query root be an input object? No. Input objects are for arguments; the query root must be an output object type.
GraphQLSchemaScalar typesEnum typesInterface typesUnion typesInput Object types