Trying to implement join but always getting null.
type User {
id: Int!
username: String!
recipes: [Recipe]
}
type Recipe {
id: Int!
title: String!
author: User
}
So basically i want to get data something like this:
User {
username,
recipes: [{//recipe}, {//recipe}]
}
And for Recipe i expecting
Recipe {
title,
author: {//user}
}
So i have query like below, and i want to get all recipes from database with included user
type Query {
recipes: [Recipe!]!
}
Here is my GraphiQL query
{
recipes {
id,
author {
id,
username
}
}
}
But in response i have author: null
{
"data": {
"recipes": [
{
"id": 1,
"author": null
}]
}
}
Any suggestions? Thanks!
Maybe somebody will face with similar issue.
Have fixed this. As said #Daniel Rearden in comments - yes issue was in resolver.
So you have to add this fields to resolver:
const resolverFunctions = {
User: {
recipes(author) {
return author.getRecipes();
}
},
Recipe: {
author(recipe) {
return recipe.getUser();
}
}
}
After you will get data what i needed above.
Related
I am writing a graphql api endpoint where I have customer details as below.
{
customer_name:
customer_address:
[
{
address_1:
},
{
address_2:
},
]
}
I need to apply pagination on customer_address which is a list.
Is this possible? Or I can do it only at top level record? Please let me know what would be the best way to do it?
You can possible by using resolver like following
input PageInput{
limit: Int!
page: Int!
}
type CustomerAddressPage {
totalCount: Int!
edges: [CustomerAddress!]!
}
type CustomerAddress {
address: String
}
type Customer {
customerName: String
customerAddress(input: PageInput!): CustomerAddressPage
}
I don't know what kind of framework you use, in nestjs you can be done as follows.
#Resolver(() => Customer)
export class CustomerResolver {
#ResolveField(() => CustomerAddressPage)
customerAddress(
#Parent() customer: Customer,
#Args('input') input: PageInput,
): Promise<CustomerAddressPage> {
return {
};
}
}
Hi all I have a query where I am trying to get messages from a user with a specific uuid or a role that matches the users role. I am unsure of how to properly use the _ilike or % in this instance. I've tried a myriad of combinations yet the role messages never get returned. My query as it sits now and the hook used in the component are below.
I appreciate any feedback.
Here is my query
query getUserMessages($userId: String!) {
messageReceivers(
where: { _or: [{ userId: { _eq: $userId } }, { message: { roles: { _ilike: "%" } } }] }
) {
messageId
userId
message {
id
audioLink
body
videoLink
user {
firstName
lastName
photo
title
specialty
profession
location
}
}
}
}
Using the lazyquery hook in component
const [getUserMessages, { error, called, loading, data }] = useGetUserMessagesLazyQuery()
const userRole = `%${user.role}%`
useEffect(() => {
getUserMessages({
variables: { userId: user?.id, message: { roles: { _ilike: userRole } } },
})
}, [user])
You are incorrectly passing userRole to the query. To fix it, apply userId's pattern to userRole.
In the query definition, add $userRole in the operation signature (You are currently hardcoding _ilike to % in the query, but you want set it dynamically as $userRole).
In the calling function, send the variables correctly variables: { userId: user?.id, userRole: userRole}.
The GraphQL Variable docs neatly describe how this fits together.
Thanks #fedonev! Though I didn't see your solution you were absolutely correct. I was able to work it out a little differently and I hope this helps someone who's run into the same issue.
By creating the variable $role in the query I was able to use the same syntax as was being used by the userId variable. If anyone has this issue please feel free to comment I will happily help if I can.
Query
query getUserMessages($userId: String!, $role: String = "%") {
messages(
where: {
_or: [{ roles: { _ilike: $role } }, { messageReceivers: { userId: { _eq: $userId } } }]
}
order_by: { createdAt: desc }
) {
createdAt
id
body
audioLink
videoLink
roles
}
Call from in component
useEffect(() => {
getUserMessages({
variables: { userId: user?.id, role: user?.role },
})
}, [user])
I want to create a GraphQL connection between User <> Role <> Role_Type and finally get the User Role Type back with a query. Here is brake it down to the only important lines of code:
type Query {
getUsers: [User]
}
type User {
_id: ID
firstname: String
roles: [Role]
}
type Role {
_id: ID
role_type_id: ID
role_types: [Role_Type]
user_id: ID
}
type Role_Type {
_id: ID
name: String
}
and in the User resolver I have:
Query: {
getUser: async (root, { _id }) => {
return prepare(await DBUsers.findOne(ObjectId(_id)))
}
},
User: {
roles: async ({_id}) => {
return (await MongoDBRoles.find({user_id: _id}).toArray()).map(prepare)
}
}
and for the Role resolver:
Role: {
role_types: async ({_id}) => {
return (await MongoDBRoleTypes.find({role_type_id: _id}).toArray()).map(prepare)
},
},
When I query now with:
{
getUser(_id: "5d555adcd2c22a242863f7a1") {
firstname
roles {
_id
role_type_id
user_id
role_types {
name
}
}
}
}
I get:
{
"data": {
"getUser": {
"firstname": "Gregor",
"roles": [
{
"_id": "5d90cf352f50882ab0ce3877",
"role_type_id": "5d90ce48b7893d19bcc328f9",
"user_id": "5d555adcd2c22a242863f7a1",
"role_types": []
}
]
}
}
}
But why is role_types empty. As you can see the role_type_id is filled. So why there is no connection.
When I watch into MongoDB I can see the Role Type of the user.
If you need more Schema/Resolver let me know.
Ok it was kind of easy to fix. Actually the Role resolver was going multiple matches. But somehow it needs to be single lookup. That does not make much sense but somehow the search for the id lookup is a single lookup. Anyway here is what I have changed and now it works properly.
Replace this:
role_types: async ({_id}) => {
return (await MongoDBRoleTypes.find({role_type_id: _id}).toArray()).map(prepare)
},
with:
role_type: async ({role_type_id}) => {
return prepare(await DBRoleTypes.findOne(ObjectId(role_type_id)))
},
and you fix this.
I have a case that I'm not sure to implement, I have a REST API that returns an array of user IDs. Like this:
{
status: ""
"users": [
{
"userId": ID
},
{
"userId": ID
}
]
}
I want to have the ability to get the user details for each userID, so I want to pass the ID to another API that gets the user details.
The issue is that the second API returns the information as follows
{
status: ""
data:[{
Info:{
userId:
name:
...
}
Permissions{
...
}
...
}]
}
My issue is how can I represent this relation in the schema?
This is the schema I came up with:
Query{
EventUsersSummary(eventId: ID): EventResponse!
}
type EventResponse{
status: String!
users: [EventUser!]!
}
type EventUser {
userId: ID!
}
type User {
info: Info
permissions: Permissions
....
}
type Info{
userId: ID
firstName: String
lastName: String
email: Email
}
....
In the resolver for EventResponse I'm removing status from the result. In the resolver I plan to return the information for each user.
Using the makeExecutableSchema with the following Query definition:
# Interface for simple presence in front-end.
type AccountType {
email: Email!
firstName: String!
lastName: String!
}
# The Root Query
type Query {
# Get's the account per ID or with an authToken.
getAccount(
email: Email
) : AccountType!
}
schema {
query: Query
}
And the following resolver:
export default {
Query: {
async getAccount(_, {email}, { authToken }) {
/**
* Authentication
*/
//const user = security.requireAuth(authToken)
/**
* Resolution
*/
const account = await accounts.find({email})
if (account.length !== 1) {
throw new GraphQLError('No account was found with the given email.', GraphQLError.codes.GRAPHQL_NOT_FOUND)
}
return account
}
}
}
When I query with:
query {
getAccount(email: "test#testing.com") {
firstName
lastName
}
}
I am getting the following result in GraphiQL:
{
"data": {
"getAccount": {
"firstName": "John",
"lastName": "Doe"
}
}
}
So, any reason I am getting this "getAccount" back in the result?
Because getAccount is not a query name. It's just a regular field on the root query type Query.
And having results on the exact same shape as the query is one of the core design principles of GraphQL:
Screenshot from http://graphql.org/ site
Query name in GraphQL goes after query keyword:
query myQueryName {
getAccount(email: "test#testing.com") {
firstName
lastName
}
}