Radosław Miernik
Open source? Embrace, understand, develop.
type Post {
_id: ID!
views: Int!
}
type User {
_id: ID!
name: String!
posts: [Post!]!
totalViews: Int!
}
type Query {
users: [User!]!
}
User.totalViews
Post.views
export const Query = {
users: async () =>
await getUsers()
};
export const User = {
posts: async ({ _id }) =>
await getPostsOf(_id),
totalViews: async ({ _id }) =>
sumViews(await getPostsOf(_id))
};
export const Query = {
users: async () =>
await getUsersWithTotalViews()
};
export const User = {
posts: async ({ _id }) =>
await getPostsOf(_id)
};
query {
users {
_id
}
}
export const Query = {
users: async (_, _, _, info) =>
includes(info, 'totalViews')
? await getUsersWithTotalViews()
: await getUsers()
};
export const User = {
posts: async ({ _id }) =>
await getPostsOf(_id)
};
type Post {
_id: ID!
likes: Int!
reach: Int!
views: Int!
}
type User {
_id: ID!
name: String!
posts: [Post!]!
totalLikes: Int!
totalReach: Int!
totalViews: Int!
}
type Query {
users: [User!]!
}
User.total*
Post.*
export const Query = {
users: async (_, _, _, info) =>
includes(info, 'totalLikes')
? includes(info, 'totalReach')
? includes(info, 'totalViews')
? await getUsersWithLRV()
: await getUsersWithLR()
: includes(info, 'totalViews')
? await getUsersWithLV()
: await getUsersWithL()
: includes(info, 'totalReach')
? includes(info, 'totalViews')
? await getUsersWithRV()
: await getUsersWithR()
: includes(info, 'totalViews')
? await getUsersWithV()
: await getUsers()
};
import { astToOptions, astToPipeline }
from 'sparrowql-graphql';
const prepareOptions = () =>
// Use astToOptions!
// See docs!
export const Query = {
users: async (_, _, _, info) => {
const pipeline = astToPipeline(
info,
prepareOptions()
);
return await Users
.aggregate(pipeline)
.toArray();
}
};
FAQ
FAA
By Radosław Miernik
GraphQL Wroclaw #4