Gridsome source-filesystem add tag description - graphql

I'm using #gridsome/source-filesystem with this config:
{
use: '#gridsome/source-filesystem',
options: {
typeName: 'Post',
path: 'content/posts/**/*.md',
refs: {
tags: {
typeName: 'Tag',
create: true
},
author: {
typeName: 'Author',
create: true
}
}
},
}
Now I want to add description for one tag only, so I created a new doc in content/posts/my-tag.md:
---
title: Tag-Title
description:tag description
---
How can I connect this document to the allTags collection?
Or any other way (without #gridsome/source-filesystem for Tags, for example), to add description to exists node in collection?

If you want to just add allTags, you can create markdown for it.
in gridsome.config.js
...
{
use: '#gridsome/source-filesystem',
options: {
path: 'content/tags/**/*.md',
typeName: 'Tag'
},
}
...
add file content/tags/my-tag.md
---
title: Tag-Title
description: tag description
---
you can explole
{
allTag {
edges {
node {
id
title
description
}
}
}
}
{
"data": {
"allTag": {
"edges": [
{
"node": {
"id": "******", // random hash
"title": "Tag-Title",
"description": "tag description"
}
},
{
"node": {
"id": "foo",
"title": "foo",
"description": ""
}
},
...
but, this is not able to connect to your Post.
or just added description to Tag, you can use addSchemaResolvers.
in gridsome.server.js
module.exports = function(api) {
api.loadSource(async ({ addSchemaResolvers }) => {
addSchemaResolvers({
Tag: {
description: {
type: "String",
resolve(obj) {
if (!obj.description) {
// coding your logic
return "set description";
} else {
return obj.description;
}
}
}
}
});
});
};

Related

How to mutate a list of objects in an array as an argument in GraphQL completely

I cannot mutate a list of objects completely, because only the last element of the array will be mutated.
What already works perfectly is, if I put each element ({play_positions_id: ...}) in the array manually like here:
mutation CreateProfile {
__typename
create_profiles_item(data: {status: "draft", play_positions: [{play_positions_id: {id: "1"}}, {play_positions_id: {id: "2"}}]}) {
id
status
play_positions {
play_positions_id {
abbreviation
name
}
}
}
}
Output:
{
"data": {
"__typename": "Mutation",
"create_profiles_item": {
"id": "1337",
"status": "draft",
"play_positions": [
{
"play_positions_id": {
"id": "1",
"abbreviation": "RWB",
"name": "Right Wingback"
}
},
{
"play_positions_id": {
"id": "2",
"abbreviation": "CAM",
"name": "Central Attacking Midfielder"
}
}
],
}
}
}
Since you can add many of those elements, I defined a variable/argument like here
mutation CreateProfile2($cpppi: [create_profiles_play_positions_input]) {
__typename
create_profiles_item(data: {status: "draft", play_positions: $cpppi}) {
id
status
play_positions {
play_positions_id {
id
abbreviation
name
}
}
}
}
Variable object for above:
"cpppi": {
"play_positions_id": {
"id": "1"
},
"play_positions_id": {
"id": "2
}
}
Output:
{
"data": {
"__typename": "Mutation",
"create_profiles_item": {
"id": "1338",
"play_positions": [
{
"play_positions_id": {
"id": "2",
"abbreviation": "CAM",
"name": "Central Attacking Midfielder"
}
}
],
}
}
}
Schema:
input create_profiles_input {
id: ID
status: String!
play_positions: [create_profiles_play_positions_input]
}
input create_profiles_play_positions_input {
id: ID
play_positions_id: create_play_positions_input
}
input create_play_positions_input {
id: ID
abbreviation: String
name: String
}
At the last both snippets, only the last object with the id "2" will be mutated. I need these to use the defined input type from my backend.
I figured it out. I got it wrong with the brackets in the variable. Here the solution:
"cpppi": [
{
"play_positions_id": {
"id": "1"
}
},
{
"play_positions_id": {
"id": "2"
}
}
]

Why is my graphql query return all product?

I'm testing graphql query on Shopify GraphiQL Explorer for filtering products on Storefront API.
My query is like this:
query ProductType {
collectionByHandle(handle: "pants") {
handle
products(first: 10, filters: {
productType: "pants"
}) {
edges {
node {
handle
productType
}
}
}
}
}
And got the result like this:
{
"data": {
"collectionByHandle": {
"handle": "pants",
"products": {
"edges": [
{
"node": {
"handle": "my-test-product-pants",
"productType": "pants"
}
},
{
"node": {
"handle": "pleated-straight-pants-mens-fashion-elastic-waist-casual-pants-men-streetwear-loose-ice-silk-trousers-mens-wide-leg-pants-s-2xl",
"productType": ""
}
},
...
Basically, the result has all the products that I have for that collection. Can anybody help me with this? This is literally the code I got from shopify website
As we can see in the tutorial filters is an array
{
"product_filters": [
{
"productType": "shoes"
},
{
"productVendor": "bestshop"
},
{
"variantOption": {
"name": "color",
"value": "blue"
}
}
]
}
So, try this instead
query ProductType {
collectionByHandle(handle: "pants") {
handle
products(first:10, filters: [{ productType: "pants" ]}) {
...
}
}
}

shorten the graphql field value

I am trying to fetch image path in an alias field using graphql, I am able to get output like this:
Output:
{
"data": {
"leaders": [
{
"partyImg": {
"image": {
"url": "/uploads/17a5f020cc974679ac52e56a22b74dd6.png"
}
}
},
{
"partyImg": {
"image": {
"url": "/uploads/70bd673d41654058847e39c14cda5fef.png"
}
}
},
{
"partyImg": {
"image": {
"url": "/uploads/c54a0ace0bb34da3985c67945b1d0bf0.png"
}
}
}
]
}
}
When I used the following graphql code:
Input:
query Leaders{
leaders{
partyImg: party{image: Image{ url }},
}
}
The output I am trying to get is:
Expected output:
{
"data": {
"leaders": [
{
"partyImg": "/uploads/17a5f020cc974679ac52e56a22b74dd6.png"
},
{
"partyImg": "/uploads/70bd673d41654058847e39c14cda5fef.png"
},
{
"partyImg": "/uploads/c54a0ace0bb34da3985c67945b1d0bf0.png"
}
]
}
}
Please help me to prepare the graphql input which could generate the expected output.
It appears the graphql schema prevents you from getting the data in the format that you want. Based on your input query, I expect the schema looks something like this:
query {
leaders: [Leader]
}
type Leader {
party: Party
}
type Party {
Image: Image
}
type Image {
url: String
}
To get the data in the format that you want, you would need a schema that looks more like:
query {
leaders: [Leader]
}
type Leader {
party: Party,
imageUrl: String
}
Then you could do:
query Leaders {
leaders {
partyImg: imageUrl
}
}
I assume you don't control the schema, so you would have to do post processing. If you are using javascript, the following could work for the above output as a simple mapping exercise.
(function() {
var output = {
"data": {
"leaders": [{
"partyImg": {
"image": {
"url": "/uploads/17a5f020cc974679ac52e56a22b74dd6.png"
}
}
},
{
"partyImg": {
"image": {
"url": "/uploads/70bd673d41654058847e39c14cda5fef.png"
}
}
},
{
"partyImg": {
"image": {
"url": "/uploads/c54a0ace0bb34da3985c67945b1d0bf0.png"
}
}
}
]
}
};
var transformed = {
data: {
leaders: output.data.leaders.map(function flattenUrl(item) {
return {
partyImg: item.partyImg.image.url
};
})
}
}
document.getElementById('transformedOutput').innerHTML = JSON.stringify(transformed);
}());
<div id="transformedOutput"></div>
If you are the author of this graphql schema, you can structure it in whatever way makes the most sense to your applications and/or consumers.

How to populate only with content of getter

I have some problem in mongoose project.
I try to populate and use getter but not all data
But now all virtuals appear in document.
I'm using mongoose.Schema and mongoose.Model
Here is example of my test code
const GroupsSchema = schema({
title: String,
users: [{
type: schema.Types.ObjectId,
ref: 'Users'
}]
});
const UsersSchema = schema({
name: String,
avatar: String
}, {
toJSON: {
virtuals: true
}
});
class Users extends Model {
get name() {
return {
name: this.name
};
}
get avatar() {
return {
avatar: this.avatar
};
}
}
Populating document
const groups = await Groups.find({}).populate('users').exec();
My current result:
[
{
"_id": "5c9bb51626924f0a08aa8c3d",
"title": "GroupName"
"users": [
{
"_id": "5c8e37169fc1f9000f8c333b",
"name": "Jack",
"avatar": "avatar.jpg",
"name": {
"name": "Jack",
},
"avatar": {
"avatar": "avatar.jpg"
}
}
]
}
]
How can I populate document with content of only name getter?
Desired result:
[
{
"_id": "5c9bb51626924f0a08aa8c3d",
"title": "GroupName"
"users": [
{
"name": "Jack"
}
]
}
]

How to get only the filtered data from Prisma / GraphQL query

I have 2 sets of data (users and events) in many-to-many relation.
I am using the following query to retrieve and filter the data.
{
events(
where: {
AND: [
{ location: { name: "Test" } }
{
time: {
startDate_lt: "2018-12-03T13:46:13.021Z"
endDate_gt: "2018-12-03T13:46:13.021Z"
}
}
{
participantList_some: {
participant: { firstName: "Lorem", lastName: "Ipsum" }
}
}
]
}
) {
participantList {
participant {
firstName
lastName
}
}
location {
name
}
}
}
So far so good, I'm getting the following outcome:
{
"data": {
"events": [
{
"participantList": [
{
"participant": {
"firstName": "Chuck",
"lastName": "Norris"
}
},
{
"participant": {
"firstName": "Lorem",
"lastName": "Ipsum"
}
}
],
"location": {
"name": "Test"
}
}
]
}
}
What I'd like to have would be to get only the participant that I've filtered, ie. "Lorem Ipsum". This way I'm getting all the (2) participants from that event.
So my desired outcome would be:
{
"data": {
"events": [
{
"participantList": [
{
"participant": {
"firstName": "Lorem",
"lastName": "Ipsum"
}
}
],
"location": {
"name": "Test"
}
}
]
}
}
At the moment I'm filtering out the unwanted data from code. I've searched how or if I can do this with a query or additional parameters but didn't find something useful. Any help or guide is appreciated.
You can add a filter to any field of the request. Which means you can filter the participants in your found events:
{
events(
where: {
AND: [
{ location: { name: "Test" } }
{
time: {
startDate_lt: "2018-12-03T13:46:13.021Z"
endDate_gt: "2018-12-03T13:46:13.021Z"
}
}
{
participantList_some: {
participant: { firstName: "Lorem", lastName: "Ipsum" }
}
}
]
}
) {
participantList (where: { participant: { firstName: "Lorem", lastName: "Ipsum" } }) {
participant {
firstName
lastName
}
}
location {
name
}
}
}

Resources