Home

Awesome

Pets Example

This example is meant to show a full implementation of the server using an SQL datastore.

Starting

start the server, if you don't have the sqlite library already installed from gorm, then it may take a while to compile it

go run *.go

and then visit the GraphiQL dev server at localhost:8080

NOTES:

Example queries

get user by ids with pets belonging to the user...

{
  getUser(id: 1) {
    name
    pets {
      name
      id
    }
  }
}

get pet by id and owner...

{
 getPet(id: 1) {
  name
  owner {
    name
    id
  }
}
}

insert pet with owner

mutation addPet($userID: Int!, $pet: PetInput!) {
  addPet(userID: $userID, pet: $pet) {
    name
  }
}

# query variables...
{
  "userID": 1,
  "pet": {
    "name": "slkdjsaldkjsalkj"
  }
}

update pet (needs query variables)

mutation UpdatePet($pet: PetInput!) {
  updatePet(pet: $pet) {
    name
    id
    owner {
      name
    }
    tags {
      title
    }
  }

}

delete pet (needs query variables)

mutation DeletePet($userID: Int!, $petID: Int!) {
  deletePet(userID: $userID, petID: $petID) {
    
  }
}

query Pet {
  one: getPet(id: 1) {
    name
    owner {
      name
    }
    tags {
      title
    }
  },
  two: 	getUser(id: 1) {
    name
  }
}

pagination ("after" or "before" can be added with the encoded cursor)

{
  getUser(id: 1) {
    name
    petsConnection(first: 2) {
      totalCount
      edges {
        cursor 
        node {
          name
        }
      }
    }
  }
}