Skip to content

Instantly share code, notes, and snippets.

@tgroshon
Last active April 4, 2019 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgroshon/ab4b19b0db36324f3e7f7773ae591340 to your computer and use it in GitHub Desktop.
Save tgroshon/ab4b19b0db36324f3e7f7773ae591340 to your computer and use it in GitHub Desktop.
Example GraphQL Schema
# These are your main API Types
type User {
id: ID!
name: String!
email: AWSEmail
}
type Activity {
id: ID!
title: String!
description: String
location: Location
creator: User
notes: [ActivityNote]
costCodes: [CostCodes]
}
type Location {
lat: Float!
long: Float!
}
type ActivityNote {
date: AWSDate
author: User
content: String
}
type CostCodes {
id: ID!
description: [String]
}
# The kinds of reads you can do
type Query {
getUser(id: ID!) User
listUsers(emailFilter: AWSEmail) [User]
getActivity(id: ID!) Activity
listActivities(locationFilter: Location) [Activity]
}
# The kinds of creates, updates, and deletes you can do
type Mutation {
addUser(name: String!, email: AWSEmail) User
addActivity(title: String!, description: String, location: Location) Activity
}
# Declares your API schema
schema {
query: Query
mutation: Mutation
}
@tgroshon
Copy link
Author

tgroshon commented Apr 4, 2019

A query looks like this:

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

The part trailing the user(id: 1) is where I specify which fields of the return type (User in this case) i want to get back. I didn't ask for email, so it won't be returned.

@tgroshon
Copy link
Author

tgroshon commented Apr 4, 2019

A more complicated/interesting query:

query {
  listActivities {
    id
    title
    creator {
      id
      name
      email
    }
    notes {
      date
      content
      author {
        id
        name
      }
    }
  }
}

This will return Activities with the creator as a sub-object, the notes as a list of notes, and the author of each note as a sub-object on each note. All in one query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment