@@ -52,6 +52,7 @@ By default all attributes on postgres types are nullable, and so a function that
52
52
returns a type instead of a table will have all its fields treated as such by the
53
53
generated GraphQL schema. This can be over-ridden on an attribute-by-attribute basis
54
54
with the ` @notNull ` [ smart tag] ( ./smart-tags ) .
55
+
55
56
:::
56
57
57
58
### Example
@@ -91,40 +92,22 @@ create function search_posts(search text)
91
92
$$ language sql stable;
92
93
```
93
94
94
- Or using a type to return a subset of the columns on the table:
95
- ```
96
- -- Columns unnecessary to this demo were omitted. You can find the full table in
97
- -- our forum example.
98
- create table post (
99
- …
100
- headline text not null,
101
- body text,
102
- …
103
- );
95
+ Or using a type to return the exact shape of data you need:
104
96
97
+ ``` sql {5}
105
98
create type post_search_result as (
106
99
headline text ,
107
100
body text
108
101
);
109
102
comment on column post_search_result.headline is E' @notNull' ;
110
103
111
- -- Create the function named `search_posts` with a text argument named `search`.
112
- -- This will expose `Query.searchPosts(search: String!, ...)` to GraphQL.
113
104
create function search_posts (search text )
114
- -- This function will return a set of posts from the `post` table. The
115
- -- `setof` part is important to PostGraphile, check out our Functions article
116
- -- to learn why.
117
105
returns setof post_search_result as $$
118
- -- Write our advanced query as a SQL query!
119
106
select headline, body
120
107
from post
121
108
where
122
- -- Use the `ILIKE` operator on both the `headline` and `body` columns. If
123
- -- either return true, return the post.
124
109
headline ilike (' %' || search || ' %' ) or
125
110
body ilike (' %' || search || ' %' )
126
- -- End the function declaring the language we used as SQL and add the
127
- -- `STABLE` marker so PostGraphile knows its a query and not a mutation.
128
111
$$ language sql stable;
129
112
```
130
113
0 commit comments