Graphql: Difference between revisions

From wikinotes
Line 19: Line 19:
curl -X POST \
curl -X POST \
   -H 'Content-Type: application/json' \
   -H 'Content-Type: application/json' \
   -d '{"query":    "query { members($search: String){ firstName lastName } }", \
   -d '{"query":    "query { members($search: String){ firstName lastName } }", `# <-- graphql syntax` \
       "variables" { "search": "al*" }}' \
       "variables" { "search": "al*" }}' `# <-- json` \
   example.com/graphql
   example.com/graphql
</syntaxhighlight>
</syntaxhighlight>

Revision as of 15:54, 23 July 2021

GraphQL is an alternative to REST APIs that enables you to query exactly what you need using a single HTTP request, rather than several.

Documentation

docs https://graphql.org/learn/
introspection docs https://graphql.org/learn/introspection/

Example

Graphql is a language, and it's schema is a large file defining all of the available types, queries, and mutations.

curl -X POST \
  -H 'Content-Type: application/json' \
  -d '{"query":    "query { members($search: String){ firstName lastName } }", `# <-- graphql syntax` \
       "variables" { "search": "al*" }}' `# <-- json` \
  example.com/graphql
curl -X POST \
  -H 'Content-Type: application/json' \
  -d '{"query":  "mutation { createMember($firstName: String!, $lastName: String!){ ... } }", \
       "variables": { "firstName": "foo", "lastName": "bar" }}' \
  example.com/graphql

Notes

graphql: basics
graphql: usage
graphql: datatypes
graphql: schemas
grpahql: syntax

Clients

graphql: graphiql web client
graphqurl cli interpreter

Introspection

List Types

{ __schema { types { name } } }                    # list all types
{ __schema { mutationType { fields { name } } } }  # list all mutations
{ __schema { queryType { fields { name } } } }     # list all queries

Query Available Fields

{
  __type(name: "Droid") {  # GraphQL type we want fields from
    name
    fields {
      name
      type {
        name
        kind
      }
    }
  }
}

Returns all fields, and their type info.

Query Type info

{
  __type(name: "Shop") {
    fields {
      name
      description
    }
  }
}