I want to link an custom GraphQL type from frontmatter to a team member with #link
{
"members": [
{
"name": "String",
"id": "String",
...
}
]
}
In the frontmatter, I have a authors array field with the team member id
type Author {
name: String
positon: String
}
type Frontmatter {
title: String
authors: [TeamJson] #link(by: "members.id" )
}
Related
I am trying to order an array returned by resolver in GraphQL.
I can only think of way of ordering data using DB Query, but how can we order data that GraphQL resolves using its resolver functions?
Below is my Query resolver:
getAllNotifications: (
_parent,
_args: { authorId: string },
context: Context
) => {
return context.prisma.blog.findMany({
where: {
authorId: _args.authorId,
},
orderBy: {
created_at: "desc",
},
});
},
And my query:
query Query($authorId: String) {
getAllNotifications(authorId: $authorId) {
title
comment {
id
created_at
}
}
}
And result:
{
"data": {
"getAllNotifications": [
{
"title": "First Ride! ",
"comment": [ <--- I am trying to order this comment array using created_at date
{
"id": "cl8afqaqx001209jtxx7mt5h6",
"created_at": "2022-09-20T16:53:07.689Z"
},
{
"id": "cl8agiq71001509l27or7oxyd",
"created_at": "2022-09-20T17:15:14.077Z"
},
{
"id": "cl8ahmvrm003109l8dp684bn6",
"created_at": "2022-09-20T17:46:27.538Z"
},
{
"id": "cl99kj24p002609iajdbpycg0",
"created_at": "2022-10-15T06:59:24.169Z"
}
]
}
]
}
}
I didn't found anything in Graphql docs regarding ordering data returned by resolver
GraphQL doesn't have functions for selection and ordering, those are jobs that resolvers must do.
Add a resolver for comment that sorts the comment array. Your existing sort just sorts based on the created_at date of the post.
I'm here with a problem with rich queries and convector chaincodes, everything works with mango queries, but when I pass content object it's is stringifyed and don't will be sent has an object, but is converted to a string "content":"{\"data\":\"1971\"}", obvious it fails the query
original sample query
{
"selector": {
"type": "io.worldsibu.examples.person",
"attributes": {
"$elemMatch": {
"id": "born-year",
"content": {
"data": "1971"
}
}
}
}
}
graphql query variables
{
"getByAttributeInput": {
"id": "born-year",
"content": {
"data": "1971"
}
},
"skip": 0,
"take": 10
}
packages/person-cc/src/person.controller.ts
chaincode controller method
#Invokable()
public async getByAttribute(
#Param(yup.string())
id: string,
#Param(yup.mixed())
value: any
) {
return await Person.query(Person, {
selector: {
type: c.CONVECTOR_MODEL_PATH_PERSON,
attributes: {
$elemMatch: {
id: id,
content: value
}
}
}
});
}
in docker logs we can view that value is content is sent has a string and not a object ex "content":"{\"data\":\"1971\"}"
{"selector":{"type":"io.worldsibu.examples.person","attributes":{"$elemMatch":{"id":"born-year","content":"{\"data\":\"1971\"}"}}}}
the trick is change #Param(yup.mixed()) to #Param(yup.object()) and now it works has expected, we can query attributes content value with arbitrary and complex objects
#Invokable()
public async getByAttribute(
#Param(yup.string())
id: string,
// #Param(yup.mixed())
#Param(yup.object())
value: any
) {
...
I am using Express + Apollo Server + GraphQL + Mongoose + MongoDB to "perform" several CRUD operations on a database.
One of the operations I am trying to make is to get the sites from the database and expand its users with their information for each record like this:
query {
getSites {
id
name
owner {
name
email
}
origins
}
}
Instead, I am getting these results:
{
"data": {
"getSites": [{
"id": "5cae36182ab9b94e94ba9af5",
"name": "Test site 1",
"owner": [{
"name": null,
"email": null
}
],
"origins": [
"test1",
"test2"
]
}, {
"id": "5cae3a3798c302247c036544",
"name": "Test site 2",
"owner": [{
"name": null,
"email": null
}
],
"origins": [
"test1",
"test2"
]
}
]
}
}
This is my typeDef code for Site:
import { gql } from 'apollo-server-express';
const site = gql `
extend type Site {
id: ID!
name: String!
origins: [String]
owner: [User]
createdOn: String
updatedOn: String
}
extend type Query {
getSites: [Site]
getSite(id: ID!): Site
}
extend type Mutation {
addSite(name: String!, owner: [String!], origins: [String]): Site
}
`;
export default site;
If I console.log(sites) I see owner is an array of Strings.
Edit:
If I change addSite(name: String!, owner: [User], origins: [String]): Site then I get when compiling:
Error: The type of Mutation.addSite(owner:) must be Input Type but got: [User]
My resolver looks like this:
getSites: async () => await Site.find().exec()
What's the proper way to define relationships today? Thanks.
I just edited my resolver to this:
getSites: async () => {
let sites = await Site.find().exec();
let ownedSites = await User.populate(sites, { path: 'owner' });
return ownedSites;
}
And that solved the errors.
How do I create a Mutation with arguments for a resolver defined in graphql-yoga as:
const resolvers =
Mutation: {
createProject(root, args) {
const id = (Number(last(data.projects).id) + 1).toString()
const newProject = { ...args, id: id }
...
I've tried the following:
mutation CreateProject($name: String!) {
createProject {
data: {
name: $name
}
}
}
and
mutation CreateProject($name: String!) {
createProject($name: name) {
statusCode
}
}
which produces
and various other structures unsuccessfully.
There seems to be no reference to a Mutation in either the project README or any of the three examples.
Update
I'm now using:
mutation CreateProject($name: String!) {
createProject(name: $name) {
id
name
}
}
which is so similar to examples I've seen on the net that I feel it must be valid & the syntax is not rejected.
The schema definition is:
scalar ID
type Project {
id: ID
type: ProjectType
name: String
}
interface MutationResult {
statusCode: Int
message: String
}
type ProjectMutationResult implements MutationResult {
statusCode: Int
message: String
project: Project
}
type Mutation {
createProject: ProjectMutationResult
}
However on submitting the mutation, I receive:
{
"error": {
"errors": [
{
"message": "Unknown argument \"name\" on field \"createProject\" of type \"Mutation\".",
"locations": [
{
"line": 2,
"column": 17
}
]
},
{
"message": "Cannot query field \"id\" on type \"ProjectMutationResult\".",
"locations": [
{
"line": 3,
"column": 5
}
]
},
{
"message": "Cannot query field \"name\" on type \"ProjectMutationResult\".",
"locations": [
{
"line": 4,
"column": 5
}
]
}
]
}
}
According to your type definition:
The createProject mutation does not expect any argument:
type Mutation {
createProject: ProjectMutationResult
}
The ProjectMutationResult type does not have an id field nor a name field:
type ProjectMutationResult implements MutationResult {
statusCode: Int
message: String
project: Project
}
So when you run the mutation:
mutation CreateProject($name: String!) {
createProject(name: $name) {
id
name
}
}
you have a complete discrepancy between what you're feeding your GraphQL server and what it's actually expecting.
So first of all, if you want to be able to set a name to your project when you create it, you need to amend your createProject definition to this:
type Mutation {
createProject(name: String!): ProjectMutationResult
}
(if you want the naming to be optional, set name to be of type String rather than String!)
Then, assuming you want to retrieve the newly created project id and name from your mutation, change the mutation itself to:
mutation CreateProject($name: String!) {
createProject(name: $name) {
project {
id
name
}
}
}
You need to do this because your createProject mutation returns a ProjectMutationResult which itself contains a project field of type Project, which is the one defining the id and name fields.
I have a question. How to create a dynamic table with vue js.
I want to render this json file into the table using Vue but it doesn't happen as I want. I want the data in two languages and app_adi but only the latest data is coming. How can I display both?
json file
{
"accounts":{
"user":{
"_id":"5a500vlflg0aslf011ld0a25a5",
"username":"john",
"id":"59d25992988fsaj19fe31d7",
"name":"Test",
"customer":" John Carew",
},
"application":[
{
"app_id":"5af56pi314-y1i96kdnqs871nih35",
"language":"es"
},
{
"app_id":"5af56pi314-blvinpgn4c95ywyt8j",
"language":"en"
}
]
}
}
I want to build this table:
username customer language app_di
john John Carew es 5af56pi314-y1i96kdnqs871nih35
en 5af56pi314-blvinpgn4c95ywyt8j
Preprocess your json in a computed property.
In your example you just need to add the "user" properties to the first "application" item.
new Vue({
el: '#app',
data() {
return {
columns: {
username: 'Name',
customer: 'Customer',
language: 'Language',
app_id: 'App_ID'
},
userData: {
"user": {
"_id": "5a500vlflg0aslf011ld0a25a5",
"username": "john",
"id": "59d25992988fsaj19fe31d7",
"name": "Test",
"customer": " John Carew",
},
"application": [{
"app_id": "5af56pi314-y1i96kdnqs871nih35",
"language": "es"
},
{
"app_id": "5af56pi314-blvinpgn4c95ywyt8j",
"language": "en"
}
]
}
}
},
computed: {
tableData() {
return this.userData.application.map((x, index) => {
return index === 0 ? Object.assign(x, this.userData.user) : x
})
}
}
})
Here is a working example: https://jsfiddle.net/ellisdod/jm3snwxc/2/