How to request the schema from a GraphQL service using curl? - graphql

I have a curl script (as a standin for real code) that can POST to my company's GraphQL endoint and get data. It's working fine.
It appears that it should also be possible to get the "schema" by crafting the appropriate request, but I have not found any way to do that at a HTTP level.
If it is possible, what would the curl look like (the data)?
Are there other requests other than "query" that I can use to gleen other information?

The body you need is pretty involved for an introspection query, but I think you're looking for something like this
introspection_query.json:
{
"query": "query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}"
}
and then you can do
curl -i -X POST http://localhost:8080/graphql -H "Content-Type: application/json" -d #introspection_query.json
Shamefully stolen from
https://gist.github.com/martinheld/9fe32b7e2c8fd932599d36e921a2a825

Related

Does Pimcore provide possibility to export Hotspots and Markers additional data in DataHub GraphQL?

Asset Hotspots have 'add Data' in context menu.
We can add related objects / fileds / documents / etc there.
How to export this data in GraphQL? I see only '__typename' property.
Sample query:
{
getCategoryListing(defaultLanguage: "en") {
totalCount
edges {
node {
categoryimage {
image {
fullpath
}
hotspots {
name
top
height
left
width
data {
__typename
}
}
marker {
name
top
left
data {
__typename
}
}
}
}
}
}
}
Yes, this functionality is provided by GraphQL itself.
Check that you granted reading rights to object class that you are trying to access otherwise you will see 'object: null'
learch which object types you get using __typename
When you know the type name and have access to that object, just get its data
Sample:
... on property_text {
type
name
text
}
or
... on object_product {
sku
}
See the documentation here: https://pimcore.com/docs/data-hub/current/GraphQL/Query/Query_Samples/Sample_Element_Properties.html
Sample query to get additional data from Pimcore Hotspots and Markers on Hotspotimage:
{
getCategoryListing(defaultLanguage: "en") {
totalCount
edges {
node {
id
categoryimage {
image {
fullpath
}
hotspots {
name
top
height
left
width
data {
__typename
... on property_text {
type
name
text
}
... on property_object {
type
name
object {
__typename
... on object_product {
sku
}
... on element {
__typename
}
}
}
}
}
marker {
name
top
left
data {
__typename
... on property_text {
type
name
text
}
... on property_object {
type
name
object {
__typename
... on object_product {
sku
}
}
}
}
}
}
}
}
}
}

GitHub GraphQL filter multi variable language filter

I'm trying to use GitHub's GraphQL API to find a list of repos matching a query but limited to a specific language. However, I can't find anything in the docs relating to the multi variable language filter the typical online search supports or how something like this is typically done with GraphQL.
{
search(query: "language:java", type: REPOSITORY, first: 10) {
repositoryCount
edges {
node {
... on Repository {
nameWithOwner
forkCount
hasIssuesEnabled
hasProjectsEnabled
homepageUrl
id
}
}
}
}
}
I want to pass two params on language and show the result but this query just use string to search. I need to send a request as multi items like this
language:['go','java','javaScript']
As a workaround, you can use aliases to build dynamic query with many search query targetting a specific language and fragments to avoid repetition of the SearchResultItemConnection in the query :
{
go: search(query: "language:go", type: REPOSITORY, first: 10) {
...SearchResult
}
java: search(query: "language:java", type: REPOSITORY, first: 10) {
...SearchResult
}
javascript: search(query: "language:javascript", type: REPOSITORY, first: 10) {
...SearchResult
}
}
fragment SearchResult on SearchResultItemConnection {
repositoryCount
edges {
node {
... on Repository {
nameWithOwner
forkCount
hasIssuesEnabled
hasProjectsEnabled
homepageUrl
id
}
}
}
}
Try it in the explorer
Note that it would only work for OR query (java or javascript or go for the list of languages) but not AND
The request can be built programmatically such as in this python script :
import requests
token = "YOUR_TOKEN"
languages = ["go","java","javaScript"]
query = """
{
%s
}
fragment SearchResult on SearchResultItemConnection {
repositoryCount
edges {
node {
... on Repository {
nameWithOwner
forkCount
hasIssuesEnabled
hasProjectsEnabled
homepageUrl
id
}
}
}
}
"""
searchFragments = "".join([
"""
%s: search(query: "language:%s", type: REPOSITORY, first: 10) {
...SearchResult
}
""" % (t,t) for t in languages
])
r = requests.post("https://api.github.com/graphql",
headers = {
"Authorization": f"Bearer {token}"
},
json = {
"query": query % searchFragments
}
)
print(r.json()["data"])

