如何定义包含复杂对象的 GraphQL 类型模型?
How to define a GraphQL type model that contains complex objects?
我正在使用 Facebook Graph API 的数据来获取用户信息并在 GraphQL 服务 (graphcool) 中创建我的用户。
function createGraphcoolUser(facebookUser) {
return api.request(`
mutation {
createUser(
facebookUserId: "${facebookUser.id}"
facebookEmail: "${facebookUser.email}"
facebookName: "${facebookUser.name}"
facebookPicture: "${facebookUser.picture}"
) {
id
}
}`)
.then((userMutationResult) => {
return userMutationResult.createUser.id
})
}
但是 ${facebookUser.picture}
是一个具有嵌套字段的对象。
{
"id": "123467890",
"email": "my@email.ca",
"name": "John Doe",
"picture": {
"data": {
"url": "https://url.to.picture.jpg"
}
}
}
那么如何在类型模型中定义它呢?
type User @model {
# Required system field:
id: ID! @isUnique # read-only (managed by Graphcool)
# Optional system fields (remove if not needed):
createdAt: DateTime! # read-only (managed by Graphcool)
updatedAt: DateTime! # read-only (managed by Graphcool)
facebookUserId: String @isUnique
facebookEmail: String
facebookName: String
facebookPicture: ---> HERE <---
posts: [Post!]! @relation(name: "UserPosts")
}
如果您需要 facebookPicture
作为类型,我认为有几种方法可以做到。看起来 data
对象只是一个包装器,所以它可以帮助我们把整个东西压平。
type User @model {
id: ID! @isUnique
facebookUserId: String! @isUnique # unique field should be required
facebookEmail: String
facebookName: String
facebookPicture: FacebookPicture @relation(name: "UserPicture")
posts: [Post!]! @relation(name: "UserPosts")
}
type FacebookPicture @model {
id: ID! @isUnique
user: User! @relation(name: "UserPicture")
url: String!
}
在 Graphcool 1.0 之前,它将强制关系变为双向关系,因此您还必须处理图像中的 user
字段。 1.0最终推出后,这可能会变得更简单。
为了给用户添加图片,你可以(应该可以)使用nested mutation。类似于:
mutation createUserAndFacebookPicture { # this mutation should be generated by Graphcool
createUser(
# user stuff...
facebookPicture: {
url: "${facebookUser.picture.data.url}"
}
) {
id
facebookPicture {
id
url
}
}
}
如果 facebookUser.picture.data
更复杂,而不仅仅是包装器,那么您可以制作一个 data
字段,并将其设为 JSON
类型。
为了回答我自己的问题,信息在文档中(对此感到抱歉):
我们必须使用输入类型
... you can also easily pass complex objects. This is particularly
valuable in the case of mutations, where you might want to pass in a
whole object to be created. In the GraphQL schema language, input
types look exactly the same as regular object types, but with the
keyword input instead of type ...
我正在使用 Facebook Graph API 的数据来获取用户信息并在 GraphQL 服务 (graphcool) 中创建我的用户。
function createGraphcoolUser(facebookUser) {
return api.request(`
mutation {
createUser(
facebookUserId: "${facebookUser.id}"
facebookEmail: "${facebookUser.email}"
facebookName: "${facebookUser.name}"
facebookPicture: "${facebookUser.picture}"
) {
id
}
}`)
.then((userMutationResult) => {
return userMutationResult.createUser.id
})
}
但是 ${facebookUser.picture}
是一个具有嵌套字段的对象。
{
"id": "123467890",
"email": "my@email.ca",
"name": "John Doe",
"picture": {
"data": {
"url": "https://url.to.picture.jpg"
}
}
}
那么如何在类型模型中定义它呢?
type User @model {
# Required system field:
id: ID! @isUnique # read-only (managed by Graphcool)
# Optional system fields (remove if not needed):
createdAt: DateTime! # read-only (managed by Graphcool)
updatedAt: DateTime! # read-only (managed by Graphcool)
facebookUserId: String @isUnique
facebookEmail: String
facebookName: String
facebookPicture: ---> HERE <---
posts: [Post!]! @relation(name: "UserPosts")
}
如果您需要 facebookPicture
作为类型,我认为有几种方法可以做到。看起来 data
对象只是一个包装器,所以它可以帮助我们把整个东西压平。
type User @model {
id: ID! @isUnique
facebookUserId: String! @isUnique # unique field should be required
facebookEmail: String
facebookName: String
facebookPicture: FacebookPicture @relation(name: "UserPicture")
posts: [Post!]! @relation(name: "UserPosts")
}
type FacebookPicture @model {
id: ID! @isUnique
user: User! @relation(name: "UserPicture")
url: String!
}
在 Graphcool 1.0 之前,它将强制关系变为双向关系,因此您还必须处理图像中的 user
字段。 1.0最终推出后,这可能会变得更简单。
为了给用户添加图片,你可以(应该可以)使用nested mutation。类似于:
mutation createUserAndFacebookPicture { # this mutation should be generated by Graphcool
createUser(
# user stuff...
facebookPicture: {
url: "${facebookUser.picture.data.url}"
}
) {
id
facebookPicture {
id
url
}
}
}
如果 facebookUser.picture.data
更复杂,而不仅仅是包装器,那么您可以制作一个 data
字段,并将其设为 JSON
类型。
为了回答我自己的问题,信息在文档中(对此感到抱歉):
我们必须使用输入类型
... you can also easily pass complex objects. This is particularly valuable in the case of mutations, where you might want to pass in a whole object to be created. In the GraphQL schema language, input types look exactly the same as regular object types, but with the keyword input instead of type ...