官术网_书友最值得收藏!

Using multiples types in GraphQL schemas

Let's create a User type and use it with our posts. First, add it somewhere to the schema:

type User {
avatar: String
username: String
}

Now that we have a User type, we need to use it inside the Post type. Add it to the Post type as follows:

user: User

The user field allows us to have a sub-object inside our posts with the post's author information.

Our extended query to test this looks like the following:

"query":"{
posts {
id
text
user {
avatar
username
}
}
}"

You cannot just specify the user as a property of the query. Instead, you need to provide a sub-selection of fields. This is required whenever you have multiple GraphQL types stacked inside each other. Then, you need to select the fields your result should contain.

Running the updated query gives us the fake data, which we already have in our front end code; just the posts array as it is.

We have made good progress with querying data, but we also want to be able to add and change data.

主站蜘蛛池模板: 凉山| 万全县| 仪征市| 辽阳市| 刚察县| 高邮市| 鄂尔多斯市| 六安市| 普定县| 信丰县| 长治市| 荥经县| 揭阳市| 娄底市| 克山县| 荣成市| 河南省| 叙永县| 安远县| 荆门市| 安仁县| 大邑县| 勃利县| 江北区| 松阳县| 介休市| 乐东| 临洮县| 平潭县| 滦南县| 安徽省| 吉隆县| 炎陵县| 海林市| 东方市| 东丽区| 仁化县| 裕民县| 肥城市| 三穗县| 黑山县|