Skip to content

Commit 5cccb7b

Browse files
lynnshaoyufacebook-github-bot
authored andcommitted
- adding docs for abstract types in relay resolvers
Reviewed By: captbaritone Differential Revision: D65181244 fbshipit-source-id: 119462c2aae4793484575df8aeb20c44a727ef80
1 parent 065c60b commit 5cccb7b

File tree

4 files changed

+104
-8
lines changed

4 files changed

+104
-8
lines changed

website/docs/guides/relay-resolvers/defining-types.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,55 @@ export type ProfilePicture = { url: string, height: number, width: number };
108108
:::tip
109109
Generally weak types are used for creating a namespace for a set of fields that ultimately "belong" to a parent object.
110110
:::
111+
112+
### Implementing Abstract Types
113+
114+
Relay Resolver types can implement [abstract types](https://relay.dev/docs/glossary/#abstract-type) defined in the graphql schema. Note, these abstract types can
115+
be defined on your GraphQL server schema OR a [client side schema extension](https://relay.dev/docs/next/guides/client-schema-extensions/).
116+
117+
For example, given the following interface:
118+
119+
```graphql
120+
# IUser.graphql
121+
interface IUser {
122+
name: String
123+
}
124+
```
125+
126+
You could define two (or more) concrete resolver types that implement the IUser interface by adding annotations in the docblock (the same applies for unions).
127+
<FbInternalOnly>
128+
Note, support for abstract types is not available for relay resolvers in Flow syntax (yet).
129+
</FbInternalOnly>
130+
131+
<Tabs
132+
groupId="resolver"
133+
defaultValue="Docblock"
134+
values={fbContent({
135+
internal: [
136+
{label: 'Docblock', value: 'Docblock'},
137+
],
138+
external: [
139+
{label: 'Docblock', value: 'Docblock'},
140+
]
141+
})}>
142+
<TabItem value="Docblock">
143+
144+
145+
```tsx
146+
/**
147+
* @RelayResolver BasicUser implements IUser
148+
*/
149+
export function BasicUser(id: DataID): BasicUserModel {
150+
return { ...BasicUserService.getById(id), name: 'BasicUser1'};
151+
}
152+
153+
/**
154+
* @RelayResolver SpecialUser implements IUser
155+
*/
156+
export function SpecialUser(id: DataID): SpecialUserModel {
157+
return { ...SpecialUserService.getById(id), name: 'SpecalUser1'};
158+
}
159+
```
160+
161+
</TabItem>
162+
</Tabs>

website/docs/guides/relay-resolvers/limitations.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ Relay Resolvers are do have some limitations. Here we will collect a list of kno
1111

1212
In a full GraphQL implementation, resolvers would have access to an `info` argument. This argument is not available in Relay Resolvers today.
1313

14-
## No support for abstract types
15-
16-
Today it is not possible to define an interface or union with multiple concrete types using Relay Resolvers. This is something we would like to support in the future, but have not yet implemented.
17-
1814
## All fields must be nullable
1915

2016
Today all resolvers must be typed as nullable in order to support coercing errors to null without having to implement null bubbling. In the future we intend Resolvers to support some version of [strict semantic nullability](https://github.com/graphql/graphql-wg/discussions/1410).

website/versioned_docs/version-v18.0.0/guides/relay-resolvers/defining-types.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,55 @@ export type ProfilePicture = { url: string, height: number, width: number };
108108
:::tip
109109
Generally weak types are used for creating a namespace for a set of fields that ultimately "belong" to a parent object.
110110
:::
111+
112+
### Implementing Abstract Types
113+
114+
Relay Resolver types can implement [abstract types](https://relay.dev/docs/glossary/#abstract-type) defined in the graphql schema. Note, these abstract types can
115+
be defined on your GraphQL server schema OR a [client side schema extension](https://relay.dev/docs/next/guides/client-schema-extensions/).
116+
117+
For example, given the following interface:
118+
119+
```graphql
120+
# IUser.graphql
121+
interface IUser {
122+
name: String
123+
}
124+
```
125+
126+
You could define two (or more) concrete resolver types that implement the IUser interface by adding annotations in the docblock (the same applies for unions).
127+
<FbInternalOnly>
128+
Note, support for abstract types is not available for relay resolvers in Flow syntax (yet).
129+
</FbInternalOnly>
130+
131+
<Tabs
132+
groupId="resolver"
133+
defaultValue="Docblock"
134+
values={fbContent({
135+
internal: [
136+
{label: 'Docblock', value: 'Docblock'},
137+
],
138+
external: [
139+
{label: 'Docblock', value: 'Docblock'},
140+
]
141+
})}>
142+
<TabItem value="Docblock">
143+
144+
145+
```tsx
146+
/**
147+
* @RelayResolver BasicUser implements IUser
148+
*/
149+
export function BasicUser(id: DataID): BasicUserModel {
150+
return { ...BasicUserService.getById(id), name: 'BasicUser1'};
151+
}
152+
153+
/**
154+
* @RelayResolver SpecialUser implements IUser
155+
*/
156+
export function SpecialUser(id: DataID): SpecialUserModel {
157+
return { ...SpecialUserService.getById(id), name: 'SpecalUser1'};
158+
}
159+
```
160+
161+
</TabItem>
162+
</Tabs>

website/versioned_docs/version-v18.0.0/guides/relay-resolvers/limitations.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ Relay Resolvers are do have some limitations. Here we will collect a list of kno
1111

1212
In a full GraphQL implementation, resolvers would have access to an `info` argument. This argument is not available in Relay Resolvers today.
1313

14-
## No support for abstract types
15-
16-
Today it is not possible to define an interface or union with multiple concrete types using Relay Resolvers. This is something we would like to support in the future, but have not yet implemented.
17-
1814
## All fields must be nullable
1915

2016
Today all resolvers must be typed as nullable in order to support coercing errors to null without having to implement null bubbling. In the future we intend Resolvers to support some version of [strict semantic nullability](https://github.com/graphql/graphql-wg/discussions/1410).

0 commit comments

Comments
 (0)