GiraphQL
Search…
Fields
Fields for Object and Interface types are defined using a shape function. This is a function that takes a FieldBuilder as an argument, and returns an object who's keys are field names, and who's values are fields created by the FieldBuilder. These examples will mostly add fields to the Query type, but the topics covered in this guide should apply to any object or interface type.

Scalars

Scalar fields can be defined a couple of different ways:

Field method

1
builder.queryType({
2
fields: t => ({
3
name: t.field({
4
description: 'Name field'
5
type: 'String',
6
resolve: () => 'Gina',
7
}),
8
}),
9
});
Copied!

Convenience methods

Convenience methods are just a wrapper around the field method, that omit the type option.
1
builder.queryType({
2
fields: (t) => ({
3
id: t.id({ resolve: () => '123' }),
4
int: t.int({ resolve: () => 123 }),
5
float: t.float({ resolve: () => 1.23 }),
6
boolean: t.boolean({ resolve: () => false }),
7
string: t.string({ resolve: () => 'abc' }),
8
idList: t.idList({ resolve: () => ['123'] }),
9
intList: t.intList({ resolve: () => [123] }),
10
floatList: t.floatList({ resolve: () => [1.23] }),
11
booleanList: t.booleanList({ resolve: () => [false] }),
12
stringList: t.stringList({ resolve: () => ['abc'] }),
13
}),
14
});
Copied!

Other types

Fields for non-scalar fields can also be created with the field method.
Some types like Objects and Interfaces can be referenced by name if they have a backing model defined in the schema builder.
1
const builder = new SchemaBuilder<{
2
Objects: { Giraffe: { name: string } };
3
}>({});
4
5
builder.queryType({
6
fields: t => ({
7
giraffe: t.field({
8
description: 'A giraffe'
9
type: 'Giraffe',
10
resolve: () => ({ name: 'Gina' }),
11
}),:
12
}),
13
});
Copied!
For types not described in the SchemaTypes type provided to the builder, including types that can not be added there like Unions and Enums, you can use a Ref returned by the builder method that created them in the type parameter. For types created using a class (Objects or Interfaces) or Enums created using a typescript enum, you can also use the the class or enum that was used to define them.
1
const LengthUnit = builder.enumType('LengthUnit', {
2
values: { Feet: {}, Meters: {} },
3
});
4
5
builder.objectType('Giraffe', {
6
fields: (t) => ({
7
preferredNeckLengthUnit: t.field({
8
type: LengthUnit,
9
resolve: () => 'Feet',
10
}),
11
}),
12
});
13
14
builder.queryType({
15
fields: (t) => ({
16
giraffe: t.field({
17
type: 'Giraffe',
18
resolve: () => ({ name: 'Gina' }),
19
}),
20
}),
21
});
Copied!

Lists

To create a list field, you can wrap the the type in an array
1
builder.queryType({
2
fields: t => ({
3
giraffes: t.field({
4
description: 'multiple giraffes'
5
type: ['Giraffe'],
6
resolve: () => [{ name: 'Gina' }, { name: 'James' }],
7
}),
8
giraffeNames: t.field({
9
type: ['String'],
10
resolve: () => ['Gina', 'James'],
11
})
12
}),
13
});
Copied!

Nullable fields

Unlike some other GraphQL implementations, fields in GiraphQL are non-nullable by default. It is still often desirable to make fields in your schema nullable. This default can be changed in the SchemaBuilder constructor, see Changing Default Nullability.
1
builder.queryType({
2
fields: (t) => ({
3
nullableField: t.field({
4
type: 'String',
5
nullable: true,
6
resolve: () => null,
7
}),
8
nullableString: t.string({
9
nullable: true,
10
resolve: () => null,
11
}),
12
nullableList: t.field({
13
type: ['String'],
14
nullable: true,
15
resolve: () => null,
16
}),
17
spareseList: t.field({
18
type: ['String'],
19
nullable: {
20
list: false,
21
items: true,
22
},
23
resolve: () => [null],
24
}),
25
}),
26
});
Copied!
Note that by default even if a list field is nullable, the items in that list are not. The last example above shows how you can make list items nullable.

Exposing fields from the underlying data

Some GraphQL implementations have a concept of "default resolvers" that can automatically resolve field that have a property of the same name in the underlying data. In GiraphQL, these relationships need to be explicitly defined, but there are helper methods that make exposing fields easier.
These helpers are not available for root types (Query, Mutation and Subscription), but will work on any other object type or interface.
1
const builder = new SchemaBuilder<{
2
Objects: { Giraffe: { name: string } };
3
}>({});
4
5
builder.objectType('Giraffe', {
6
fields: (t) => ({
7
name: t.exposeString('name', {}),
8
}),
9
});
Copied!
The available expose helpers are:
    exposeString
    exposeInt
    exposeFloat
    exposeBoolean
    exposeID
    exposeStringList
    exposeIntList
    exposeFloatList
    exposeBooleanList
    exposeIDList

Arguments

Arguments for a field can defined in the options for a field:
1
builder.queryType({
2
fields: (t) => ({
3
giraffeByName: t.field({
4
type: 'Giraffe',
5
args: {
6
name: t.arg.string({ required: true }),
7
},
8
resolve: (root, args) => {
9
if (args.name !== 'Gina') {
10
throw new NotFoundError(`Unknown Giraffe ${name}`);
11
}
12
13
return { name: 'Gina' };
14
},
15
}),
16
}),
17
});
Copied!
For more information see the Arguments Guide.

Adding fields to existing type

In addition to being able to define fields when defining types, you can also add additional fields independently. This is useful for breaking up types with a lot of fields into multiple files, or co-locating fields with their type (eg. add all query/mutation fields for a type in the same file where the type is defined).
1
builder.queryFields((t) => ({
2
giraffe: t.field({
3
type: Giraffe,
4
resolve: () => new Giraffe('James', new Date(Date.UTC(2012, 11, 12)), 5.2),
5
}),
6
}));
7
8
builder.objectField(Giraffe, 'ageInDogYears', (t) =>
9
t.int({
10
resolve: (parent) => parent.age * 7,
11
}),
12
);
Copied!
To see all they methods available for defining fields see the SchemaBuilder API
Last modified 2mo ago