GraphQLSchema
public protocol GraphQLSchema
GraphQL Schema
A GraphQL Schema describes the entire API that is available. It’s basically only a name space for your Query and Mutation Types.
Your Query and Mutation Types have to be Root Types with the same Viewer Context.
- A Query Type is mandatory.
- A Mutation Type is optional.
Examples
If you only want to offer a Query Type, you only need to implement that class:
enum MySchema {
class Query: QueryType {
func greeting(name: String = "World") -> String {
return "Hello, \(name)!"
}
init(viewerContext: ()) { ... }
}
}
that gets translated to the following schema:
type Query {
greeting(name: String! = "World"): String!
}
Alternatively you can also specify a Mutation Type:
enum MySchema {
class Query: QueryType {
func greeting(name: String = "World") -> String {
return "Hello, \(name)!"
}
init(viewerContext: ()) { ... }
}
class Mutation: MutationType {
func store(status: String) -> String { ... }
init(viewerContext: ()) { ... }
}
}
Which would generate this Schema:
type Query {
greeting(name: String! = "World"): String!
}
type Mutation {
store(status: String!): String!
}
If your types need to have a context for the current user, they need to use the exact same ViewerContext
:
enum MySchema {
typealias ViewerContext = User
class Query: QueryType {
let user: User
var greeting: String {
return "Hello, \(user.name)!"
}
init(viewerContext: User) { ... }
}
class Mutation: MutationType {
let user: User
func store(status: String) -> String { ... }
init(viewerContext: User) { ... }
}
}
-
Type of the Response of the Schema
Declaration
Swift
typealias Result = GraphQLResult
-
Type of an Empty Mutation Type. Signals GraphZahl that no mutations are allowed
Declaration
Swift
typealias None = EmptyRootType<ViewerContext>
-
Type that tells the Query/Mutation type everything about the User. This defaults to Void, to signal that all requests are treated the same, and this API does not compute anything different on a by user basis.
Declaration
Swift
associatedtype ViewerContext = Void where Self.ViewerContext == Self.Mutation.ViewerContext
-
Type of the Query for this Schema. Should conform to
QueryType
. And should have the same Viewer Context as the SchemaDeclaration
Swift
associatedtype Query : MandatoryRootType
-
Type of the Mutation for this Schema. Should conform to
MutationType
. And should have the same Viewer Context as the Schema. Defaults toNone
signalling GraphZahl that no mutations are allowed.Declaration
Swift
associatedtype Mutation : GraphQLRootType = Self.None where Self.Mutation.ViewerContext == Self.Query.ViewerContext
-
prepare(viewerContext:
Extension method) Undocumented
Declaration
Swift
public static func prepare(viewerContext: ViewerContext? = nil) throws
-
perform(request:
Extension methodviewerContext: variableValues: eventLoopGroup: ) Performs a GraphQL Request
Declaration
Swift
public static func perform(request: String, viewerContext: ViewerContext, variableValues: [String : Map] = [:], eventLoopGroup: EventLoopGroup? = nil) throws -> Future<GraphQLResult>
Parameters
request
Query Text
viewerContext
Viewer Context
Return Value
A future with the result with data and errors for the query
-
viewerContext
Extension methodUndocumented
Declaration
Swift
public static var viewerContext: Context.Key<ViewerContext> { get }
-
perform(request:
Extension methodvariableValues: eventLoopGroup: ) Performs a GraphQL Request
Declaration
Swift
public static func perform(request: String, variableValues: [String : Map] = [:], eventLoopGroup: EventLoopGroup? = nil) throws -> Future<GraphQLResult>
Parameters
request
Query Text
Return Value
A future with the result with data and errors for the query