Skip to content

Latest commit

 

History

History
270 lines (230 loc) · 3.8 KB

custom-fields.adoc

File metadata and controls

270 lines (230 loc) · 3.8 KB

Custom queries and mutations

Setup

Schema
type Person {
  name: String
  label: String! @cypher(statement: "RETURN toLower(this.name)")
  nullable: String @cypher(statement: "RETURN null")
}

type Order {
  sid: ID!
  name: String
  description: String
  date: Int
  position: [OrderPosition] @relation(name:"PART_OF",direction:IN)
}

type OrderPosition {
  amount: Int
  order: Order @relation(name:"PART_OF")
  sid: ID!
}

schema {
  mutation: MutationType
  query: QueryType
}
type QueryType {
  Person(name:String) : Person @cypher(statement:"MATCH (p:Person { name: $name }) RETURN p")
  getOrderPosition(sid:ID!) : [OrderPosition] @cypher(statement:"MATCH (op:OrderPosition {sid: $sid}) RETURN op")
}

type MutationType {
  createPerson(name:String) : Person @cypher(statement:"CREATE (p:Person { name: \"Test\"+$name }) RETURN p")
  setOrderPositionAmount(sid:ID!, amount:Int) : [OrderPosition] @cypher(statement:"MATCH (op:OrderPosition {sid:{sid}}) SET op.amount = $amount RETURN op")
}
Schema Configuration
{
  "capitalizeQueryFields": true
}
Test Data
CREATE (:Person {name:'Jane'}), (:Person {name:'John'})

Mutation conflicts with auto-generated field

GraphQL-Query
mutation { createPerson(name:"Jill") {name} }
GraphQL-Response
{
  "createPerson" : {
    "name" : "TestJill"
  }
}
Cypher Params
{
  "createPersonName" : "Jill"
}
Cypher
CALL {
	WITH $createPersonName AS name
	CREATE (p:Person { name: "Test"+name }) RETURN p AS createPerson LIMIT 1
}
RETURN createPerson {
	.name
} AS createPerson

Test Data
CREATE (:Person {name:'Jane'}), (:Person {name:'John'})

Query conflicts with auto-generated field

GraphQL-Query
query { Person(name:"Jane") {name} }
GraphQL-Response
{
  "Person" : {
    "name" : "Jane"
  }
}
Cypher Params
{
  "personName" : "Jane"
}
Cypher
CALL {
	WITH $personName AS name
	MATCH (p:Person { name: name }) RETURN p AS person LIMIT 1
}
RETURN person {
	.name
} AS Person

Query non nullable value

GraphQL-Query
query { Person(name:"Jane") {name, label} }
GraphQL-Response
{
  "Person" : {
    "name" : "Jane",
    "label" : "jane"
  }
}
Cypher Params
{
  "personName" : "Jane"
}
Cypher
CALL {
	WITH $personName AS name
	MATCH (p:Person { name: name }) RETURN p AS person LIMIT 1
}
CALL {
	WITH person
	WITH person AS this
	RETURN toLower(this.name) AS personLabel LIMIT 1
}
RETURN person {
	.name,
	label: personLabel
} AS Person

Query nullable value

GraphQL-Query
query { Person(name:"Jane") {name, nullable} }
GraphQL-Response
{
  "Person" : {
    "nullable" : null,
    "name" : "Jane"
  }
}
Cypher Params
{
  "personName" : "Jane"
}
Cypher
CALL {
	WITH $personName AS name
	MATCH (p:Person { name: name }) RETURN p AS person LIMIT 1
}
CALL {
	WITH person
	WITH person AS this
	RETURN null AS personNullable LIMIT 1
}
RETURN person {
	.name,
	nullable: personNullable
} AS Person

Query field by getter

GraphQL-Query
query { OrderPosition { amount } }
GraphQL-Response
{
  "OrderPosition" : [ ]
}
Cypher Params
{ }
Cypher
MATCH (orderPosition:OrderPosition)
RETURN orderPosition {
	.amount
} AS OrderPosition