Skip to content
The Swift GraphQL implementation for macOS and Linux
Branch: master
Clone or download
Latest commit 86b49d7 Mar 19, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Images make logo a bit bigger Oct 18, 2016
Sources/GraphQL
Tests Tidy up repeated test code into setup/teardown Mar 8, 2019
.gitignore it starts Oct 13, 2016
.swift-version
.travis.yml
LICENSE
Package.resolved swift package update Mar 8, 2019
Package.swift Reference wickwirew/Runtime by release tag 1.1.0 Jan 23, 2019
README.md Update README.md Mar 19, 2019

README.md

GraphQL

The Swift implementation for GraphQL, a query language for APIs created by Facebook.

Swift License Slack Travis Codecov Codebeat

Looking for help? Find resources from the community.

Graphiti

This repo contains the core GraphQL implementation. For a better experience when creating your types use Graphiti.

Graphiti is a Swift library for building GraphQL schemas/types fast, safely and easily.

Getting Started

An overview of GraphQL in general is available in the README for the Specification for GraphQL. That overview describes a simple set of GraphQL examples that exist as tests in this repository. A good way to get started with this repository is to walk through that README and the corresponding tests in parallel.

Using GraphQL

Add GraphQL to your Package.swift

// swift-tools-version:4.0
import PackageDescription

let package = Package(
    dependencies: [
        .package(url: "https://github.com/GraphQLSwift/GraphQL.git", from: "0.9.0"),
    ]
)

GraphQL provides two important capabilities: building a type schema, and serving queries against that type schema.

First, build a GraphQL type schema which maps to your code base.

import GraphQL

let schema = try GraphQLSchema(
    query: GraphQLObjectType(
        name: "RootQueryType",
        fields: [
            "hello": GraphQLField(
                type: GraphQLString,
                resolve: { _, _, _, _ in "world" }
            )
        ]
    )
)

This defines a simple schema with one type and one field, that resolves to a fixed value. More complex examples are included in the Tests directory.

Then, serve the result of a query against that type schema.

let query = "{ hello }"
let result = try graphql(schema: schema, request: query)
print(result)

Output:

{
    "data": {
        "hello": "world"
    }
}

This runs a query fetching the one field defined. The graphql function will first ensure the query is syntactically and semantically valid before executing it, reporting errors otherwise.

let query = "{ boyhowdy }"
let result = try graphql(schema: schema, request: query)
print(result)

Output:

{
    "errors": [
        {
            "locations": [
                {
                    "line": 1,
                    "column": 3
                }
            ],
            "message": "Cannot query field \"boyhowdy\" on type \"RootQueryType\"."
        }
    ]
}

Field Execution Strategies

Depending on your needs you can alter the field execution strategies used for field value resolution.

By default the SerialFieldExecutionStrategy is used for all operation types (query, mutation, subscription).

To use a different strategy simply provide it to the graphql function:

try graphql(
    queryStrategy: ConcurrentDispatchFieldExecutionStrategy(),
    schema: schema,
    request: query
)

The following strategies are available:

  • SerialFieldExecutionStrategy
  • ConcurrentDispatchFieldExecutionStrategy

Please note: Not all strategies are applicable for all operation types.

License

This project is released under the MIT license. See LICENSE for details.

You can’t perform that action at this time.