Skip to content

Example Mutations and Queries

MichaelJCompton edited this page Feb 23, 2019 · 3 revisions

Once you have a GraphSchema GraphQL API endpoint up and running (see Running GraphSchema), you can run GraphQL queries and mutations.

If you followed on from Running GraphSchema and used the helper script, you'll have an API endpoint running at http://localhost:10550/api/graphql.

Queries and mutations are run however you'd prefer for GraphQL. It's just a GraphQL endpoint, so anything that can consume GraphQL should work.

In development and testing, you might use insomnia, a GraphQL chrome extension, or postman. An app you write would contact the API with HTTP POST requests.

I'm using Insomnia in the examples below.

Add some data

The example mutations have some samples of how to add data using the API. For example, I pasted the mutation to add an author into insomnia

added, the variables

{
    "input": {
        "name": "A. Author",
        "password": "aPaSSwoRd"
    }
}

made sure it was posting json to the right HTTP endpoint, and ran the mutation, resulting in.

Adding posts is similar (just make sure the uid links to the right author). E.G. running

mutation addPost($input: PostInput!) {
  addPost(inputData: $input) {
    id,
    title,
    text,
    author { name }
  }
}

with variables

{
    "input": {
        "title": "GraphQL in Dgraph",
        "text": "Conventions based API from declarative schema",
        "author": {
            "uid": "0x2"
        }
    }
}

gives

Query data

Add some more data and then try the example queries.

For example, to query for all authors.

query queryAuthor {
  queryAuthor {
    id,
    name
  }
}

Or, get an individual author with the query

query getAuthor($inp: ID!) {
  getAuthor(id: $inp) {
    id,
    name,
    posts { title }
  }
}

and insert the Id as a variable

{
    "inp": "0x2"
}

Or you can insert the Id directly into the query.