GraphQL-handler icon indicating copy to clipboard operation
GraphQL-handler copied to clipboard

GraphQL Handler with Swift Perfect Framework

GraphQL Handler

Swift GraphQL Handler

Swift Version License codebeat badge

A simple handler implementation of GraphQL using Perfect

How it works?

A simple route with a indexHandler call when GET on /graphql route.

routes.add(method: .get, uri: "/graphql", handler: indexHandler)

Define your schema with RootQueries, RootMutations, CustomTypes e etc...

let UserType =  try! GraphQLObjectType(
    name: "User",
    description: "A user in system",
    fields:[
        "id": GraphQLField(
            type: GraphQLNonNull(GraphQLString),
            description: "The id of the user."
        ),
        "name": GraphQLField(
            type: GraphQLString,
            description: "The name of the user."
        ),
        "email": GraphQLField(
            type: GraphQLString,
            description: "The email of the user."
        ),
    ]
)

In handler you will call the GraphQL parser:

let result = try graphql(schema: schema, request: query)

GraphQL library returns a map by default, to get a JSON you should use result.description like @paulofaria says here: #1

Usage example

make run

listen on :8080

http://localhost:8080/graphql?query=query{user(id: "3000"){id,name,email}}

Release History

  • 0.0.2
    • Improve Schema and add sample data
  • 0.0.1
    • Simple handler with basic schema

Meta

Vinicius Souza – @iamvsouza[email protected]

Distributed under the MIT license. See License

https://github.com/vsouza