0

I am new to GraphQL and I wonder how I can explore an API without a possible wildcard (*) (https://github.com/graphql/graphql-spec/issues/127).

I am currently setting up a headless Craft CMS with GraphQL and I don't really know how my data is nested.

Event with the REST API I have no chance of just getting all the data, because I have to setup all the endpoints and therefore I have to know all field names as well.

So how could I easily explore my CraftCMS data structure?

Thanks for any hints on this.

Cheers

merc

------ Edit ------- If I use @simonpedro s suggestion:

{
  __schema {
    types {
      name
      kind
      fields {
        name
      }
    }
  }
}

I can see a lot of types (?)/fields (?)... For example I see:

{
      "name": "FlexibleContentTeaser",
      "kind": "OBJECT",
      "fields": [
        {
          "name": "id"
        },
        {
          "name": "enabled"
        },
        {
          "name": "teaserTitle"
        },
        {
          "name": "text"
        },
        {
          "name": "teaserLink"
        },
        {
          "name": "teaserLinkConnection"
        }
      ]

But now I would like to know how a teaserLink ist structured. I somehow found out that the teaserLink (it is a field with the type Entries, where I can link to another page) has the properties url & title.

But how would I set up query to explore the properties available within teaserLink?

I tried all sorts of queries, but I am always confrontend with messages like this: GraphiQL warning

I would be really glad if somebody could give me another pointer how I can find out which properties I can actually query...

Thank you

Merc
  • 4,241
  • 8
  • 52
  • 81

1 Answers1

1

As far as I'm concerned currently there is no graphql implementation with that capability. However, if what you want to do is to explore the "data structure", i.e, the schema, you should use schema instrospection, which was thought for that (explore the graphql schema). For example, a simple graphql instrospection query would be like this:

{
  __schema {
    types {
      name
      kind
      fields {
        name
      }
    }
  }
}

References: - https://graphql.org/learn/introspection/

UPDATE for edit:

What you want to do I think is the following: Make a query like this

{
  __schema {
    types {
      name
      kind
      fields {
        name
        type {
          fields {
            name
          }
        }
      }
    }
  }
}

And then find the wished type field to grab more information (the fields) from it. Something like this (I don't know if this works, just an idea):

const typeFlexibleContentTeaser = data.__schema.types.find(t => t === "FlexibleContentTeaser")

const teaserLinkField = typeFlexibleContentTeaser.fields.find(f => f.name === "teaserLink")

const teaserLinkField = teaserLinkField.type.fields;

i.e, you have to transverse recursively through the type field.

simonpedro
  • 46
  • 1
  • 3
  • Looks very promising. I will check it out. Thx – Merc Jul 25 '19 at 15:35
  • I personally prefer seeing a full API filled with data to explore it, but this definitely solves my problem. Thank you – Merc Jul 30 '19 at 06:47
  • Well I do have another question though: With your solution I can only see the properties within the root, but I want to know what's inside. (I will edit my question to have more editing features... --> see above) – Merc Jul 30 '19 at 07:15
  • Hm thank you for further elaborating this. Unfortunately I don't really understand the process. I did make the query, but where should I put the JS code? I thought that fetching the data with the query above does not contain the wanted fields... Or am I wrong? As far as I understand your data object somehow already includes everything, or am I wrong? – Merc Aug 02 '19 at 10:13