Reading article A Deep Dive into Strapi GraphQL
REST API work fine
http://localhost:1337/api/blogs?filters[id][$eq]=1
But GraphQL Not
query {
blogs {
id
Title
Body
}
}
Response
{
"errors": [
{
"message": "Cannot query field \"id\" on type \"BlogEntityResponseCollection\".",
}
...
}
}
Strapi v4.0.7 node v14.17.5
Using GraphQL extension for chrome and Altai client for query
Try start Strapi with:
yarn start
yarn develop
using GraphQL playground for chrome hints i make queries like that, different from article
query {
blog(id: 1) {
data {
attributes{
Title
}
}
}
}
querying all the blogs
query {
blogs {
data {
attributes{
Title
Body
}
}
}
}
querying single blog
query {
blog (id: 1) {
data {
attributes{
Title
Body
}
}
}
}
Related
this graphQL query works for me through the GitHub GraphQL API:
query {
repository(owner: "MY_Org", name: "My_Repo") {
object(expression: "master:README.md") {
... on Blob {
text
}
}
}
}
However, if I try and run the command against GitHub-Enterprise GraphQL I just get back null for the ... Blob inline fragment.
Is this a limitation of GitHub Enterprise? Also is there another way I can accomplish reading a file via GHE GQL someone can suggest?
Thank you!
Also, here's the response I am getting indicating GQL is querying the repo correctly:
{
"data": {
"repository": {
"name": "My_Repo",
"url": "https://github.[my_company].com/[My_Org]/[My_repo]",
"object": null
}
}
}
GraphQL lets you ask for specific fields, the response contains only the fields that you had asked for. For example:
a graphql query like:
{
hero {
name
}
}
will return:
{
"data": {
"hero": {
"name": "R2-D2"
}
}
}
where as a graphQl query like:
{
hero {
name
friends {
name
}
}
}
would return:
{
"data": {
"hero": {
"name": "R2-D2",
"friends": [
{
"name": "Luke"
},
{
"name": "Han Solo"
},
{
"name": "Leia"
}
]
}
}
}
Is there a similar mechanism/library/pattern that can be used in gRPC to achieve the same?
FieldMask is similar in protobuf. It is a list of fields to retain, so the first example would be paths: "hero.name" and the second would be paths: ["hero.name", "hero.friends.name"].
It is probably most frequently used to specify which fields should be changed in an update. But it can equally be used to specify the fields that should be returned.
The server can either process the FieldMask directly (e.g., only using the listed fields in a SELECT SQL query), or it can retrieve all the information and filter the result using FieldMaskUtil.merge() to copy just the requested fields into a new proto message to return to the client.
I'm following this tutorial on Medium to get Gatsby working with Prismic.
In the GraphiQL explorer, the two queries below both yield the same result and was wondering when I should use one over the other (i.e. edges.node.data vs nodes.data):
Query #1:
query Articles {
articles: allPrismicArticle {
edges {
node {
data {
title {
text
}
image {
url
}
paragraph {
html
}
}
}
}
}
}
Query #2:
query Articles {
articles: allPrismicArticle {
nodes {
data {
title {
text
}
image {
url
}
paragraph {
html
}
}
}
}
}
As you've found out, there's no difference at all. nodes can be thought of as a shortcut to edges.map(edge => edge.node). This'll save us a bit of typing when using the data returned by graphql.
There's a few case where querying edges is useful, for example in a allMarkdownRemark query, edges may contain helpful info like total posts.
I'm looking into GraphQL and got to a question I'm not even sure how to look for in the docs. Probably this isn't even supposed to work, but I'll ask anyway.
I have this query:
query {
organization(login: "facebook") {
repositories(first: 5) {
items: edges {
repo: node {
name
owner {
login
}
}
}
}
}
}
Now, in the response I would like a way to place the login next to name, instead of nested in owner login.
So in the response instead of:
{
...
"name": "react-native",
"owner": {
"login": "facebook"
}
}
I would like to have:
{
...
"name": "react-native",
"ownerName": "facebook"
}
Thank you.
From the specification this is not possible. The response has to be shaped in the way the object types are shaped. There is a project called GraphQL Lodash that gives you a new directive to modify the results. It can be helpful here but it is rather experimental and IMO very undogmatic.
I started using Gatsby along with GraphQL to return a query that lists all galleries that are part of a specific category. In this example the category is called "Lifestyle". This all works successfully and I get an array with all of the Galleries in the category. I also need to sort this array based off a sub field called "date". Is that possible to do via the GraphQL query itself? I tried adding (sort: { fields: [date], order: DESC }) to the gallery field but that didn't work.
Any thoughts on how to achieve this or is this as close as GraphQL can get me to what I need?
Thanks in advance for any help. Still trying to wrap my head around GraphQL.
Ryan
Example of my current query
Could you provide a bit more details about your content model?
If you use a "Reference" field in contentful, it is sadly not possible as of now with the plugin as far as I know.
If you use a "Short text, list" field, like the tags in the default example.
With the default example, you can do the following query:
{
allContentfulPost(filter:{tags:{eq:"fantasy"}}, sort:{fields:[date], order:DESC}) {
edges {
node {
title {
childMarkdownRemark {
html
}
}
slug
date
}
}
}
}
It will give you the following result:
{
"data": {
"allContentfulPost": {
"edges": [
{
"node": {
"title": {
"childMarkdownRemark": {
"html": "<p>Disney Movie</p>"
}
},
"slug": "down-the-rabbit-hole",
"date": "2017-11-26"
}
},
{
"node": {
"title": {
"childMarkdownRemark": {
"html": "<p>Old book</p>"
}
},
"slug": "down-the-rabbit-hole-2",
"date": "1865-11-26"
}
}
]
}
}
}
I came across this same issue as I was trying to sort posts from a category tag that I was using in Contentful. Like #chmac said, you can sort the data from GraphQL with Javascript.
I had to search for a good example, but I finally found one in this Gatsby starter:
Github: https://github.com/ryanwiemer/gatsby-starter-gcn/blob/master/src/templates/tag.js
Live Example: https://gcn.netlify.com/tag/fancy/
You can see in the source file that they sorted the data in a new constant called posts using moment (https://www.npmjs.com/package/moment) and lodash. In my personal example I had to tweak my constant like so:
const courses = orderBy(
this.props.data.contentfulCategory.course,
// eslint-disable-next-line
[object => new moment(object.createdAt)],
['desc']
)
Then I just used a map function like so in the component return:
{/* Courses */}
{courses.map(course => (
<div className="hero__profile" key={course.id}>
<h2>{course.title}</h2>
</div>
))}
I hope this helps!