Fields
The examples on this page use the db.user and db.post tables from Relations.
Choose the exposed fields
To expose only some fields of a type, list them in fields:
const graph = r.graph({
User: r.table(db.user, {
fields: {
id: true,
name: true,
posts: true,
},
}),
Post: r.table(db.post),
})true resolves the field from the column or relation of the same name. The graph rejects a query for any field that is not listed, such as a password column that the GraphQL schema declares.
Which fields a type resolves depends on its options:
- By default, a type without
fieldsresolves every field of the GraphQL type from the column or relation of the same name. - With
fields, and withoutallowAllFields: true, the type resolves only the listed fields. - With
allowAllFields: trueon the type or on the graph, the type resolves every field, andfieldsonly overrides how the listed ones resolve.allowAllFields: falseon a type restores itsfieldslist underallowAllFields: trueof the graph.
const graph = r.graph({
User: r.table(db.user, {
// Every field, with name overridden.
allowAllFields: true,
fields: { name: r.field({ transform: name => name.toUpperCase() }) },
}),
Post: r.table(db.post),
})Rename a field
When the GraphQL field has a different name than the column, map it to the column:
const graph = r.graph({
Post: r.table(db.post, {
fields: {
id: true,
body: "text",
},
}),
})With a concrete table such as db.post, TypeScript checks string literals like "text" against the table's columns, relations, and the names in modifiers. If you declare the mapping separately, use as const to keep the source names as literals. For a typed GraphQL context, pass Context to r.graph<Context> so r.table can infer the table.
To combine the new name with other options, pass the column as tableField of r.field:
body: r.field({ tableField: "text", transform: text => text.trim() })Compute a field in SQL
To compute a field in the database, pass a function that selects it under the name of the GraphQL field:
const graph = r.graph({
Post: r.table(db.post, {
fields: {
id: true,
text: true,
upper_text: query => query.select({ upper_text: query.sql<string>`upper(text)` }),
},
}),
})The function runs only when the client selects the field.
Post-process a value
To change a selected value before the response, pass transform(value, instance, context) to r.field. Combine it with modify, which selects the value instead of the column, to post-process a value computed in SQL:
const graph = r.graph({
Post: r.table(db.post, {
fields: {
id: true,
preview: r.field({
// One character more than the preview shows, to tell whether the text is longer.
modify: query => query.select({ preview: query.sql<string>`substr(text, 1, 101)` }),
transform: preview => (preview.length > 100 ? preview.slice(0, 100) + "…" : preview),
}),
},
}),
})transform may return a promise. instance is the object with its other selected columns, and the context property of the third argument holds the GraphQL context.
Currently broken
transform is not applied when the type is loaded through a relation, such as user { posts { preview } }.