contentful graphql nested structure - graphql

I have a page in contentful which I retrieve content to my react app with graphql.
In this page, a link a content model called Person which is like that:
{
"name": "Person",
"description": "",
"displayField": "name",
"fields": [
{
"id": "name",
"name": "Name",
"type": "Symbol",
"localized": false,
"required": true,
"validations": [],
"disabled": false,
"omitted": false
},
{
"id": "profilePic",
"name": "profile pic",
"type": "Link",
"localized": false,
"required": true,
"validations": [],
"disabled": false,
"omitted": false,
"linkType": "Asset"
},
{
"id": "socialLinks",
"name": "social links",
"type": "Array",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false,
"items": {
"type": "Link",
"validations": [
{
"linkContentType": [
"socialLink"
]
}
],
"linkType": "Entry"
}
}
ProfilePic is another content model which include name and picUrl.
SocialLinks is an array of socialLink content model which contain name and link
I can retrieve without problem my profilePic name or picUrl but I cannot get the socialLinks.
I have read the contentful documentation about one-to-many but is not clear to me how to apply to my case: https://www.contentful.com/developers/docs/references/graphql/#/reference/schema-generation/one-to-many-multi-type-relationships
My query:
...rest of the query..
founder{
name,
profilePic{
url,
title
},
socialLinksCollection {
items {
name,
link
}
}
},
... rest of the query...
Can somoene help me understand why it doesn't work as a normal collection?
Should I maybe use this concept of linkedFrom? But how exactly?

I had to add (limit: 1) to my socialLinksCollections otehrwise there were too many calls and wasn't possible to get the data back.
Thanks to #stefan judis for the suggestion where to look out for the errors.

Related

Dependent field in Laravel Nova using Nova Dependency Container

I'm quite new to Laravel Nova, and I'm trying to implement dependent fields but there's no easy solution to it. I want to display a checkbox if a certain value is selected in a select dropdown.
After a little searching, I found this: https://github.com/epartment/nova-dependency-container. I'm trying to use this, but something's not working right. I'm using Laravel v5.7 and Nova v1.3.2. Hence, I've installed v1.1.2 of this package.
I followed the installation guidelines and have written the following code:
<?php
namespace App\Nova;
...
use Epartment\NovaDependencyContainer\HasDependencies;
use Epartment\NovaDependencyContainer\NovaDependencyContainer;
...
class Submission extends Resource
{
use HasDependencies;
....
public function fields(Request $request)
{
$fields[] = Select::make('Route', 'route')->options([
0 => 'First Name',
1 => 'First Name / Last Name',
2 => 'Full Name'
])->displayUsingLabels();
$fields[] = NovaDependencyContainer::make([
Text::make('Title', 'title')
])->dependsOn('route', 2);
...
return $fields;
}
....
}
?>
I can see the field Route with the right options and values. However, when I select 'Full Name', the dependent field Title is not displayed.
When I see the JSON response of the fields, this is what I get:
...
{
"component": "select-field",
"prefixComponent": true,
"indexName": "Route",
"name": "Route",
"attribute": "route",
"value": null,
"panel": null,
"sortable": false,
"textAlign": "left",
"options": [
{
"label": "First Name",
"value": 0
},
{
"label": "First Name / Last Name",
"value": 1
},
{
"label": "Full Name",
"value": 2
}
]
},
{
"component": "nova-dependency-container",
"prefixComponent": true,
"indexName": "",
"name": "",
"attribute": "",
"value": [],
"panel": null,
"sortable": false,
"textAlign": "left",
"fields": [
{
"component": "text-field",
"prefixComponent": true,
"indexName": "Title",
"name": "Title",
"attribute": "title",
"value": null,
"panel": null,
"sortable": false,
"textAlign": "left"
}
],
"dependencies": [
{
"field": "route",
"value": 2
}
]
},
...
I'm not sure where I'm going wrong. Would be great if someone could guide me here. Or if there's another solution that works.
Thanks!

Strapi mongodd de-populate

Is there any way to prevent a call like this strapi.services.MODEL_NAME.find(query) from populating its relations?
In my specific case I have a simple Message model:
"attributes": {
"body": {
"type": "string",
"minLength": 1,
"required": true,
"maxLength": 300
},
"chat": {
"model": "chat"
},
"user": {
"model": "user",
"plugin": "users-permissions"
}
}
and in a particular case I wish not to populate user & chat, just reference their IDs.
I believe in my case the solution would be to add an empty array:
strapi.services.MODEL_NAME.find(query, [])
You can add autoPopulate option to false.
"user": {
"model": "user",
"plugin": "users-permissions",
"autoPopulate": false
}

Apollo readQuery Fails Even Though Target Object is Present?

I'm working on a call to readQuery. I'm getting an error message:
modules.js?hash=2d0033b4773d9cb6f118946043f7a3d4385825fe:25847
Error: Can't find field resolutions({"id":"Resolution:DHSzPa8bvPCDjuAac"})
on object (ROOT_QUERY) {
"resolutions": [
{
"type": "id",
"id": "Resolution:AepgCCio9KWGkwyMC",
"generated": false
},
{
"type": "id",
"id": "Resolution:DHSzPa8bvPCDjuAac", // <==ID I'M SEEKING
"generated": false
}
],
"user": {
"type": "id",
"id": "User:WWv57KsvqWeAoBNHY",
"generated": false
}
}.
The object with that id appears to be plainly visible as the second entry in the list of resolutions.
Here's my query:
const GET_CURRENT_RESOLUTION_AND_GOALS = gql`
query Resolutions($id: String!) {
resolutions(id: $id) {
_id
name
completed
goals {
_id
name
completed
}
}
}
`;
...and here's how I'm calling it:
<Mutation
mutation={CREATE_GOAL}
update={(cache, {data: {createGoal}}) => {
let id = 'Resolution:' + resolutionId;
const {resolutions} = cache.readQuery({
query: GET_CURRENT_RESOLUTION_AND_GOALS,
variables: {
id
},
});
}}
>
What am I missing?
Update
Per the GraphQL Dev Tools extension for Chrome, here's the whole GraphQL data store:
{
"data": {
"resolutions": [
{
"_id": "AepgCCio9KWGkwyMC",
"name": "testing 123",
"completed": false,
"goals": [
{
"_id": "TXq4nvukpLcqQhMRL",
"name": "test goal abc",
"completed": false,
"__typename": "Goal"
},
],
"__typename": "Resolution"
},
{
"_id": "DHSzPa8bvPCDjuAac",
"name": "testing 345",
"completed": false,
"goals": [
{
"_id": "PEkg5oEEi2tJ6i8LH",
"name": "goal abc",
"completed": false,
"__typename": "Goal"
},
{
"_id": "X4H4dFzGm5gkq5bPE",
"name": "goal bcd",
"completed": false,
"__typename": "Goal"
},
{
"_id": "hYunrXsMq7Gme7Xck",
"name": "goal cde",
"completed": false,
"__typename": "Goal"
}
"__typename": "Resolution"
}
],
"user": {
"_id": "WWv57KsvqWeAoBNHY",
"__typename": "User"
}
}
}
Posted as answer for fellow apollo users with similar problems:
Remove the prefix of Resolution:, the query should only take the id.
Then the question arises how is your datastore filled?
To read a query from cache, the query needs to have been called with exactly the same arguments on the remote API before. This way apollo knows what the result for a field is with specific arguments. If you never called the remote endpoint with the arguments you want to use but know what the result would be, you can circumvent that and resolve the query locally by implementing a cache resolver. Have a look at the example in the documentation. Here the store contains a list of books (in your case resultions) and the query for a single book by id can be resolved with a simple cache lookup.

swagger: how to validate formData

So far I'm able to do swagger validation if the parameters are from "in": "body" or if the input expected is in a json format.
However, I can't find how to validate a simple string entered as formData.
Below is my swagger script (in json format)
v1swag = {
"cancels_post": {
"tags": ["/api/v1"],
"parameters": [
{
"name": "token",
"in": "formData",
"type": "string",
"required": True,
"description": "Cancels the provided token.",
}
],
"responses": {
"200": {
"description": "Success!",
}
}
}
}
I removed the schema as it seems to only work for "in": "body"
I've been searching the net but can't seem to find the light.
Though I will still be searching... Any hints would be greatly appreciated.
Thank you very much in advance.
A different source media type has to be consumed here. Specify "consumes" member to include media type of application/x-www-form-urlencoded.
v1swag = {
"cancels_post": {
"tags": ["/api/v1"],
"consumes": [
"application/x-www-form-urlencoded"
],
"parameters": [
{
"name": "token",
"in": "formData",
"type": "string",
"required": True,
"description": "Cancels the provided token.",
}
],
"responses": {
"200": {
"description": "Success!",
}
}
}
}

Display enum property of model definitions

i am trying to display the enum of a model in the model description.
The schema of my model is defined under definitions and uses an enum for the action property, because only this three types are allowed. (see code below)
I am using swagger version 2.0. In version 1.2 this seems to work: http://petstore.swagger.wordnik.com/ you can find the example under store/order.
they also use an enum and this is displayed behind the property in the model view.
How can i achieve the same result with the new version?
Thanks for help!
"paths": {
"/event": {
"post": {
"tags": [
"event"
],
"summary": "Add an new Event.",
"description": "TEST",
"operationId": "addEvent",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"in": "body",
"name": "data",
"description": "",
"required": true,
"schema": {
"$ref": "#/definitions/Event"
}
}
],
"responses": {
"405": {
"description": "Invalid input"
}
}
}
}
}
"definitions": {
"Event": {
"id": "eventModel",
"required": [
"action"
],
"properties": {
"action": {
"type": "string",
"default": "START",
"enum": [
"START",
"UPDATE",
"END"
],
"description": "blabla"
}
}
}
}
PS: another mistake i recognized right now, is that the shown model description of arrays misses the closing bracket ].
Your definition is fine, there's a known bug in swagger-ui - https://github.com/swagger-api/swagger-ui/issues/672. Feel free to subscribe the issue and follow the progress there.
As a side note, there's no "id" property in models in Swagger 2.0.

Resources