GiraphQL
Search…
Simple Objects
The Simple Objects Plugin provides a way to define objects and interfaces without defining type definitions for those objects, while still getting full type safety.

Usage

Install

1
yarn add @giraphql/plugin-simple-objects
Copied!

Setup

1
import SimpleObjectsPlugin from '@giraphql/plugin-simple-objects';
2
const builder = new SchemaBuilder({
3
plugins: [SimpleObjectsPlugin],
4
});
Copied!

Example

1
import SchemaBuilder from '@giraphql/core';
2
import SimpleObjectsPlugin from '@giraphql/plugin-simple-objects';
3
4
const builder = new SchemaBuilder({
5
plugins: [SimpleObjectsPlugin],
6
});
7
8
const ContactInfo = builder.simpleObject('ContactInfo', {
9
fields: (t) => ({
10
email: t.string({
11
nullable: false,
12
}),
13
phoneNUmber: t.string({
14
nullable: true,
15
}),
16
}),
17
});
18
19
const Node = builder.simpleInterface('Node', {
20
fields: (t) => ({
21
id: t.id({
22
nullable: false,
23
}),
24
}),
25
});
26
27
const UserType = builder.simpleObject('User', {
28
interfaces: [Node],
29
fields: (t) => ({
30
firstName: t.string(),
31
lastName: t.string(),
32
contactInfo: t.field({
33
type: ContactInfo,
34
nullable: false,
35
}),
36
}),
37
});
38
39
builder.queryType({
40
fields: (t) => ({
41
user: t.field({
42
type: UserType,
43
args: {
44
id: t.arg.id({ required: true }),
45
},
46
resolve: (parent, args, { User }) => {
47
return {
48
id: '1003',
49
firstName: 'Leia',
50
lastName: 'Organa',
51
contactInfo: {
52
53
phoneNUmber: null,
54
},
55
};
56
},
57
}),
58
}),
59
});
Copied!

Limitations

When using simpleObjects in combination with other plugins like authorization, those plugins may use unknown as the parent type in some custom fields (eg. parent of a permission check function on a field).
Last modified 4mo ago