Is there a way to filter out null values for literals and references

If we have an Author with no beacons to Articles and thus WroteArticles was null and we wanted to only return Authors who had non-empty/non-null WroteArticles, how could that be done?
As an example we can use the Weaviate demo site
I've tried filter operations using where and various operators, but I must be missing something obvious. Example of a query I've tried on my own data set below, where I did have a Thing with no beacons.
{
Get {
Things {
Author (where:{
operator:Equal,
path:["WroteArticles"]
valueString:" "
}){
name
WroteArticles {
... on Article {
InPublication {
... on Publication {
name
}
}
}
}
}
}
}
}
You can now do this as follows (also in the documentation):
{
Get {
Things {
Author(
where:{
valueInt: 2
operator:GreaterThanEqual
path: ["WroteArticles"]
}
) {
name
WroteArticles {
... on Article {
title
}
}
}
}
}
}

GraphQl API select specific values

I have the following query :
{
allPeople {
people {
name
filmConnection {
films {
title
}
}
}
}
}
I would like to select all people that have a film connection with the title a new hope. How do i select this specific thing from the API. I could also just get it like this and handle it in the code. But surely there is a better way.
What i'd expect :
{
allPeople {
people {
name
filmConnection {
films {
title : "a new hope"
}
}
}
}
}
That didnt work..
Try out here in this playground :
https://graphql.org/swapi-graphql?query=%7B%0A%20%20allPeople%20%7B%0A%20%20%20%20people%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20filmConnection%20%7B%0A%20%20%20%20%20%20%20%20films%20%7B%0A%20%20%20%20%20%20%20%20%20%20title%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A
graphql queries ... are not for building [sql] queries ;)
this is more for defining shape of required data
parameter CAN BE passed to deeper child (f.e. can be used to filter films)
{
allPeople {
people {
name {
filmConnection {
films(title_eq:"a new hope") {
title
}
}
}
}
}
}
... if API supports this kind of filtering for films
... but it won't filter people - you'll have all people AND filtered films (for all people) because filters won't work on parents.
You CAN have custom API that will be this kind of filtering aware, f.e.
{
allPeople {
people(connectedFilmTitle_eq:"a new hope") {
name {
filmConnection {
films {
title
}
}
}
}
}
}
customized (not automatically gnerated) peolpe resolver can make appriopriate query [on joined tables] and return structure you're asking for.
In that case probably you don't need deeper structures - ...filmConnection { films { title - you know this data earlier (filter parameters).
... but probably you have a many2many relation and cen reverse this query:
{
allFilms {
films(title_eq:"a new hope") {
title {
peoleConnection {
people {
name
}
}
}
}
}
}

how to query prismic slices and returning data from each slice

I'm trying to use Gatsby's /___graphq debugger and the README file for gatsby-source-prismic says you can return slices. So below I'm returning the slice with a name PrismicProductBodySteps.
{
allPrismicHomePage {
edges {
node {
data {
seo_title
body {
__typename
... on PrismicProductBodySteps {
}
}
}
}
}
}
}
}
Can someone explain to me what ... on PrismicProductBodySteps means ?
In a gatsby component I've seen this as an example.
body {
... on PrismicProductsBodySteps {
...ProductStepsFragment
}
Can anyone explain to me what the ...ProductStepsFragment means ?
PrismicProductBodySteps would be a custom node type name representing a dynamic series of content blocks. That custom node type name is coming from a Prismic data model; yours will likely be different.
According to the gatsby-source-prismic documentation, using custom node type names requires you to figure out what they are first:
The easiest way to get the type of nodes is to use the /___graphql
debugger and run the below query (adjust the document type and field
name).
{
allPrismicPage {
edges {
node {
id
data {
body {
__typename
}
}
}
}
}
}
Once you have your custom node type name, you can use a GraphQL fragment to pull data specific to each fragment. Again, this would depend on how you have the fragments defined in your data model, but it would look something like this:
{
allPrismicHomePage {
edges {
node {
data {
seo_title
body {
__typename
... on PrismicYourContentBlockOne {
text {
html
}
}
... on PrismicYourContentBlockTwo {
text {
html
}
}
... on PrismicYourContentBlockThree {
text {
html
}
}
}
}
}
}
}
}

Resources