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

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.

主站蜘蛛池模板: 长武县| 武功县| 仙桃市| 浦东新区| 安平县| 任丘市| 游戏| 新绛县| 苗栗县| 武陟县| 金川县| 浑源县| 三台县| 麻城市| 泰州市| 乐亭县| 望谟县| 诸暨市| 内江市| 葵青区| 鄄城县| 民乐县| 武强县| 哈巴河县| 宝兴县| 双辽市| 藁城市| 陆河县| 岳普湖县| 正定县| 三穗县| 克什克腾旗| 大渡口区| 友谊县| 白沙| 柳林县| 古交市| 天水市| 高雄县| 卢龙县| 庆云县|