Skip to content

Instantly share code, notes, and snippets.

@jexp
Last active December 8, 2021 15:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jexp/750d4b7c72a6596025a7f0f09d47106b to your computer and use it in GitHub Desktop.
Save jexp/750d4b7c72a6596025a7f0f09d47106b to your computer and use it in GitHub Desktop.
GraphQL Training Queries
{
movies(options: { limit: 10 }) {
title
actors {
name
}
}
}
{
directors(where:
{name:"Robert Redford"}) {
name
directed {
title
plot
}
}
}
mutation {
createBooks(
input: {
isbn: "1492047686"
title: "Graph Algorithms"
price: 37.48
description:
"Practical Examples in Apache Spark and Neo4j"
}
) {
books {
isbn
title
price
description
__typename
}
}
}
mutation {
createReviews(
input: {
rating: 5
text: "Best overview of graph data science!"
book: { connect: { where: { node: { title: "Graph Algorithms" } } } }
}
) {
reviews {
rating
text
createdAt
book {
title
}
}
}
}
mutation {
createCustomers(
input: {
username: "EmilEifrem7474"
reviews: {
connect: {
where: { node: { text: "Best overview of graph data science!" } }
}
}
orders: {
create: {
node: {
books: {
connect: { where: { node: { title: "Graph Algorithms" } } }
}
shipTo: {
create: {
node: {
address: "111 E 5th Ave, San Mateo, CA 94401"
location: {
latitude: 37.5635980790
longitude: -122.322243272725
}
}
}
}
}
}
}
}
) {
customers {
username
orders {
placedAt
books {
title
}
shipTo {
address
}
}
reviews {
text
rating
book {
title
}
}
}
}
}
match (a) detach delete a
mutation {
createBooks(
input: [
{
isbn: "1492047686"
title: "Graph Algorithms"
price: 37.48
description: "Practical Examples in Apache Spark and Neo4j"
}
{
isbn: "1119387507"
title: "Inspired"
price: 21.38
description: "How to Create Tech Products Customers Love"
}
{
isbn: "190962151X"
title: "Ross Poldark"
price: 15.52
description: "Ross Poldark is the first novel in Winston Graham's sweeping saga of Cornish life in the eighteenth century."
}
]
) {
books {
title
}
}
createCustomers(
input: [
{
username: "EmilEifrem7474"
reviews: {
create: {
node: {
rating: 5
text: "Best overview of graph data science!"
book: { connect: { where: { node: { isbn: "1492047686" } } } }
}
}
}
orders: {
create: {
node: {
books: {
connect: { where: { node: { title: "Graph Algorithms" } } }
}
shipTo: {
create: {
node: {
address: "111 E 5th Ave, San Mateo, CA 94401"
location: {
latitude: 37.5635980790
longitude: -122.322243272725
}
}
}
}
}
}
}
}
{
username: "BookLover123"
reviews: {
create: {
node: {
rating: 4
text: "Beautiful depiction of Cornwall."
book: { connect: { where: { node: { isbn: "190962151X" } } } }
}
}
}
orders: {
create: {
node: {
books: {
connect: [
{ where: { node: { title: "Ross Poldark" } } }
{ where: { node: { isbn: "1119387507" } } }
{ where: { node: { isbn: "1492047686" } } }
]
}
shipTo: {
create: {
node: {
address: "Nordenskiöldsgatan 24, 211 19 Malmö, Sweden"
location: {
latitude: 55.6122270502
longitude: 12.99481772774
}
}
}
}
}
}
}
}
]
) {
customers {
username
}
}
}
{
books {
title
description
price
}
}
type Order {
orderID: ID! @id
placedAt: DateTime @timestamp
shippingCost: Float
shipTo: Address @relationship(type: "SHIPS_TO", direction: OUT)
customer: Customer @relationship(type: "PLACED", direction: IN)
books: [Book] @relationship(type: "CONTAINS", direction: OUT)
}
type Customer {
username: String
orders: [Order] @relationship(type: "PLACED", direction: OUT)
reviews: [Review] @relationship(type: "WROTE", direction: OUT)
}
type Address {
address: String
location: Point
order: Order @relationship(type: "SHIPS_TO", direction: IN)
}
type Book {
isbn: ID!
title: String
price: Float
description: String
authors: [Author] @relationship(type: "AUTHOR_OF", direction: IN)
subjects: [Subject] @relationship(type: "ABOUT", direction: OUT)
reviews: [Review] @relationship(type: "REVIEWS", direction: IN)
}
type Review {
rating: Int
text: String
createdAt: DateTime @timestamp
book: Book @relationship(type: "REVIEWS", direction: OUT)
author: Customer @relationship(type: "WROTE", direction: IN)
}
type Author {
name: String!
books: [Book] @relationship(type: "AUTHOR_OF", direction: OUT)
}
type Subject {
name: String!
books: [Book] @relationship(type: "ABOUT", direction: IN)
}
# Write your query or mutation here
mutation {
createAuthors(
input: [
{
name: "Marty Cagan"
books: { connect: { where: { node: { title: "Inspired" } } }}
}
{
name: "Winston Graham"
books: { connect: { where: { node: { title: "Ross Poldark" } } }}
}
{
name: "Mark Needham"
books: { connect: { where: { node:{ title: "Graph Algorithms" } } }}
}
{
name: "Amy E. Hodler"
books: { connect: { where:{ node: { title: "Graph Algorithms" } }} }
}
]
) {
authors {
name
books {
title
}
}
}
}
mutation {
inspired: updateBooks(
where: { title: "Inspired" }
create: {
subjects: [
{ node: { name: "Product management" } }
{ node: { name: "Design" } }
]
}
) {
books {
title
subjects {
name
}
}
}
poldark: updateBooks(
where: { title: "Ross Poldark" }
create: {
subjects: [
{ node: { name: "Historical fiction" } }
{ node: { name: "Cornwall" } }
]
}
) {
books {
title
subjects {
name
}
}
}
algorithms: updateBooks(
where: { title: "Graph Algorithms" }
create: {
subjects: [
{ node: { name: "Graph theory" } }
{ node: { name: "Neo4j" } }
]
}
) {
books {
title
subjects {
name
}
}
}
}
{
books {
title
reviews {
rating
text
author {
username
}
}
}
}
mutation {
createBooks(
input: [
{
isbn: "1492047686"
title: "Graph Algorithms"
price: 37.48
description: "Practical Examples in Apache Spark and Neo4j"
subjects: {
create: [
{ node: { name: "Graph theory" } }
{ node: { name: "Neo4j" } }
]
}
authors: {
create: [
{ node: { name: "Mark Needham" } }
{ node: { name: "Amy E. Hodler" } }
]
}
}
{
isbn: "1119387507"
title: "Inspired"
price: 21.38
description: "How to Create Tech Products Customers Love"
subjects: {
create: [
{ node: { name: "Product management" } }
{ node: { name: "Design" } }
]
}
authors: { create: { node: { name: "Marty Cagan" } } }
}
{
isbn: "190962151X"
title: "Ross Poldark"
price: 15.52
description: "Ross Poldark is the first novel in Winston Graham's sweeping saga of Cornish life in the eighteenth century."
subjects: {
create: [
{ node: { name: "Historical fiction" } }
{ node: { name: "Cornwall" } }
]
}
authors: { create: { node: { name: "Winston Graham" } } }
}
]
) {
books {
title
}
}
createCustomers(
input: [
{
username: "EmilEifrem7474"
reviews: {
create: {
node: {
rating: 5
text: "Best overview of graph data science!"
book: { connect: { where: { node: { isbn: "1492047686" } } } }
}
}
}
orders: {
create: {
node: {
books: {
connect: { where: { node: { title: "Graph Algorithms" } } }
}
shipTo: {
create: {
node: {
address: "111 E 5th Ave, San Mateo, CA 94401"
location: {
latitude: 37.5635980790
longitude: -122.322243272725
}
}
}
}
}
}
}
}
{
username: "BookLover123"
reviews: {
create: {
node: {
rating: 4
text: "Beautiful depiction of Cornwall."
book: { connect: { where: { node: { isbn: "190962151X" } } } }
}
}
}
orders: {
create: {
node: {
books: {
connect: [
{ where: { node: { title: "Ross Poldark" } } }
{ where: { node: { isbn: "1119387507" } } }
{ where: { node: { isbn: "1492047686" } } }
]
}
shipTo: {
create: {
node: {
address: "Nordenskiöldsgatan 24, 211 19 Malmö, Sweden"
location: {
latitude: 55.6122270502
longitude: 12.99481772774
}
}
}
}
}
}
}
}
]
) {
customers {
username
}
}
}
type Order {
orderID: ID! @id
placedAt: DateTime @timestamp
shippingCost: Float
shipTo: Address @relationship(type: "SHIPS_TO", direction: OUT)
customer: Customer @relationship(type: "PLACED", direction: IN)
books: [Book] @relationship(type: "CONTAINS", direction: OUT)
}
extend type Order {
subTotal: Float @cypher(statement:"MATCH (this)-[:CONTAINS]->(b:Book) RETURN sum(b.price)")
shippingCost: Float @cypher(statement:"MATCH (this)-[:SHIPS_TO]->(a:Address) RETURN round(0.01 * distance(a.location, Point({latitude: 40.7128, longitude: -74.0060})) / 1000, 2)")
estimatedDelivery: DateTime @ignore
}
extend type Order @auth(rules: [{bind: {customer: {username: "$jwt.sub"}}}, {roles: ["admin"]}])
type Customer {
username: String
orders: [Order] @relationship(type: "PLACED", direction: OUT)
reviews: [Review] @relationship(type: "WROTE", direction: OUT)
recommended(limit: Int = 3): [Book] @cypher(statement: "MATCH (this)-[:PLACED]->(:Order)-[:CONTAINS]->(:Book)<-[:CONTAINS]-(:Order)<-[:PLACED]-(c:Customer) MATCH (c)-[:PLACED]->(:Order)-[:CONTAINS]->(rec:Book) WHERE NOT EXISTS((this)-[:PLACED]->(:Order)-[:CONTAINS]->(rec)) RETURN rec LIMIT toInteger($limit)")
}
extend type Customer @auth(rules: [{where: {username: "$jwt.sub"}}])
type Address {
address: String
location: Point
order: Order @relationship(type: "SHIPS_TO", direction: IN)
}
extend type Address {
currentWeather: Weather @cypher(statement:"CALL apoc.load.json('https://www.7timer.info/bin/civil.php?lon=' + this.location.longitude + '&lat=' + this.location.latitude + '&ac=0&unit=metric&output=json&tzshift=0') YIELD value WITH value.dataseries[0] as weather RETURN {temperature: toFloat(weather.temp2m), windSpeed: toFloat(weather.wind10m.speed), windDirection: weather.wind10m.direction, precipitation: weather.prec_type, summary: weather.weather} AS conditions")
}
type Weather {
temperature: Float
windSpeed: Float
windDirection: String
precipitation: String
summary: String
}
type Book {
isbn: ID!
title: String
price: Float
description: String
authors: [Author] @relationship(type: "AUTHOR_OF", direction: IN)
subjects: [Subject] @relationship(type: "ABOUT", direction: OUT)
reviews: [Review] @relationship(type: "REVIEWS", direction: IN)
}
extend type Book @auth(rules: [{operations: [CREATE, UPDATE, DELETE], roles: ["admin"]}])
extend type Book {
similar: [Book] @cypher(statement: """
MATCH (this)-[:ABOUT]->(s:Subject)<-[:ABOUT]-(rec:Book)
WITH rec, COUNT(*) AS num
RETURN rec ORDER BY num DESC
""")
}
type Review {
rating: Int
text: String
createdAt: DateTime @timestamp
book: Book @relationship(type: "REVIEWS", direction: OUT)
author: Customer @relationship(type: "WROTE", direction: IN)
}
extend type Review @auth(rules: [{operations: [CREATE,UPDATE], bind: {author: {username: "$jwt.sub"} }}])
type Author {
name: String!
books: [Book] @relationship(type: "AUTHOR_OF", direction: OUT)
}
type Subject {
name: String!
books: [Book] @relationship(type: "ABOUT", direction: IN)
}
extend type Subject @auth(rules: [{isAuthenticated: true}])
type Mutation {
mergeBookSubjects(subject: String!, bookTitles: [String!]!): Subject @cypher(statement: """
MERGE (s:Subject {name: $subject})
WITH s
UNWIND $bookTitles AS bookTitle
MATCH (t:Book {title: bookTitle})
MERGE (t)-[:ABOUT]->(s)
RETURN s
""")
}
type Query {
bookSearch(searchString: String!): [Book] @cypher(statement: """
CALL db.index.fulltext.queryNodes('bookIndex', $searchString+'~')
YIELD node RETURN node
""")
}
extend type Query {
booksForCurrentUser: [Book] @auth(rules: [{ isAuthenticated: true }]) @cypher(statement: """
MATCH (c:Customer {username: $auth.jwt.sub})-[:PLACED]->(:Order)-[:CONTAINS]->(b:Book)
MATCH (b)-[:ABOUT]->(s:Subject)<-[:ABOUT]-(rec:Book)
WITH rec, COUNT(*) AS score ORDER BY score DESC
RETURN rec
""")
}
{ "authorization":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJCb2JMb2JsYXc3Njg3Iiwicm9sZXMiOlsiYWRtaW4iXSwiaWF0IjoxNTE2MjM5MDIyfQ.f2GKIu31gz39fMJwj5_byFCMDPDy3ncdWOIhhqcwBxk"}
dFt8QaYykR6PauvxcyKVXKauxvQuWQTc
{ "authorization":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJNeVVzZXIxMjMiLCJpYXQiOjE1MTYyMzkwMjJ9.SNXGvSS3NaEVIUNHKxfN_FTXQGMjVnm7qZW7QFb5ur8"}
mutation {
createBooks(
input: {
isbn: "1492047686"
title: "Ancillary Justice"
price: 6.05
description: "Once, she was the Justice of Toren -- a colossal starship with an artificial intelligence linking thousands of soldiers in the service of the Radch, the empire that conquered the galaxy."
}
) {
books {
isbn
title
}
}
createCustomers(input: { username: "MyUser123" }) {
customers {
username
}
}
}
mutation {
createOrders(
input: {
books: { connect: { where: { node: { title: "Ancillary Justice" } } } }
customer: { connect: { where: { node: { username: "MyUser123" } } } }
shipTo: {
create: {
node: {
address: "Via Nemorense, Roma, Italy"
location: { latitude: 41.883333, longitude: 12.483333 }
}
}
}
}
) {
orders {
placedAt
books {
title
}
customer {
username
}
shipTo {
address
}
}
}
}
mutation {
createReviews(
input: {
rating: 5
text: "Great SF Book"
book: { connect: { where: { node: { title: "Ancillary Justice" } } } }
author: { connect: { where: { node: { username: "MyUser123" } } } }
}
) {
reviews {
rating
text
createdAt
book {
title
}
}
}
}
query {
customers {
username
orders {
placedAt
books {
title
}
shipTo {
address
}
}
reviews {
text
rating
book {
title
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment