I am using the Facebook Graph API’s data to get the user info and create my user in the GraphQL service (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
})
}
But ${facebookUser.picture}
is an object with nested fields.
{
"id": "123467890",
"email": "[email protected]",
"name": "John Doe",
"picture": {
"data": {
"url": "https://url.to.picture.jpg"
}
}
}
So how do I define it in the type model ?
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")
}
2
Answers
To answer my own question the info was in the docs (sorry about that):
We must use Input types
http://graphql.org/learn/schema/#input-types
If you need the
facebookPicture
to be a type, I think there’s a couple ways to do it. It appears that thedata
object is just a wrapper, so it could help us to flatten the whole thing down.Prior to Graphcool 1.0, it will force the relationship into being two-way, so you will have to handle having a
user
field in the image as well. This might become a bit simpler after 1.0 is finally rolled out.In order to add the image to the user, you can (should be able to) use a nested mutation. Something like:
If there the
facebookUser.picture.data
is more complicated and not just a wrapper, then you could make adata
field, and make that aJSON
type.